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.

7
  • Please add some code, it's the best way to see where things go wrong. Commented May 31, 2013 at 2: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; } } Commented May 31, 2013 at 3:09
  • the error occured in send() calls Commented May 31, 2013 at 3:09
  • Sorry I meant in the question (just edit it), the formatting would look better there :) Commented May 31, 2013 at 3:12
  • 1
    Please show the definition of "sockArr" Commented May 31, 2013 at 3:23

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.