Advertisement

mutlithreaded server problem

Started by October 28, 2005 09:49 AM
0 comments, last by Funcdoobiest 19 years, 3 months ago
Can anyone please help me, I am trying to write a multithreaded server. below is the while loop for the thread which handles a client, at the moment it just receives data and sends it to all clients (except the original sender), or at least that was the aim but when I run it the first client to connect sends it's data which is received by the server but then it just sits there. When a second client connects its sends continuosly (as intended) and receives the dat back from the client. if I remove (i != sockListener) the program runs fine for a short while before timeouts start creeping in every so often on the WaitFor...() until they occur constantly. One thing I have never understood is what does it mean when a socket is not ready to send (like you may find out using select()) does it simply mean there is no space in the underlying protocol buffers? Can these buffers only be emptied by the receving socket calling recv()? My only theory is that sending to the listener constantly is filling the buffer so send() ends up blocking infinitely preventing release of the mutex and evetually killing my program? I'm completely newe to all this network stuff (as if this wasn't obvious!!) so as ever any help is greatly appreciated!

			recv(sockComm,(char*) &Data, sizeof(Data), 0);
			printf("received %d, %f\n", Data.iInt, Data.fFloat);
			printf("received %d, %d, %d, %d\n\n\n", Data.iArray[0], Data.iArray[1], Data.iArray[2], Data.iArray[3]);
			
			//attempt to obtain mutex
			dwWaitResult = WaitForSingleObject(mutexMasterSet, 1000);
			
			switch(dwWaitResult){
				//if we have mutex process sends
				case WAIT_OBJECT_0:
						// send to everyone!
						for(i = 1; i <= sockMax; i++) { 
							if (FD_ISSET(i, &fdsMasterSet)) {
								// except ourselves (and listener)
								if ((i != sockComm) && (i != sockListener)) {
									send(sockComm, (char*) &Data, sizeof(Data), 0);
									printf("sent %d, %f\n\n\n", Data.iInt, Data.fFloat);
								}
							}
						}	
						//finished with masterset so release relevant mutex
						ReleaseMutex(mutexMasterSet);
						break;
				//if we timed out - deal with it
				case WAIT_TIMEOUT:
					printf("WAIT TIMED OUT\n\n");
					break;
				//not clear what abandoned means exactly, look up
				case WAIT_ABANDONED:
					printf("WAIT ABANDONED\n\n");
					break;
			}


EDIT I have just realised also that the clients are being echoed back their own data but not sent data from the others. Maybe investigating this will turn up the problem, in the meantime though any suggeations are still welcome!
Its sorted, embaressingly simple error!!

send(sockComm, (char*) &Data, sizeof(Data), 0);

should have been

send(i, (char*) &Data, sizeof(Data), 0);

oops!

This topic is closed to new replies.

Advertisement