• Post Reply Bookmark Topic Watch Topic
  • New Topic

InputStream blocking?  RSS feed

 
David Ball
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I'm a new Java Programmer messing with sockets for the first time. I'm having trouble with my InputStreams (actually ObjectInputStreams). Whenever I declare them in either my client or server it causes all functions to hang, and generally act like it is blocking. Any ideas? Code snipits are below:

============================================================================

public class LMSClient {
Socket fromServer = null;
ObjectOutputStream clientOut = null;
ObjectInputStream clientIn = null;

Holdings userLib;
Book[] myBooks;
LMSBook[] inBooks;

public LMSClient(InetAddress ip){
try {
fromServer = new Socket(ip, 6789);
} catch (IOException e2) {
System.out.println("Connection Failed");
e2.printStackTrace();
}
System.out.println("Connection Made!");
try {
clientOut = new ObjectOutputStream(new BufferedOutputStream(fromServer.getOutputStream()));
System.out.println("Output Stream good!");
clientIn = new ObjectInputStream(new BufferedInputStream(fromServer.getInputStream()));
System.out.println("Input Stream Good!");
} catch (IOException e) {
System.out.println("Error creating client streams.");
e.printStackTrace();
}

============================================================================

The statement "Output Stream Good!" prints to the console, but not "Input Stream Good!" There is similar code on the server side, but I only posted this to cut down on reading. I figure the problem is the same for both. Thanks!
 
peter wooster
Ranch Hand
Posts: 1033
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David Ball:
Hi there,

I'm a new Java Programmer messing with sockets for the first time. I'm having trouble with my InputStreams (actually ObjectInputStreams). Whenever I declare them in either my client or server it causes all functions to hang, and generally act like it is blocking. ...
The statement "Output Stream Good!" prints to the console, but not "Input Stream Good!" There is similar code on the server side, but I only posted this to cut down on reading. I figure the problem is the same for both. Thanks!


You must use threads to interweave the reads and writes when using the stream based Socket I/O. There are plenty of examples of this in the Java Tutorials. I'm sure you'll find a few if you search here. I've posted some rather advanced examples on the SUN forums.

The best approach for this is to use a thread for send that does the writes and a seperate thread for receive that does the reads. If your program is a Swing GUI these threads must not be the Event Dispatch Thread, or you will block the GUI. So you end up with 1+(2*connections) threads. If your program is command line you can probably use the main thread to support the send side. If your protocol is full duplex you will definitely need 2 threads per connection, if its simplex or half duplex you can get away with one thread per connection.

If you are using Swing you will need to use invokeLater to get your swing updates done on the Event Dispatch Thread.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!