socket communication problems

Jim Frost madd at bu-cs.BU.EDU
Sat Sep 23 06:05:18 AEST 1989


In article <16453 at watdragon.waterloo.edu> sekoppenhoef at rose.waterloo.edu writes:
|Shouldn't I be able to read and write on
|BOTH ends of a socket connection? [...]
|What am i doing wrong?
[...]
|**These two commands are what I'm using to write and read... the 'write' works
|**on the client end but not the read, and vice versa for the server end
|the server end uses an ACCEPT to get the scket name for communication of course.	
|	write(sock, buf, n) 
|	read(sock,buf,sizeof(buf) ) 

This will fix your problem:

	for (i= 0; i < sizeof(buf);)
	  i += read(sock, buf + i, sizeof(buf) - i);

How fondly I remember having this same problem, asking Barry Shein
about it, and hearing "did you check the return value from read?" and
feeling very silly.  You'll find that socket reads don't read the
whole thing much of the time.

Note that you should really do error checking in that loop, too.

jim frost
software tool & die
madd at std.com



More information about the Comp.unix.wizards mailing list