Socket perror is operation on Non-socket Solution

Source: Internet
Author: User
Tags connect socket socket error

If a problem occurs, log

If an error occurs when you call the connect socket accept function, perror ("socket"); perror ("Connect"); printf ("% s \ n", strerror (errno )); can make specific errors

The select function emphasizes again: <0: Error returned, = 0: Normal, timeout returned, that is, there is no FD readable and writable, only for non-blocking> 0: normal return, you can also call the fdisset function to check the FD;

Socket operation on Non-socket errors may occur in two situations:

1. Create a socket:

If (listenfd = socket (af_inet, sock_stream, 0) =-1 ){

Perror ("creating socket failed! ");
Exit (-1 );
}

A socket operation on Non-socket error occurs during bind.

The correct code should be:

If (listenfd = socket (af_inet, sock_stream, 0) =-1 ){
Perror ("creating socket failed! ");
Exit (-1 );
}

2. Accept:

If (connfd = accept (listenfd, (struct sockaddr *) & client_addr, (socklen_t *) & sin_size) =-1 ){
Perror ("Accept error! ");
Exit (-1 );
}

A socket operation on Non-socket error occurs during Recv.

The correct code is:

If (connfd = accept (listenfd, (struct sockaddr *) & client_addr, (socklen_t *) & sin_size) =-1 ){
Perror ("Accept error! ");
Exit (-1 );
}

The reason for the socket operation on Non-socket error is:

If (listenfd = socket (af_inet, sock_stream, 0) =-1)

If (connfd = accept (listenfd, (struct sockaddr *) & client_addr, (socklen_t *) & sin_size) =-1)

This is because () is missing in the two sentences. The assignment has the lowest priority. As a result, the value of listenfd and connfd is 0 when the creation/connection is successful, and the value of listenfd and connfd is 1 when the connection fails.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.