Results 1 to 2 of 2

Thread: [HELP] CL_Socket always disconnect

  1. #1

    Default [HELP] CL_Socket always disconnect

    Hi

    I've been experimenting with the CL_Socket class I have a problem: After calling the accept function on the listening socket, the returned socket is disconnected right away.

    However I also found that it was possible to successfully send data to the new accepted socket and have it receive it correctly. The problem is that after receiving the data, it disconnect itself right away.

    This code from socket_generic.cpp explain what is happening
    Code:
    void CL_Socket_KeepAliveHelper::keep_alive()
    {
    ...
    
    	int result = select(impl->sock+1, &rfds, &wfds, &efds, &tv);
    	if (result > 0)
    	{
    		if (FD_ISSET(impl->sock, &rfds))
    		{
    
    			// Peek and see if there is anydata on the socket, if not 
    			// the remote side has disconnected.
    			nread = recv(impl->sock, &buf, 1, MSG_PEEK);
    
    			if(nread == 0)
    			{
    				sig_disconnected();
    				impl->disconnect();
    				return;
    			}
    			else
    				sig_read_triggered();
    		}
    ...

    So if the select function indicate that the socket have data ready to be read, but then that the data length is 0, it disconnect it.

    Is that the correct behavior? Why does my socket always reach this state and get disconnected?

    Here is some of my code:
    Code:
    void TestSck::Listen(char *port)
    {
    	if(state == state_ready)
    	{
    		IP->set_port(std::string(port));
    		sck->bind(*IP);
    		sck->listen(10);
    		state = state_listen;
    	}
    }
    
    void TestSck::Connect(char *host, char *port)
    {
    	if(state == state_ready)
    	{
    		IP->set_address(std::string(host), std::string(port));
    		sck->connect(*IP);
    		state = state_connect;
    	}
    }
    
    
    void TestSck::on_sck_read()
    {
    	if(state == state_listen)
    	{
    		CL_Socket newSocket = sck->accept();
    
    		CL_IPAddress sourceIP = newSocket.get_dest_address();
    		CL_IPAddress destIP = newSocket.get_source_address();
    
    		std::cout << "SocketID " << id << " Listen Read" << std::endl << "Source = " << sourceIP.get_address() << std::endl << "Target = " << destIP.get_address() << std::endl << std::endl;
    
    		newSocket.send(std::string("Handshake ffs"));
    
    		TestSck *ts = new TestSck(&newSocket);
    	}
    	else
    	{
    		const int nBuff = 1024;
    		char vBuff[nBuff];
    		memset(vBuff, 0, sizeof(vBuff));
    		int len = sck->recv(vBuff, nBuff);
    		while(len)
    		{
    			std::cout << "SocketID " << id << " Read" << std::endl << vBuff << std::endl << std::endl;
    			len = sck->recv(vBuff, nBuff);
    		}
    	}
    }
    I use one instance of my TestSck class to Listen, and another instance to connect.

    Upon connection my client socket successfully receive the handshake message, but then right away disconnect (the sig_disconnected signal is received).


    Why is my connection always disconnecting?
    In ClanLib code, why does the select return info that the socket is ready to be read and then find that there are 0 bytes to read and close it?
    What is wrong here?

    Thanks

  2. #2

    Default

    I found the problem.

    Code:
    void TestSck::on_sck_read()
    {
    	if(state == state_listen)
    	{
    		CL_Socket newSocket = sck->accept();
    		...
    Here newSocket is local and goes out of scope when the function exit.
    Then the destructor in CL_Socket does it's job and disconnect.
    duh

Similar Threads

  1. use CL_Socket in ClanLib 0.8
    By silekey in forum Official ClanLib SDK Forums
    Replies: 1
    Last Post: 07-10-2007, 10:00 AM

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •