Jump to content
  • Advertisement
Sign in to follow this  
helldoggie666

[java] Socket problem

This topic is 4026 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

Hey, I have a little problem with my program. I have a client with a thread that is listening for incomming commands from the server. This is done with sockets. Also the server has a same kind of thread for the client commands. So what I'm trying to do is just communicating between the server and the client with sockets. I do two request, one for login in and right after that one for retrieving some data. The first request works fine, but I don't get anything back for the second request. I noticed that when I let the client sleep in between for 100 milliseconds the second request is going fine too. I use the same socket for incomming as outgoing requests. Can this be the problem? Or do I need some interval between requests? Some indication what could be wrong is ok, then i can search further for it. Thanks Tim

Share this post


Link to post
Share on other sites
Advertisement
Quote:
I have a client with a thread that is listening for incomming
commands from the server.

Are you using 'listening' literally here, as in, your client socket is actually performing the LISTEN operation? I suspect you meant 'receiving', but it is necessary to ask this question to make absolutely sure you do.

Quote:
I do two request, one for login in and right after that one for
retrieving some data. The first request works fine, but I don't
get anything back for the second request.
I noticed that when I let the client sleep in between for 100 milliseconds
the second request is going fine too.

By default, java has Nagle's algorithm enabled which will buffer small messages and send them as one message to reduce the amount of network traffic. As you have given no details about exactly how you send your data, or what data you are sending I can only suspect that this is happening, but your observation of how it 'works' when you delay between the two send calls seems to confirm it.

What you could do is implementing a way of detecting when a packet has ended, for example, by sending the length of the packet along with it so you know how much data belongs to that one packet. You can also disable Nagle's algorithm by setting the TCP_NODELAY option with the Socket.setOption method.

Hoping this helps,

Rogier

Share this post


Link to post
Share on other sites

Here is my code which I use for listening for incomming commands.


public void run(){
/*
* The stream listener will listen for all incoming objects and send them
* to the clientconnection.
*/

ObjectInputStream is = null;

try {
is = new ObjectInputStream(socket.getInputStream());
Object object = null;
while(clientConn.isRunning() && (object = is.readObject())!=null){
System.out.println("object from server");
buildClientCommand(object);
}
} catch (IOException e) {
//System.out.println("Client stream:" + e.toString());
} catch (ClassNotFoundException e) {
System.out.println("Error in client stream listener:" + e.toString());
}
try {
if(is!=null)
is.close();
} catch (IOException e) {
System.out.println("Error in client stream listener when trying to close:" + e.toString());
}
}



Hmm I don't know Nagle's algoritme. Ok search some info about that.

Thanks :-)

Share this post


Link to post
Share on other sites
Hi,

Your code is fine for listening I don't see any potential problems there. I would suggest making sure that when the Server sends both requests that you flush the ObjectOutputStream after sending because like rogierpennink said java buffers messages, flushing will cause the message to be sent immediately.

Good Luck!

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!