Discussion:
Sockets problem: getInputStream hanging
(too old to reply)
Allan Bruce
2006-01-12 23:13:01 UTC
Permalink
I am starting Java network programming for the first time although I have
experience in Java itself and socket programming in C++. I have an IM
server already running which I use to communicate with C++ clients and that
all runs fine. Now I want to develop a Java client but am having problems.
When I connect the socket then try to get the input stream my program just
hangs. The code is below, if anyone can tell me why this might happen, I
would appreciate it.

Another point is that, I want to send actual bytes on the line and receive
actual bytes, what type of input/output stream should I be using?

Thanks
Allan


private boolean connectToAServer()
{
System.out.println("Attempting to connect to a server...");

try
{
mSocket = new Socket(Globals.gPrefs.mainServer(),
Globals.gPrefs.mainServerPort());
}
catch (Exception e)
{
System.err.println("Could not connect to main server: " + e);
}

if (mSocket == null)
{
try
{
mSocket = new Socket(Globals.gPrefs.backupServer(),
Globals.gPrefs.backupServerPort());
}
catch (Exception e)
{
System.err.println("Could not connect to backup server: " +
e);
}
}

if (mSocket == null)
return ERROR;

System.out.println("Connection established on " +
mSocket.getInetAddress());

try
{
mOutput = new ObjectOutputStream( mSocket.getOutputStream());
mOutput.flush();
mInput = new ObjectInputStream(mSocket.getInputStream()); //
hangs here
}
catch (Exception e)
{
closeSocket();
return ERROR;
}

return OK;
}
Roedy Green
2006-01-12 23:21:43 UTC
Permalink
Post by Allan Bruce
mOutput = new ObjectOutputStream( mSocket.getOutputStream());
mOutput.flush();
mInput = new ObjectInputStream(mSocket.getInputStream()); //
C/C++ won't have a clue how to create or read an ObjectStream. You
want perhaps a DataOutputStream or a LEDataOutputStream.

See http://mindprod.com/applets/fileio.html
for sample code.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Allan Bruce
2006-01-13 01:11:59 UTC
Permalink
Post by Roedy Green
Post by Allan Bruce
mOutput = new ObjectOutputStream( mSocket.getOutputStream());
mOutput.flush();
mInput = new ObjectInputStream(mSocket.getInputStream()); //
C/C++ won't have a clue how to create or read an ObjectStream. You
want perhaps a DataOutputStream or a LEDataOutputStream.
See http://mindprod.com/applets/fileio.html
for sample code.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Thanks, I have some successful comms now :o) Can you help with the
following:

I have setup my socket as in my original post, and I can verify that it
sends data fine to the server and I can read a packet. How can I setup this
socket to listen for packets now? In C I would add them to a polling set
and continually check this to see if any of my sockets had data on them.
How would I implement this in Java?

Thanks
Allan
Roedy Green
2006-01-13 02:08:34 UTC
Permalink
Post by Allan Bruce
I have setup my socket as in my original post, and I can verify that it
sends data fine to the server and I can read a packet. How can I setup this
socket to listen for packets now? In C I would add them to a polling set
and continually check this to see if any of my sockets had data on them.
How would I implement this in Java?
There are two sorts of thing you can do:

1. accept TCP/IP streaming socket connections from the outside world.
Have a look at http://mindprod.com/products1.html#ECHOSERVER for the
world's most mindless Java server. It shows you the essential trick.

2. accept UDP packets from the outside world. I don't have code for
that handy. But you could get started by reading
http://mindprod.com/jgloss/udp.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Gordon Beaton
2006-01-13 07:38:16 UTC
Permalink
Post by Allan Bruce
I have setup my socket as in my original post, and I can verify that
it sends data fine to the server and I can read a packet. How can I
setup this socket to listen for packets now? In C I would add them
to a polling set and continually check this to see if any of my
sockets had data on them. How would I implement this in Java?
You have basically two choices:

- create a thread to handle each connection, and use blocking read
calls.

- use a java.nio.channels.Selector, much the same way you would use
select() in C to multiplex many connections on a single thread.

/gordon
--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Allan Bruce
2006-01-13 15:30:51 UTC
Permalink
Post by Gordon Beaton
Post by Allan Bruce
I have setup my socket as in my original post, and I can verify that
it sends data fine to the server and I can read a packet. How can I
setup this socket to listen for packets now? In C I would add them
to a polling set and continually check this to see if any of my
sockets had data on them. How would I implement this in Java?
- create a thread to handle each connection, and use blocking read
calls.
- use a java.nio.channels.Selector, much the same way you would use
select() in C to multiplex many connections on a single thread.
/gordon
Thanks Gordon,

I have opted for the first option since I willonly ever have at most 5
sockets at the same time - but mainly i will only be using 1. Unless you
can convince me otherwise?

Thanks
Allan
Gordon Beaton
2006-01-13 15:46:22 UTC
Permalink
Post by Allan Bruce
I have opted for the first option since I willonly ever have at most
5 sockets at the same time - but mainly i will only be using 1.
Unless you can convince me otherwise?
That's the easier solution and sounds like it's ok for this project,
but in time you'll find java.nio to be a useful skill. Your call.

BTW since nobody's pointed it out yet, your original code was hanging
in new ObjectInputStream(), not sock.getInputStream(). The
ObjectInputStream constructor is stuck waiting for the peer to create
a corresponding ObjectOutputStream and send a header.

/gordon
--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Loading...