Andy Hu
Andy Hu

Reputation: 21

linux c socket error: Input/output error

I've got a "Input/output error" error when I try to send data to a tcp server. What does this mean in terms of sockets? Its basically the same code I was used always worked fine. I was hoping someone could tell me what are the reasons of inpput/output error when I tried to send over a socket and how I could check/fix them. Any help is appreciated.

struct SOCKETSTUS {
int sendSockFd;
int recvSockFd;
short status;
long heartBeatSendTime;
long heartBeatRecTime;
long loginPackSendTime;
};

struct SOCKETSTUS sockArr[128];

if (tv.tv_sec - sockArr[i].heartBeatSendTime >= beatTim) 
{ 
   if (send(sockArr[i].sendSockFd, szBuffer, packetSize, 0) != packetSize) 
   { 
     fprintf(stderr, "Heartbeat package send failed:[%d][%s]\n", errno, strerror(errno)); 
     if (errno == EBADF || errno == ECONNRESET || errno == ENOTCONN || errno == EPIPE) 
     { 
       Debug("link lose connection\n"); Reconn(i); continue; 
     } 
   } 
   else 
   { 
     sockArr[i].heartBeatSendTime = tv.tv_sec; 
     if (sockArr[i].status == SOCK_IN_FLY)      
         sockArr[i].heartBeatRecTime = tv.tv_sec; 
   } 
}

The error occured in send() calls.

Upvotes: 2

Views: 3358

Answers (1)

pilcrow
pilcrow

Reputation: 58544

Your error check is incorrect. send() returns the number of bytes sent or -1 on error. You check only that the return value equals packetSize, not that the return value indicates error. Sometimes send() on a stream socket will return fewer bytes than requested.

So, some previous syscall (perhaps a harmlessly failed tty manipulation? a dodgy signal handler?) set errno to EIO.

Change your code to treat -1 different from a "short" send.

Upvotes: 2

Related Questions