2

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.

Andy Hu
  • 21
  • 1
  • 3
  • Please add some code, it's the best way to see where things go wrong. – Nobilis May 31 '13 at 02:59
  • 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; } } – Andy Hu May 31 '13 at 03:09
  • the error occured in send() calls – Andy Hu May 31 '13 at 03:09
  • Sorry I meant in the question (just edit it), the formatting would look better there :) – Nobilis May 31 '13 at 03:12
  • Don't put a lot of code in the comments, it doesn't look nice, just edit the original question (click on `edit` just below the tags). anyway, it's been done for you :) – Nobilis May 31 '13 at 03:21
  • thank very much, it's in the original question now. – Andy Hu May 31 '13 at 03:23
  • 1
    Please show the definition of "sockArr" – Deepu May 31 '13 at 03:23

1 Answers1

2

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.

Community
  • 1
  • 1
pilcrow
  • 56,591
  • 13
  • 94
  • 135