I'm trying to understand how sockets work, and having trouble. My code on my server (machine 10.72.77.49) is:
and my code on my client is:
Regardless of what I'm doing on my server, on my client I always execute "java RcvStrng 2". If first I execute on my server "java SndStrng AB DE" I get output:
stOs.write( { 0, 65, 0, 66, 0, 0 }, 0, 5);
stOs.write( { 0, 68, 0, 69, 0, 0 }, 0, 5);
and on my client I get:
inStream.read( { 0, 65, 0, 66, 0, 0 }, 0, 6) == 5.
Read in: "AB".
inStream.read( { 0, 68, 0, 69, 0, 0 }, 0, 6) == 5.
Read in: "DE".
Took 140 ms.
This is what I want, because my goal is to enter two strings on a command line on my server, disassemble it into individual <byte>s, send it over the socket, and reassemble it into the original pair of strings on the client.
Now when on the server I execute "java SndStrng ABC DE" I get output:
stOs.write( { 0, 65, 0, 66, 0, 67 }, 0, 6);
stOs.write( { 0, 65, 0, 66, 0, 67 }, 0, 1);
stOs.write( { 0, 68, 0, 69, 0, 67 }, 0, 5);
which again is what I want; the single byte sent on the second line is just a signal to my client that the
string started with the first write has ended.
But then the output I get on my client is:
inStream.read( { 0, 65, 0, 66, 0, 67 }, 0, 6) == 6.
inStream.read( { 0, 0, 68, 0, 69, 0 }, 0, 6) == 6.
Took 240 ms.
So the single byte my server sends to signal the end of string "ABC" is getting added into the first string. If my server can call <stOs.write()> on a <char> array and specify five significant bytes being sent, and my client can read that information and interpret it correctly, then when my server calls <stOs.write()> on a <char> array and specifies
one significant byte being sent, why doesn't the <inStream.read()> on my client return a
one? Why the different behavior if
five bytes are being sent across from the behavior when
one byte is being sent across?
Kevin Simonson