A Brief Introduction to the Linux Accept Function

Source: Internet
Author: User

The system calls accept (), which is 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 of the Linux Accept function!

The Linux Accept function is defined as follows:
# Include <sys/socket. h>
Intaccept (intsockfd, void * addr, int * addrlen );

Sockfd is quite simple. It is the same socket descriptor as listen. Addr is a pointer to the local data structure sockaddr_in. This is where the access information is required. 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 (structsockaddr_in ). 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.
This is a code snippet you should be familiar.
 

 
 
  1. # Include <string. h>
  2. # Include <sys/socket. h>
  3. # Include <sys/types. h>
  4. # DefineMYPORT3490/* User Access Port */
  5. # DefineBACKLOG10/* Number of pending connections */
  6. Main ()
  7. {
  8. Intsockfd, new_fd;/* listenonsock_fd, newconnectionnew_fd */
  9. Structsockaddr_inmy_addr;/* address information */
  10. Structsockaddr_intheir_addr;/* connector 'saddressinformation */
  11. Intsin_size;
  12. Sockfd = socket (AF_INET, SOCK_STREAM, 0);/* error check */
  13. My_addr.sin_family = AF_INET;/* hostbyteorder */
  14. My_addr.sin_port = htons (MYPORT);/* short, networkbyteorder */
  15. My_addr.sin_addr.s_addr = INADDR_ANY;/* auto-fillwithmyIP */
  16. Bzero (& (my_addr.sin_zero),;/* zerotherestofthestruct */
  17. /* Don 'tforgetyourerrorcheckingforthesecils :*/
  18. Bind (sockfd, (structsockaddr *) & my_addr, sizeof (structsockaddr ));
  19. Listen (sockfd, BACKLOG );
  20. Sin_size = sizeof (structsockaddr_in );
  21. New_fd = accept (sockfd, & their_addr, & sin_size );
  22. .
  23. .
  24. .

Note: The new socket descriptor new_fd should be used 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.

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.