Jeff Verdegan wrote:All you need for two-way communication is one socket, which implies one port. (You can't have two sockets using the same port.)
If you want both sides to be sending concurrently, independently of each other, as opposed to request, response, request, response, then you'll need two threads.
Okay, I've written some code that takes two bytes on the command line, opens a socket with another machine, and sends the two bytes across the socket. The actual code is:
On the other machine I have some code that reads in the two bytes and multiplies them:
I go to machine 10.72.77.49 and type in:
java SendTwo 13267 3 7
and then I got to my laptop and type in:
java Multiply 13267 10.72.77.49
and I get output on my laptop:
3 times 7 is 21.
and on machine 10.72.77.49 I get output:
Wrote values 3 and 7 to the socket.
My question is, how do I use my <stSocket> in program <Multiply> on 10.72.77.49 to send the product back to the <stSocket> in program <SendTwo> so it can print it out?
Obviously this is an extremely simple process, just multiplying two numbers, but I kept it simple to make the problem simple, so I could understand how I can establish two-way communication with one socket.
Is it simply a matter of replacing the comment in <Multiply> with a declaration of an <OutputStream> object and setting it equal to <stSocket.getOutputStream()> like I did in <SendTwo>, and then doing a <write( product)> on that object? And then replacing the comment in <SendTwo> with a declaration of an <InputStream> object and setting it equal to <stSocket.getInputStream()> like I did in <Multiply> and then doing a <read()> on that object? If so, do I have to close the previously declared <iStream> and <oStream> objects before I create the new <OutputStream> and <InputStream> objects?
If it isn't this simple, how
can I get two-way communication with this simple example?
Kevin Simonson