> ok so should i change that to != -1
No. sin.read() reads the first byte of the stream. You then test for EOF
and discard the character, which is why you're missing one.
Aside from that, however, your code badly mixes bytes and characters. If
you move to different platforms or places with different encodings this can
cause you problems. If you're providing both the sender and receiver there
are much easier ways to do this, particularly for characters. All that
line-reading business is taken care of for you, as well as ensuring that the
characters on both sides match.
BufferedReader br = new BufferedReader (
new InputStreamReader (sin, "UTF-8"));
String s = br.readline ()
while (s != null) {
// Process the line
s = br.readline ()
}
The sender simply has to make a corresponding PrintWriter
PrintWriter pw = new PrintWriter (
new OutputStreamWriter (outstream, "UTF-8"),
true /* autoflush */);
pw.println ("Here is the message");
Then you can just send text strings back and forth. A common technique is
for the client to send to the server, who reads a line and then sends a line
in reply. That is, the client goes write-read and the server goes
read-write.
Cheers,
Matt Humphrey matth@ivizNOSPAM.com http://www.iviz.com/
sconeek@gmail.com - 06 Jun 2006 03:32 GMT
i have tried to implement your approach, but its not working for me. i
am unable to even read any messages.
and my old approach picked up the first characters in some messages and
in some it missed, so its working but why is it working only on a few
and not others. will keep on trying in the meantime.
sconeek@gmail.com - 06 Jun 2006 03:38 GMT
sorry mate, modified my approach and it works well now. thanks a lot
for your help.