Sign in to follow this  

Java sockets, message won't send unless I close the socket

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

When I'm sending messages it will not actually send the text unless I close the connection, but I need to send multiple messages back and fourth during a single connection. heres my source:
Socket socket = new Socket("localhost", 3333);
				
DataOutputStream o = new DataOutputStream(socket.getOutputStream());
o.writeBytes("hello");

//closing it is the only way to get it to send, using either
o.close();
//or
socket.close()

The receiver source is
ServerSocket listenSocket = new ServerSocket(3333);
BufferedReader inFromConn = new BufferedReader(new InputStreamReader(socket.getInputStream()));						
while(!inFromConn.ready());

System.out.println(inFromConn.readLine()); //isn't called until the connection is closed

thanks

Share this post


Link to post
Share on other sites
IIRC the Socket class uses a tcp connection, which will be buffered. A flush() call should cause it to send any pending data.

Remember that tcp is a stream protocol, not a packet protocol. That means that your "messages" may be split up or merged by the time they reach the other end of your connection. (See Q14 in the network forum faq http://www.gamedev.net/community/forums/showfaq.asp?forum_id=15 ).

Share this post


Link to post
Share on other sites
I tried flush(), but it did seemingly nothing.


edit:
I got it working with this:

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println("hello");


I'm not sure what the difference between the two output clases, but it works how i want.

[Edited by - johnnyBravo on September 15, 2007 7:41:34 AM]

Share this post


Link to post
Share on other sites
Are you really fulshing the right stream? becuase were ever you have a buffer you must flush inorder to get an immidate send. Make sure you call flush on the outer most enclosing stream, otherwise you might as well be doing nothing. And if it doesn't work something is really really wrong.

Share this post


Link to post
Share on other sites
Quote:
Original post by johnnyBravo
I'm not sure what the difference between the two output clases, but it works how i want.


The difference is the newline written by println. The readLine method waits for a newline. If you had used writeBytes("hello\n"), your first example would have worked (I think).

Share this post


Link to post
Share on other sites

This topic is 3742 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this