The method of waiting for socket connection and locating socket in C language _c language

Source: Internet
Author: User
Tags error code

C language Listen () function: Waiting for connection
header file:

#include <sys/socket.h>

To define a function:

int listen (int s, int backlog);

Function Description: Listen () is used to wait for the socket connection of the parameter S. The parameter backlog specifies the maximum connection requirements that can be processed at the same time, and if the number of connections reaches this limit, the client side will receive a econnrefused error. Listen () does not begin to receive the line, just set the socket to Listen mode, the real receiving client side line is accept (). Typically listen () is called after the socket (), bind (), and then the Accept () is invoked.

Return value: Success returns 0, failure returns-1, error reason is in errno

Additional Note: Listen () only applies to sock_stream or sock_seqpacket socket types. If the socket is af_inet, the parameter backlog maximum can be set to 128.

Error code:
EBADF parameter SOCKFD non-legitimate socket processing code
eaccess Insufficient Permissions
The socket specified by Eopnotsupp does not support listen mode.

C language Bind () function: Locating the socket
header file:

#include <sys/types.h>  #include <sys/socket.h>

Definition function: int bind (int sockfd, struct sockaddr * my_addr, int addrlen);

Function Description: Bind () is used to set a name for the socket of the parameter sockfd. This name is directed to a SOCKADDR structure by the parameter my_addr, and a common data structure is defined for different socket domain

struct sockaddr
{
  unsigned short int sa_family;
  Char sa_data[14];

1. sa_family is the domain parameter when calling socket (), that is, the af_xxxx value.
2, Sa_data use up to 14 characters length.

This SOCKADDR structure will have different structure definitions for use of different socket domain, for example, using Af_inet domain, whose socketaddr structure definition is

struct socketaddr_in
{
  unsigned short int sin_family;
  uint16_t Sin_port;
  struct IN_ADDR sin_addr;
  unsigned char sin_zero[8];

struct in_addr
{
  uint32_t s_addr;
};

1, sin_family is sa_family
2, Sin_port for the use of the port number
3, Sin_addr. S_ADDR is not used for IP address Sin_zero.
The parameter addrlen is the structure length of the sockaddr.

Return value: Success returns 0, failure returns 1, and the reason for the error is in errno.

Error code:
1, EBADF parameter SOCKFD non-legal socket processing code.
2, eaccess Insufficient authority
3, Enotsock parameter sockfd is a document descriptive word, not socket.

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.