File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I/O stream from Socket not ready

 
Richard Scothern
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Richard,
You need to flush the stream. Try adding out.flush() after out.newLine() and it should work.
Michael Morris
 
Richard Scothern
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I already tried this and unfortunately it doesn't work.
Richard
 
Michael Morris
Ranch Hand
Posts: 3451
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic