
Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
>> 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