Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2006

Tip: Looking for answers? Try searching our database.

Sockets problem: getInputStream hanging

Thread view: 
Allan Bruce - 12 Jan 2006 23:13 GMT
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 - 12 Jan 2006 23:21 GMT
>           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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Allan Bruce - 13 Jan 2006 01:11 GMT
>>           mOutput = new ObjectOutputStream( mSocket.getOutputStream());
>>            mOutput.flush();
[quoted text clipped - 5 lines]
> See http://mindprod.com/applets/fileio.html
> for sample code.

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 - 13 Jan 2006 02:08 GMT
>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
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Gordon Beaton - 13 Jan 2006 07:38 GMT
> 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

Signature

[  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 - 13 Jan 2006 15:30 GMT
>> 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
[quoted text clipped - 11 lines]
>
> /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 - 13 Jan 2006 15:46 GMT
> 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

Signature

[  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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.