From: grahn+nntp@snipabacken.se   
      
   On Fri, 2011-07-29, Barry Margolin wrote:   
   > In article   
   > <38bd2f79-5433-4674-832a-9c2d7f0a091b@x19g2000prc.googlegroups.com>,   
   > Pandurangan R S wrote:   
   >   
   >> On Jul 27, 1:33 pm, Jorgen Grahn wrote:   
   >> > I got this question at work, and was a bit annoyed that I couldn't   
   >> > easily find a straight answer in the man pages or in Stevens.   
   >> >   
   >> > Under what conditions could write(sock, buf, n), with n>0, return zero   
   >> > for a TCP socket? According to POSIX? In Linux?   
   >> >   
   >> > The return values I'm comfortable with are:   
   >> > n - all got written   
   >> > 1..n-1 - partial write   
   >> > -1 - fatal error, EINTR or EWOULDBLOCK   
   >> >   
   >> > That doesn't seem to leave room for a distinct meaning of 0.   
   >> >   
   >> > This supposedly happened to the guy who asked me, on Linux. I'm not   
   >> > sure if the socket was set blocking or nonblocking.   
   >> >   
   >> > /Jorgen   
   >> >   
   >> > --   
   >> > // Jorgen Grahn > > \X/ snipabacken.se> O o .   
   >>   
   >> Return value of zero means nothing was written to the socket (socket   
   >> send buffer is full, that it could not accommodate even a single   
   >> byte). This is not a serious error, just that you should attempt again   
   >> to write to the socket at a later point in time. In a sense, this is   
   >> similar to partial write 1..n-1 bytes, but may be we cannot call this   
   >> a partial write, since nothing was actually written. May be we should   
   >> call it incomplete write in general instead of partial write if we   
   >> want to be picky on the terms :)   
   >>   
   >> I believe the man page states this quite clearly   
   >>   
   >> "On success, the number of bytes written is returned (zero indicates   
   >> nothing was written). On error, -1 is returned, and errno is set   
   >> appropriately."   
      
   Right, that's the Linux write(2) man page. But it covers more uses   
   than TCP sockets, and like Barry Magolin below, I have a hard time   
   seeing what socket cases aren't already covered by the two classics:   
   block or say EWOULDBLOCK.   
      
   > How can this happen?   
   >   
   > If the socket is in blocking mode and the buffer is full, the write   
   > should block until it's able to write at least one byte, then return   
   > 1..n.   
   >   
   > If the socket is in non-blocking mode and the buffer is full, it should   
   > return -1 with errno = EWOULDBLOCK.   
   >   
   > What other cases are there?   
      
   write(sock, buf, 0) is one trivial such case, but that's not what I'm   
   after, I think,   
      
   /Jorgen   
      
   --   
    // Jorgen Grahn O o .   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|