Prob with disconnects using Winsock2

Started by
4 comments, last by TalonSnow 18 years, 11 months ago
Ok, I have given up on DirectPlay and moved to Winsocks, specifically using Winsock2. I am trying to setup an ansychronous threaded server and ansychronous client. Right now I am able to have the server create a thread for every connection, and send a message to the client, as well as the client receiving the message. I do this by writing out on the server how many connections I have received, and on the client a message if they are receiving a message or not. Seems to work fine as i increase the clients: PROBLEM: Once I close one client, they all stop working. Stop working as in they stop receiving messages, and the server stops sending out a message to the clients still open. Also another small problem I haven't tracked down, is when i close a client, sometimes the number of clients will reduce by one, and other times it wont. and lastly in the game loop for the client, which runs every frame it calls netReceive in the network engine on the client, which tries to catch an incoming message. Any help or suggestions would be appreciated, and thankyou if you took the time to read through my post, I know it is alot of code. ***** Code taken out and problem has been fixed, solution below. HOWEVER, new problem occuring now as stated in 4th replay made by me ******* [Edited by - TalonSnow on April 28, 2005 9:10:33 PM]
<=- Talon -=>
Advertisement
It seems that you're running into a thread synchronization problem. Two threads are accessing the numPlayers variable at once, and only one of them gets its result written.
You should look into synchronization functions like EnterCriticalSection.

Edit (Offtopic): There's no point at all in allocating 256 bytes on the heap every loop. 256 bytes is nothing, considering you have a 1Mb stack to play with (usually). Just allocate the buffers on the stack, and save yourself time and bother.
taking out the numPlayers-- still makes it behave the way it does however. I can see where the thread is fighting over it and therefore the plers number isnt decreased on a closed connection. But why would all threads stop if one ends?
<=- Talon -=>
Hmm, I can't really tell from he code you've posted. However, if a client disconnects, then send() will fail, numPlayers will be reduced, then it'll loop again and call send() on a closed socket. Which will also fail.

Also (offtopic again), you do know that your send() call might result in multiple recv()s, because of the way TCP/IP works, right? In which case you might get one "message sent" on the server, but several "message recieved" messages on the client.
ah, i think you just gave me the idea on why it's not working! :)Ill have to test it once i get back, to see, but mabey. I was thinking for some stupid reason that when i closed the socket the thread would stop as well. So on my server i go from 3 connects to none if one quits, but thats because it keeps looping and decreasing the numplayers every cycle. So im assuming that the others are all still connected but that the server doesnt show that they are. Well anyways I hope this is the reason :)
<=- Talon -=>
ok that wasn't the problem, but i have fixed the initial problem. The main problem was I was sending the same socket info to all clients, plus the ID on the second thread was the same for every case it ran. So I applied an array to the indentifyer now for the second thread and added [x] to the array it passed, therefor giving each thread its own unique handle. So now they all are running seperate with seperate sockets for the connections.
Then i came across the problem, where the initial looping thread to find connections was hanging on the Accept method, so i made it not wait now by assigning the attribute ioctlsocket to make it synchronous.

NEW PROBLEM:
Now it seems to run just fine, except for the new client version. it is using same code as sample client, but with all the other bells and whistles (graphics, sound, input, etc..) and not just the networking. On some computers it will connect just fine, and on others it wont even connect. Any ideas on why this would happen? Whats even stranger is the fact that if i use the basic client (exact same networking code) it will connect on computers that the main client wont work on. So im stumped again :)

Any help would be appreciated.
<=- Talon -=>

This topic is closed to new replies.

Advertisement