• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

I/O stream from Socket not ready

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I already tried this and unfortunately it doesn't work.
Richard
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic