Thread: Asynchronous sockets

  1. #1
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250

    Asynchronous sockets

    Hmm done a lot of reserach on this, but still I cannot find out why this is failing...


    Basically I have set up one test program to have one listening socket, and one client socket, I use Telnet to contact my test program, it works fine, has async sockets, and whenever I type it spits stuff onto the screen.

    Now to the problem:

    I have another program which uses an STL map to hold all the incoming sockets...

    here's the relevant socket handling code..



    Code:
    HRESULT Cappsvr2View::socketMsg(WPARAM wp, LPARAM lp)
    {
    	if(WSAGETSELECTERROR(lp))
    	{
    		WSACleanup();
    		TRACE0("Get select error, bailing out!");
    		exit(1);
    	}
    	int recvd = 0;
    	int sockNum = (int)wp;
    	int tsockNum = 0;
    	char * tmpbuf;
    	char buf[100];
    	ZeroMemory((void *)buf, 100);
    	sockaddr_in clientSock;
    	int lenclient = sizeof(clientSock);
    	int errs = 0;
    	CString tmp;
    	LANCLIENT * tmpclient = new LANCLIENT;
    	std::map<int, LANCLIENT*>::iterator it;
    
    	switch(WSAGETSELECTEVENT(lp))
    	{
    		case FD_ACCEPT:
    			//AfxMessageBox("Client connecting!");
    			tmpclient->ClientSocket = accept(wp, (LPSOCKADDR)&clientSock, &lenclient);
    			errs = WSAAsyncSelect(tmpclient->ClientSocket, GetSafeHwnd(), WM_ONSOCKET, FD_WRITE | FD_READ | FD_CLOSE);
    			TRACE1("Error code for AsyncSelect is %d", errs);
    			if(INVALID_SOCKET == tmpclient->ClientSocket)
    			{
    				errs = WSAGetLastError();
    				TRACE1("ERROR: Socket error: %d\n", errs);
    				WSACleanup();
    			}
    			tmpclient->clnt_addr.sin_addr   = clientSock.sin_addr;
    			tmpclient->clnt_addr.sin_family = clientSock.sin_family;
    			tmpclient->clnt_addr.sin_port   = clientSock.sin_port;
    			tmpclient->currentMsg = "";
    			connections[(int)tmpclient->ClientSocket] = tmpclient;
    			TRACE2("Client Socket is %d and sockNum is %d\n", 
    				connections[(int)tmpclient->ClientSocket]->ClientSocket, sockNum);
    			
    			TRACE1("Current sockets is %d", connections[(int)tmpclient->ClientSocket]->ClientSocket);
    			break;
    		case FD_READ:		
    			recvd = recv((SOCKET)wp, buf, (int)strlen(buf), 0);
    			//TRACE2("Socket num is %d and wp is %d", connections[sockNum]->ClientSocket, (int)wp);
    			errs = WSAGetLastError();
    			//send(connections[sockNum]->ClientSocket, "welcome\n", (int)strlen("welcome\n"), 0);
    
    			if(recvd == SOCKET_ERROR)
    			{
    				recvd = WSAGetLastError();
    				TRACE1("Error with error code %d", recvd);
    			}
    			else if(errs == WSAEWOULDBLOCK)
    			{
    				TRACE0("Getting would block error!");
    			}
    			else if(recvd > 0)
    			{
    				
    				buf[recvd] = '\0';
    				tmpbuf = new char[(int)strlen(buf) + 1];
    				strcpy(tmpbuf, buf);
    				connections[sockNum]->currentMsg += tmpbuf;
    				TRACE1("We received %s", tmp);
    				//send(Clients[clientCount-1], tmpbuf, (int)strlen(tmpbuf), 0);
    				send(wp, tmpbuf, (int)strlen(tmpbuf), 0);
    				delete tmpbuf;
    				tmpbuf = NULL;
    			}
    			//AfxMessageBox(buf); 
    			break;
    		case FD_WRITE:
    			it = connections.begin();
    			while(it != connections.end())
    			{
    				send(it->second->ClientSocket, "Hello 2 you!\n", (int)strlen("Hello 2 you!\n"),0);
    				it++;
    			}
    			break;
    		case FD_CLOSE:
    			AfxMessageBox("Client terminated connection!");
    			break;
    
    		default:
    			break;
    	}
    	//delete tmpclient;
    	//tmpclient = NULL;
    	return 1;
    }

    here's the struct LANCLIENT


    Code:
    struct LANCLIENT
    	{
    		int InUse;
    		SOCKET ClientSocket;
    		struct sockaddr_in clnt_addr;
    		CString currentMsg;
    	};
    I just lose the ability to talk with the telnet client when I use the map, perhaps I am being an idiot and not finding what I need to know, but all I wanted to do was accumulate what the telnet client typed for the moment, any help would be much appreciated, thanks!

    Also whenever I type in the telnet client field, I get the FD_READ message, but it always contains 0 bytes when I do a recv... which I read means basically the connection is terminated.
    Last edited by dpro; 12-20-2004 at 02:32 PM.

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    recvd = recv((SOCKET)wp, buf, (int)strlen(buf), 0);

    You've already Zeromemoried buf, so strlen is going to return zero - you're telling recv your buffer length is zero! Try

    recvd = recv((SOCKET)wp, buf, sizeof(buf), 0);

    Also, you don't need to zero the buffer - don't treat it like a c-string, because it's not - you'll run into problems (e.g. what if the client sends a null as part of the data?).

  3. #3
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    ahhh thanks I had read that you should always zeromemory it, but thats what I get for blindly following all forum advice that worked like a charm, I didn't realize that was the whole problem. Thanks a bunch

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    Not sure that's the whole problem, I haven't looked very hard

  5. #5
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    hehe well it certainly worked, and it makes sense... but I wasn't really thinking about it beforehand. however if you spot something glaringly bad or bad codewise feel free to let me know

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Discerning sockets using asynchronous
    By jmd15 in forum Networking/Device Communication
    Replies: 4
    Last Post: 02-16-2006, 02:03 PM
  2. question about asynchronous sockets
    By pode in forum Networking/Device Communication
    Replies: 8
    Last Post: 12-27-2004, 02:33 PM
  3. Asynchronous Sockets Class
    By neandrake in forum Networking/Device Communication
    Replies: 0
    Last Post: 11-17-2003, 09:42 AM
  4. Starting window sockets
    By _Cl0wn_ in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2003, 11:49 AM
  5. Asynchronous sockets?
    By SyntaxBubble in forum Windows Programming
    Replies: 2
    Last Post: 02-04-2002, 09:33 PM