File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes I/O stream from Socket not ready Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "I/O stream from Socket not ready" Watch "I/O stream from Socket not ready" New topic
Author

I/O stream from Socket not ready

Richard Scothern
Ranch Hand

Joined: May 25, 2001
Posts: 83
Hi,
I'm having a little problem with some sockets. I'm creating client and server sockets to talk to each other, however my serversude socket is never receiving data.
// client
socket = new Socket(IP_ADDRESS, PORT_NUMBER);
out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.write("BIDDER:" + name);
out.newLine();
// server
socket = serverSocket.accept();
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
..
..
try {
if(!in.ready()) {
System.out.println("in not ready " + i);
continue;
}
message = in.readLine();
}
catch(IOException ioe) {
...
}
After the client sends the data to the server, a new thread is created with the code above in. However, in.ready() always returns false. I think this may be because there is no data in the stream, but can't figure out why.
There are no exceptions thrown, so the connections are being made.
Any ideas?
Thanks,
Richard
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Richard,
You need to flush the stream. Try adding out.flush() after out.newLine() and it should work.
Michael Morris


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Richard Scothern
Ranch Hand

Joined: May 25, 2001
Posts: 83
Hi,
I already tried this and unfortunately it doesn't work.
Richard
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
I'm not one to dispute the words of a fellow Java brother but flushing the streams definitely does work. I wrote a test client and server. If the stream is not flushed, the server hangs; when the stream is flushed all proceeds as expected.
Here's the code:
TestServer.java

TestClient.java

Michael Morris
[ February 25, 2003: Message edited by: Michael Morris ]
Richard Scothern
Ranch Hand

Joined: May 25, 2001
Posts: 83
hmmm, ok.
I do believe you, it just didn't work for me, for some reason.
I got around it by just using an auto-flushing printwriter. But I'll take another look.
Thanks for your help.
Richard
 
 
subject: I/O stream from Socket not ready