Instructions on 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 code snippet of the Linux Accept function is as follows:
 

 
 
  1. struct sockaddr addrc;  
  2. int fdc;  
  3. socklen_t len=sizeof(struct sockaddr_in);  
  4. fdc=accept(fds,(struct sockaddr*)&addrc,&len);  
  5. if(fdc==-1)  
  6. {  
  7. fprintf(stderr,"Accept error:%s\n",strerror(errno));  
  8. switch(errno)  
  9. {  
  10. case EBADF:  
  11. printf("EBADF\n");  
  12. break;  
  13. case EFAULT:  
  14. printf("EFAULT\n");  
  15. break;  
  16. case ENOTSOCK:  
  17. printf("ENOTSOCK\n");  
  18. break;  
  19. case EOPNOTSUPP:  
  20. printf("EOPNOTUPP\n");  
  21. break;  
  22. case EPERM:  
  23. case ENOBUFS:  
  24. case ENOMEM:  
  25. printf("THis\n");  
  26. case EINVAL:  
  27. printf("EINVAL\n");  
  28. break;  
  29. default:  
  30. printf("Other\n");  
  31. }  
  32. exit(1);  

The time when the Linux Accept function runs will report Accept error: Invalid argument
EINVAL
According to my personal guess, it should be caused by the third parameter. The first parameter of accept should be okay. The first parameter output through the function is a positive value, and the previous bind and other functions have been used. No problem.
Thank you for your help.

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.