TCP Runtime

I have a TCP server and a client written in C that opens a connection that should be open forever. Errno returns 0 when first sent, which is great. On subsequent transfers, it gives me errno 60, which is that the operation is complete. The package is still being received by the server, although nothing seems to be wrong. What can cause errno to be installed? Is it better to just ignore this error as it works correctly?

+2


source to share


3 answers


Random guess: errno

Set only when the function fails. If the function is not interrupted, it errno

remains as is. It is not set to 0. The following is incorrect:

send( โ€ฆ );
if (errno) {
    perror("send");
}

      

Instead, you should check the sending result:

ssize_t res = send( โ€ฆ );
if (-1 == res) {
    perror("send")
}

      

You can confirm this with the following program:



#include <stdio.h>
#include <unistd.h>

int main() {
    char buf[16];
    ssize_t res;

    read(999, buf, sizeof(buf));
    perror("read");

    write(1, "hello world\n", 12);
    perror("write");
}

      

which outputs:

read: Bad file descriptor
hello world
write: Bad file descriptor

      

(Note: if STDOUT is fd 1)

+3


source


As mentioned by others, you need to check the return values โ€‹โ€‹of Unix system functions ... all of them! ... to interpret the values โ€‹โ€‹in a errno

bulletproof way. I refer you to Canon Clause 6, Henry Spencer Ten Commandments for C Programmers :



If a function is declared to return an error code in case of difficulty, you should check that code, although checks do check the size of your code and cause pain in your thumbnails, because if you think "this can't happen to me" the gods will surely punish you for your arrogance.

+2


source


I think you are only checking the errno variable, but not returning the value of send ().

You should check the errno variable after the function return value indicating that the errno function was set by the function. If the function does not report the error errno, the variable errno has no meaning.

0


source







All Articles