AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Sockets problem: getInputStream hanging

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
Allan Bruce
Guest





PostPosted: Thu Jan 12, 2006 11:13 pm    Post subject: Sockets problem: getInputStream hanging Reply with quote



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;
}


Back to top
Roedy Green
Guest





PostPosted: Thu Jan 12, 2006 11:21 pm    Post subject: Re: Sockets problem: getInputStream hanging Reply with quote



On Thu, 12 Jan 2006 23:13:01 -0000, "Allan Bruce" <amb (AT) abc (DOT) net> wrote,
quoted or indirectly quoted someone who said :

Quote:
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.

Back to top
Allan Bruce
Guest





PostPosted: Fri Jan 13, 2006 1:11 am    Post subject: Re: Sockets problem: getInputStream hanging Reply with quote




"Roedy Green" <my_email_is_posted_on_my_website (AT) munged (DOT) invalid> wrote in
message news:72pds1ttfr6m5vrbjbcs3q38flkdquij3l (AT) 4ax (DOT) com...
Quote:
On Thu, 12 Jan 2006 23:13:01 -0000, "Allan Bruce" <amb (AT) abc (DOT) net> wrote,
quoted or indirectly quoted someone who said :

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 Surprised) 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



Back to top
Roedy Green
Guest





PostPosted: Fri Jan 13, 2006 2:08 am    Post subject: Re: Sockets problem: getInputStream hanging Reply with quote

On Fri, 13 Jan 2006 01:11:59 -0000, "Allan Bruce" <amb (AT) abc (DOT) net> wrote,
quoted or indirectly quoted someone who said :

Quote:
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.

Back to top
Gordon Beaton
Guest





PostPosted: Fri Jan 13, 2006 7:38 am    Post subject: Re: Sockets problem: getInputStream hanging Reply with quote

On Fri, 13 Jan 2006 01:11:59 -0000, Allan Bruce wrote:
Quote:
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

Back to top
Allan Bruce
Guest





PostPosted: Fri Jan 13, 2006 3:30 pm    Post subject: Re: Sockets problem: getInputStream hanging Reply with quote


"Gordon Beaton" <not (AT) for (DOT) email> wrote

Quote:
On Fri, 13 Jan 2006 01:11:59 -0000, Allan Bruce wrote:
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


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



Back to top
Gordon Beaton
Guest





PostPosted: Fri Jan 13, 2006 3:46 pm    Post subject: Re: Sockets problem: getInputStream hanging Reply with quote

On Fri, 13 Jan 2006 15:30:51 -0000, Allan Bruce wrote:
Quote:
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

Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.