[java] Socket problem

Started by
2 comments, last by 5MinuteGaming 16 years, 9 months ago
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
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


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 :-)
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!

This topic is closed to new replies.

Advertisement