Network IPC, socket (3): establish a connection

Source: Internet
Author: User
Tags htons

1. Prerequisites:

If you are dealing with connection-oriented network services (sock_stream or sock_seqpacket), you need to establish a connection between the client and the server before starting data transmission.

 

2. Function introduction:

Use connect to establish a connection:

  • Header file: <sys/socket. h>
  • Prototype: int connect (INT sockfd, const struct sockaddr * ADDR, socklen_t Len );
  • Return Value: 0 is returned if the request is successful, and-1 is returned if an error occurs.
  • Note: the address specified in connect is the server address for communication.

 

Listen with listen (connection requests can be accepted ):

  • Header file: <sys/socket. h>
  • Prototype: int listen (INT sockfd, int backlog );
  • Return Value: 0 is returned if the request is successful, and-1 is returned if an error occurs.
  • Parameter: backlog provides a prompt indicating the number of connection requests that the process can wait. the actual upper limit is determined by the system and protocol. once the queue is full, the system will reject redundant connection requests. Therefore, this value should be selected based on the expected server load and processing capability.

 

After the server calls listen, the socket can receive connection requests.

Use accept to obtain the connection request and establish a connection:

  • Header file: <sys/socket. h>
  • Prototype: int accept (INT sockfd, struct sockaddr * restrict ADDR, socklen_t * restrict Len );
  • Returned value: if the request succeeds, the socket descriptor is returned. If an error occurs, the value-1 is returned.
  • Parameter: if you do not care about the client ID, set ADDR and Len to null. Otherwise, make sure that the buffer zone of the ADDR parameter has enough space to store the address before you call accept, set Len to the corresponding size.

The returned socket has the same socket type and address family as the original socket (sockfd parameter), but is not associated. The original socket continues to be available and accepts other connection requests.

If no connection request exists, accept will be blocked. If sockfd is in non-blocking mode, accept will return-1 and set errno to eagain or ewouldblock.

In addition, the server can use poll or select to wait for a request to arrive, which will be introduced in future articles.

 

3. Example:

Client Connection Program:

# Include <sys/types. h>
# Include <sys/socket. h>
# Include <netinet/in. h>
# Include <ARPA/inet. h>
# Define Port 1234
# Define server_ip "127.0.0.1"

Int main ()
...{
Int S;
Struct sockaddr_in ADDR;
Char buffer [256];
If (S = socket (af_inet, sock_stream, 0) <0 )...{
Perror ("socket ");
Exit (1 );
}

/** // * Fill in the sockaddr_in structure */
Bzero (& ADDR, sizeof (ADDR ));
ADDR. sin_family = af_inet;
ADDR. sin_port = htons (port );
ADDR. sin_addr.s_addr = inet_addr (server_ip );

/** // * Try to connect */
If (connect (S, & ADDR, sizeof (ADDR) <0 )...{
Perror ("Connect ");
Exit (1 );
}
Return 0;
}

 

Server listener:

# Include <sys/socket. h>
# Include <netinet/in. h>
# Include <ARPA/inet. h>
# Define Port 1234
# Define maxsockfd 10

Int main ()
...{
Int sockfd, newsockfd;
Struct sockaddr_in ADDR;
Int addr_len = sizeof (struct sockaddr_in );

/** // * Create a socket */
If (sockfd = socket (af_inet, sock_stream, 0) <0 )...{
Perror ("socket ");
Exit (1 );
}

/** // * Fill in ADDR */
Bzero (& ADDR, sizeof (ADDR ));
ADDR. sin_family = af_inet;
ADDR. sin_port = htons (port );
ADDR. sin_addr.s_addr = htonl (inaddr_any );

/** // * Bind */
If (BIND (sockfd, & ADDR, sizeof (ADDR) <0 )...{
Perror ("Connect ");
Exit (1 );
}

/** // * Listener */
If (Listen (sockfd, 3) <0 )...{
Perror ("Listen ");
Exit (1 );
}

/** // * Accept the request */
If (newsockfd = accept (sockfd, null, null) <0 )...{
Perror ("accept ");
Exit (1 );
}

Return 0;
}

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.