Socket question... does this happen...

Socket question... does this happen...

Post by Godfrey Dega » Tue, 29 Oct 1996 04:00:00



Hello.  I have a client/server application that uses sockets.  I've
run into a problem of the server read()'ing a socket and*.

After toying with the code a bit, I think I found out what's wrong:

Here's the problematic server code, in much shorter form:

for( i = 0; i < 3; i++)
  {
  read(...);
  do_Something();
  }

Here's the client code:
write(...);
write(...);
write(...);

The above code freezes the client and server, with the server*
on the second read.

Now if I change the client code to:

write(...);
sleep(2);
write(...);
sleep(2);
write(...);
sleep(2);

Everything is fine!  Why is that?  I'm just writing short strings
(less than 30-50 chars) with each write.  My understanding (perhaps
faulty) is that a write() by a client before the server is ready to
read() results in the information being held in a buffer?

If you can help me figure out what's going on, I'd greatly appreciate
it.  Thanks!

-Godfrey Degamo,

 
 
 

Socket question... does this happen...

Post by Bruce R. Willia » Wed, 30 Oct 1996 04:00:00



>Here's the problematic server code, in much shorter form:

>for( i = 0; i < 3; i++)
>  {
>  read(...);
>  do_Something();
>  }

>Here's the client code:
>write(...);
>write(...);
>write(...);

>The above code freezes the client and server, with the server*
>on the second read.

>Now if I change the client code to:

>write(...);
>sleep(2);
>write(...);
>sleep(2);
>write(...);
>sleep(2);

  Yes, you are right that the TCP/IP stack will buffer until the
server is ready to read.  What you are not aware of is that there is
no guarantee that each individual packet sent will arrive as the same
individual packet!  The stacks are free to combine them it two arrive
before the server program reads.

  You should also be prepared to receive packets which are broken up
into smaller parts--the client's stack may decide it only has room for
50 bytes at the moment when you told it you have 100 bytes to send...
your client then has to post the remaining 50 bytes with another call.

  Have fun!

-Bruce

----

Software Engineer, Project Manager
ROI Systems, Inc.                 801-255-1777 x106, or Fax: 255-9699

 
 
 

Socket question... does this happen...

Post by Andrew Gier » Wed, 30 Oct 1996 04:00:00


 Godfrey> Hello.  I have a client/server application that uses
 Godfrey> sockets.  I've run into a problem of the server read()'ing a
 Godfrey> socket and*.

 Godfrey> After toying with the code a bit, I think I found out what's
 Godfrey> wrong:

 Godfrey> Here's the problematic server code, in much shorter form:

 Godfrey> for( i = 0; i < 3; i++)
 Godfrey>   {
 Godfrey>   read(...);
 Godfrey>   do_Something();
 Godfrey>   }

 Godfrey> Here's the client code:
 Godfrey> write(...);
 Godfrey> write(...);
 Godfrey> write(...);

 Godfrey> The above code freezes the client and server, with the
 Godfrey> server* on the second read.

 Godfrey> Now if I change the client code to:

 Godfrey> write(...);
 Godfrey> sleep(2);
 Godfrey> write(...);
 Godfrey> sleep(2);
 Godfrey> write(...);
 Godfrey> sleep(2);

 Godfrey> Everything is fine!  Why is that?  I'm just writing short
 Godfrey> strings (less than 30-50 chars) with each write.  My
 Godfrey> understanding (perhaps faulty) is that a write() by a client
 Godfrey> before the server is ready to read() results in the
 Godfrey> information being held in a buffer?

You don't say, but I assume you're using stream sockets.

The problem is that the boundaries of the writes are lost; there is no
1-1 relationship between writes and reads. You will probably find that
all your data is being read in the first read() call.

ObFAQ:
You might want to take a look at the unix-socket-faq, regularly posted
here (and in news.answers & comp.answers), and also available at:
  http://www.veryComputer.com/
  http://www.veryComputer.com/~vic/sock-faq
  ftp://rtfm.mit.edu/pub/usenet/news.answers/unix-faq/socket

--

"Ceterum censeo Microsoftam delendam esse" - Alain Knaff in nanam

 
 
 

Socket question... does this happen...

Post by Andrew Gabri » Thu, 31 Oct 1996 04:00:00




Quote:>My understanding (perhaps
>faulty) is that a write() by a client before the server is ready to
>read() results in the information being held in a buffer?

Spot on - *a* buffer, and not *3* buffers (assuming you're using
a byte-stream protocol, e.g. TCP).

Boundaries between write() data are not known and not preserved.
The first read() will probably get the data from all 3 write()s,
leaving no data to read on the second/third read()s, hence you
block.

--