Skip to content
Advertisement

Socket connection in Linux

I just got a error from connect API in linux.

I knew ‘connect’ will return zero if connection is successful, I got return value “3”

is it error code? or is there something else what don’t know?

connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));

I got value 3 from sock.

Advertisement

Answer

To get the return value of connect(), it is most straight forward to use a variable that is used as the left hand side of an assignment.

int result = connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
printf("connect returned: %dn", result);

sock must be a socket, and was assigned a file descriptor number as a result of a call to socket(). Most UNIX APIs that return a new file descriptor will return the lowest available one. A program usually starts off with descriptors 0, 1, and 2 already in use (for STDIN, STDOUT, and STDERR). So, it is not unexpected that socket() returned 3.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement