Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


SDLNet - Disconnect on Recv(...);?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 Exiled   Members   -  Reputation: 105

Like
0Likes
Like

Posted 05 June 2012 - 09:57 PM

I'm aware I just made a post about SDL_net; however, I have run into a new problem. Packets are sending and getting received. However, when the server receives the data, the client simply disconnects. The SDLNet_TCP_Recv(...); function returns -1. I cannot find the error, for SDLNet_GetError(); isn't working for me(it returns nothing). Has anybody run into this problem before?

My code is below.

//Receiving Code

result = SDLNet_CheckSockets(player_sockSet, 0);
    //loop through each client
    for(int client = 0; client < maxPlayers; client++) {
	    //check the socket for activity
	    int clientSocketActivity = SDLNet_SocketReady(players[client].sock);
	    if(players[client].connected == true && clientSocketActivity > 0) {
		    //attempt to read from the socket.
		    //non-positive return values indicate error
		    int bytesRecv = SDLNet_TCP_Recv(players[client].sock, msg, 256);
		   
		    if(bytesRecv > 0) {
			    //display the result
			    stringstream ss;
			    ss << "Received " << bytesRecv << " bytes from client " << client << "!";
			    consoleOut(ss.str());
		    }
		    else {
			    //there was an error
			    stringstream ss;
			    ss << "Player " << client << " has disconnected.";
			    consoleOut(ss.str());
			    players[client].connected = false;
			    connectedPlayers--;
			   
			    //disconnect the socket
		    }
	    }
    }

Please excuse the bad indenting, it's because of copy + paste.

Thanks in advance!
-Ex

Sponsor:

#2 hplus0603   Moderators   -  Reputation: 5724

Like
0Likes
Like

Posted 06 June 2012 - 12:04 AM

It looks like the server falls off the end and exits its process when it receives something.
If it does that, the socket gets automatically closed by the kernel, and the client will be disconnected.
enum Bool { True, False, FileNotFound };

#3 Exiled   Members   -  Reputation: 105

Like
0Likes
Like

Posted 06 June 2012 - 12:08 AM

It looks like the server falls off the end and exits its process when it receives something.
If it does that, the socket gets automatically closed by the kernel, and the client will be disconnected.


Hmm. That makes sense - any ideas on how I can fix it?

#4 Tribad   Members   -  Reputation: 887

Like
0Likes
Like

Posted 06 June 2012 - 12:55 AM

I would suggest to install a network sniffer like wireshark to see what your connections do.
So you can see who is disconnecting, the client or the server.

#5 Exiled   Members   -  Reputation: 105

Like
0Likes
Like

Posted 06 June 2012 - 03:22 AM

I would suggest to install a network sniffer like wireshark to see what your connections do.
So you can see who is disconnecting, the client or the server.

I already know the client is disconnecting, the server can easily handle more connections.

#6 Tribad   Members   -  Reputation: 887

Like
0Likes
Like

Posted 06 June 2012 - 04:12 AM

Aha
And what is the servers code posted then. I think that the client code is needed for review. It is absolutely ok that the server gives you a -1 if the client send an RST or FIN. The question is what is wrong in the client code that it closes the connection after sending.
If the client dies before the packet has been processed by the server the client sends a RST. That clears anything that already has been received by the OS socket code. This is why termination should first shutdown, FIN, the connection. This allows the receiver to process any data already received. And if that is done the socket gets closed, RST.

So I would think the client dies and the OS skips the shutdown, FIN, so you are getting a close, RST.

#7 Exiled   Members   -  Reputation: 105

Like
0Likes
Like

Posted 06 June 2012 - 04:13 AM

Aha
And what is the servers code posted then. I think that the client code is needed for review. It is absolutely ok that the server gives you a -1 if the client send an RST or FIN. The question is what is wrong in the client code that it closes the connection after sending.
If the client dies before the packet has been processed by the server the client sends a RST. That clears anything that already has been received by the OS socket code. This is why termination should first shutdown, FIN, the connection. This allows the receiver to process any data already received. And if that is done the socket gets closed, RST.

So I would think the client dies and the OS skips the shutdown, FIN, so you are getting a close, RST.


I am using Telnet as a test client. Therefore, the code problem is with the server.

#8 Tribad   Members   -  Reputation: 887

Like
0Likes
Like

Posted 06 June 2012 - 05:43 AM

So I take from your words, that you have not analysed the traffic.
This is only an indication. Only if you analyse the packets transmitted you have knowledge. It is a guess that telnet does not terminate the session at will.

#9 Exiled   Members   -  Reputation: 105

Like
0Likes
Like

Posted 06 June 2012 - 12:01 PM

So I take from your words, that you have not analysed the traffic.
This is only an indication. Only if you analyse the packets transmitted you have knowledge. It is a guess that telnet does not terminate the session at will.


It is not a guess; it happened with my game client as well.

#10 Tribad   Members   -  Reputation: 887

Like
1Likes
Like

Posted 06 June 2012 - 12:20 PM

As I said before. You do not analysed the traffic.

This only makes it a better guess.
Between you clients and the server you have the TCP/IP stacks of two machines and the wireing between them. Or if they run on one machine you even have the TCP/IP stack between the two application and the operating system anyways.
If you have a hardware with multiple cores running you have parallel execution of the two applications. That a lot of stuff. And as long as you do not check the traffic between your clients and the server you are only guessing.

You are seeing misbehaviour and you do not have a clue whats wrong. So you worry about whats wrong but you must prove your result. This is how failure analysis is done. And while developing software you will do this all the time. If you are not proving your idea about the failure you cannot exclude things.

What you are doing right now is excluding without knowledge. This is fatal and not best practice.

#11 Exiled   Members   -  Reputation: 105

Like
0Likes
Like

Posted 06 June 2012 - 02:07 PM

As I said before. You do not analysed the traffic.

This only makes it a better guess.
Between you clients and the server you have the TCP/IP stacks of two machines and the wireing between them. Or if they run on one machine you even have the TCP/IP stack between the two application and the operating system anyways.
If you have a hardware with multiple cores running you have parallel execution of the two applications. That a lot of stuff. And as long as you do not check the traffic between your clients and the server you are only guessing.

You are seeing misbehaviour and you do not have a clue whats wrong. So you worry about whats wrong but you must prove your result. This is how failure analysis is done. And while developing software you will do this all the time. If you are not proving your idea about the failure you cannot exclude things.

What you are doing right now is excluding without knowledge. This is fatal and not best practice.


"I do not analysed the traffic"....

You have no direct point, Telnet is developed by Microsoft. Both my client and Telnet disconnect when the server receives something. This means the fault is with the server; it cannot be with the client.

#12 hplus0603   Moderators   -  Reputation: 5724

Like
0Likes
Like

Posted 06 June 2012 - 04:17 PM

You need to bring up the server in a debugger. Put a breakpoint where it receives the data. Look at what it does -- step through each line, until you get to a point where the server either closes the socket, or exits the process. Congratulations! You have now found the point where the server drops the client!

enum Bool { True, False, FileNotFound };




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS