C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 12-20-2004, 02:29 PM   #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.
dpro is offline   Reply With Quote
Old 12-20-2004, 06:41 PM   #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?).
azteched is offline   Reply With Quote
Old 12-20-2004, 06:46 PM   #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
dpro is offline   Reply With Quote
Old 12-20-2004, 06:51 PM   #4
Registered User
 
Join Date: Dec 2004
Posts: 95
Not sure that's the whole problem, I haven't looked very hard
azteched is offline   Reply With Quote
Old 12-20-2004, 06:53 PM   #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
dpro is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 03:19 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22