Jump to content
  • Advertisement
Sign in to follow this  

Prob with disconnects using Winsock2

This topic is 4771 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!