Beej's Guide Network to Programming series serialization 13

Source: Internet
Author: User

5.6. accept () --- "Thank you for calling port 3490"
Ready, the system calls accept () will be a bit odd! As you can imagine, someone connects (connect () to your machine from a long distance through a port you are listening on (listen. Its connection will be added to the queue waiting for acceptance (accept. You call accept () to tell it that you have idle connections. It will return a new socket file descriptor! In this way, you have two sockets. The original one is listening to your port, and the new one is preparing to send () and receive (recv () data. This is the process!

Function prototype:

# Include <sys/types. h>

# Include <sys/socket. h>

 

Int accept (intsockfd, struct sockaddr * addr, socklen_t * addrlen );

 

Sockfd is quite simple. It is the same socket descriptor as listen. Addr is a pointer to the local data structure sockaddr_storage. This is where the information to be accessed is going (you can determine the address that calls you on that port ). Before its address is passed to accept, addrlen is a local integer variable and is set to sizeof (struct sockaddr_storage ). Accept will not give additional bytes to addr. If you put less, it will be reflected by changing the value of addrlen.

Similarly, if an error occurs,-1 is returned and the global error variable errno is set.

The following is an example:

# Include <string. h>

# Include <sys/types. h>

# Include <sys/socket. h>

# Include <netinet/in. h>

 

# Define MYPORT "3490" // the port users will be ing

# Define BACKLOG 10 // how many pending connectionsqueue will hold

 

Int main (void)

{

Struct sockaddr_storage their_addr;

Socklen_t addr_size;

Struct addrinfo hints, * res;

Int sockfd, new_fd;

 

//!! Don't forget your error checkingfor these CILS !!

 

// First, load up address structs withgetaddrinfo ():

 

Memset (& hints, 0, sizeof (hints ));

Hints. ai_family = AF_UNSPEC;

Hints. ai_socktype = SOCK_STREAM;

Hints. ai_flags = AI_PASSIVE; // fill in my IP for me

 

Getaddrinfo (NULL, MYPORT, & hints, & res );

 

// Make a socket, bind it, and listen onit:

Sockfd = socket (res-> ai_family, res-> ai_socktype, res-> ai_protocol );

 

Bind (sockfd, res-> ai_addr, res-> ai_addrlen );

 

Listen (sockfd, BACKLOG );

 

// Now accept an incoming connection:

 

Addr_size = sizeof (their_addr );

New_fd = accept (sockfd, (struct sockaddr *) & their_addr, & addr_size );

 

// Ready to communicate on socketdescriptor new_fd!

.

.Www.2cto.com

.

}

Note that you should use the new socket descriptor new_fd in system call send () and recv. If you only want a connection, you can use close () to close the original file descriptor sockfd to avoid more connections on the same port.


From the column xiaobin_HLJ80

Related Article

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.