When programming sockets for TCP it's
important that you never assume that writes and 
reads will send or receive everything in one go.
Java seem to take care of the writes but in most other 
languages like C and Perl you have to handle partial writes
yourself within a loop.

Client reads return the number of bytes that are currently 
available. As soon as the first byte of data arrives the client
is ready to read. It may take many packets at the network
level for the entire message to be transferred or the server may
implement its writes one byte at a time. If the client
is able to read faster than the packets are arriving then the
data will be seen in chunks. If the client is slower than the
network then you may be able to read the entire packet in
one go.

Some system like Java will hide what is happening a the
network level but typically thing to remember are:
  1. If writes return the number of bytes written always check
the value and write out what is remanding.
  2. Never assume that entire packets will returned by a single
read even if during testing this is always the case.
 3. If reads can be interrupted then check the error and restart
the read.

For your problem of only reading one byte it may be a case
that the server is writing Strings one byte at atime. Not very
efficient but there nothing illegal about it. Putting your client
into a loop until all the data is available should fix your problem.

When it comes to network programming learn to
use a monitoring tool like snoop or tcpdump. Even
truss or strace may help show what is happening.

Hope this helps

Ross Mark

PS Anyone know how to do decent quoting within MS outlook?
ie '>'  around the original message.
-----Original Message-----
From:   [EMAIL PROTECTED] [SMTP:[EMAIL PROTECTED]]
Sent:   Friday, September 25, 1998 7:28 AM
To:     [EMAIL PROTECTED]
Subject:        socket problems


This problem may have nothing to do with java on linux, but
I thought i might ask and see if anyone knows what is 
happening. I am having many problems with sockets in java and I'm not
sure whether I'm missing something or if something else is
wrong.

I have a java server and a C client.

First, the server's DataOutputStream.writeBytes() method seems to be
doing something else in addition to writing bytes to client. I infer
this because the client's "read" function never returns 
more than 1 reflecting only 1 byte is being read even though
the server is sending 2 bytes.

***
         --  JAVA method called in server --
        clientsocket_out.writeBytes("OK"); 

          -- C client function called --
        n = read(sockfd, recvline, 15); (n is 1 and recvline
        is "O", but n should be 2 and recvline should be "OK")

***

If I use clientsocket_out.write(buf) where buf is a byte
array and buf.length is 2, the C "read" function works just
fine. In this case n is 2 reflecting that 2 bytes were read and
recvline contains the 2 bytes sent by the server.

Anyone have any idea why this is?

Also, no matter what I do, I can't seem to pass more than
1460 bytes to a DataOutputStream (Socket) using the write
method. Lets say I have byte buffer[]=new byte[30000] and 
I call the method clientsocket_out.write(buf), I expect that 
write will write 30000 bytes to the dos, but instead the client 
reports that only 1460 bytes have been read. Is this because of 
TCP MTU or some other limitation?

Thanks,

Mark

Reply via email to