Implementation Process of UDPClient C Language

Source: Internet
Author: User

The UDPClient program is a common program. In many language tutorials, there are application explanations for anger. Now we will explain how to use the C language to implement UDPClient. Now let's take a closer look at the specific steps for writing the UDPClient program.

1) initialize the variables in the sockaddr_in structure and assign values. Here, "8888" is used as the port to connect to the service program, read the IP address from the command line parameters, and determine whether the IP address meets the requirements.

2) Use socket) to create a UDP socket. The second parameter is SOCK_DGRAM.

3) connect to the service program. Unlike TCP, UDP connect does not shake hands with the service program three times. As mentioned above, UDP is non-connected and can actually be connected. Using the connected UDP, the kernel can directly return the error message to the user program, so as to avoid calling recvfrom because the data is not received. It seems that the customer program does not respond.

4) send data to the service program. Because UDP is connected, write is used to replace sendto ). The data here reads the user input directly from the standard input.

5) receive the data sent back by the service program, and also use read) to replace recvfrom ).

6) process the received data, which is directly output to the standard output.

Udpclient. c program content:

 
 
  1. #include  
  2. #include  
  3. #include  
  4. #include  
  5. #include  
  6. #include  
  7. #include  
  8. #include  
  9. #define MAXLINE 80  
  10. #define SERV_PORT 8888  
  11. void do_cliFILE *fp, int sockfd, struct sockaddr *pservaddr, socklen_t servlen)  
  12. {  
  13. int n;  
  14. char sendline[MAXLINE], recvline[MAXLINE + 1];  
  15. /* connect to server */ 
  16. ifconnectsockfd, struct sockaddr *)pservaddr, servlen) == -1)  
  17. {  
  18. perror"connect error");  
  19. exit1);  
  20. }  
  21. whilefgetssendline, MAXLINE, fp) != NULL)  
  22. {  
  23. /* read a line and send to server */ 
  24. writesockfd, sendline, strlensendline));  
  25. /* receive data from server */ 
  26. n = readsockfd, recvline, MAXLINE);  
  27. ifn == -1)  
  28. {  
  29. perror"read error");  
  30. exit1);  
  31. }  
  32. recvline[n] = 0; /* terminate string */ 
  33. fputsrecvline, stdout);  
  34. }  
  35. }  
  36. int mainint argc, char **argv)  
  37. {  
  38. int sockfd;  
  39. struct sockaddr_in srvaddr;  
  40. /* check args */ 
  41. ifargc != 2)  
  42. {  
  43. printf"usage: udpclient \n");  
  44. exit1);  
  45. }  
  46. /* init servaddr */ 
  47. bzero&servaddr, sizeofservaddr));  
  48. servaddr.sin_family = AF_INET;  
  49. servaddr.sin_port = htonsSERV_PORT);  
  50. ifinet_ptonAF_INET, argv[1], &servaddr.sin_addr) <= 0)  
  51. {  
  52. printf"[%s] is not a valid IPaddress\n", argv[1]);  
  53. exit1);  
  54. }  
  55. sockfd = socketAF_INET, SOCK_DGRAM, 0);  
  56. do_clistdin, sockfd, struct sockaddr *)&servaddr, sizeofservaddr));  
  57. 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.