TCP/IP network programming (Transcription note 2)--UDP

Source: Internet
Author: User
Tags htons

<title>TCP/IP network programming (Transcription Note 2) –udp</title> TCP/IP network programming (Transcription Note 2) –udptable of Contents
    • Server
    • Client
    • Connect

Source: "TCP/IP network Programming"

Transcription

TCP protocol to provide services to 10 clients, 10 server-side sockets are required in addition to listen sockets
(accept), but in UDP, both the server side and the client require only 1 sockets

UDP client does not need bind, when calling the SendTo function, if found no address will automatically assign the corresponding socket,
The assigned address remains until the end of the program

UDP clients can use Connect (sock, message, sizeof (message)), the benefits

    1. Do not create a new socket for each UDP connection
    2. You can also use the read, write function, which does not require the use of sendto, recvfrom
Server
int Server_sock;struct sockaddr_in server_addr;struct sockaddr_in client_addr;socklen_t client_size; server_sock = socket (af_inet, SOCK_DGRAM, 0); memset (&server_addr, 0,sizeof(SERVER_ADDR)); server_addr.sin_family = Af_inet;server_addr.sin_addr.s_addr = htonl (inaddr_any); server_addr.sin_port = Htons (Atoi ( ARGV[1]); Bind (Server_sock, (struct sockaddr*) &server_addr,sizeof(SERVER_ADDR));Char buf[Buf_size+1];int Str_len; while(1) {client_size =sizeof(CLIENT_ADDR); Str_len = Recvfrom (Server_sock, buf, Buf_size, 0, (struct sockaddr*) &client_addr, &client_size);if(Str_len = = 0) { Break; } sendto (Server_sock, buf, Str_len, 0, (struct sockaddr*) &client_addr, client_size);} Close (Server_sock);
Client
int Server_sock;struct sockaddr_in server_addr;socklen_t server_size; server_sock = socket (af_inet, SOCK_DGRAM, 0); memset (&server_addr, 0,sizeof(SERVER_ADDR)); server_addr.sin_family = Af_inet;server_addr.sin_port = Htons (Atoi (argv[2])); Inet_aton (argv[1], &server_ ADDR.SIN_ADDR);Char buf[Buf_size+1];int Str_len; while(1) {printf ("Send to Server (q/q to quit):"); Fgets (buf, Buf_size, stdin);if(strcmp (BUF,"Q\N") = = 0 | | strcmp (BUF,"Q\N") = = 0) { Break; } sendto (Server_sock, buf, strlen (BUF), 0, (struct sockaddr*) &server_addr,sizeof(SERVER_ADDR)); Server_size =sizeof(SERVER_ADDR); Str_len = Recvfrom (Server_sock, buf, Buf_size, 0, (struct sockaddr*) &server_addr, &server_size);        Buf[str_len] = 0; printf"from server:%s\N", buf);} Close (Server_sock);
Connect
    int Server_sock;struct sockaddr_in server_addr;socklen_t server_size;    Server_sock = socket (af_inet, SOCK_DGRAM, 0); memset (&server_addr, 0,sizeof(SERVER_ADDR));    server_addr.sin_family = af_inet;    Server_addr.sin_port = htons (Atoi (argv[2])); Inet_aton (argv[1], &server_addr.sin_addr); + + Connect (Server_sock, (struct sockaddr*) &server_addr,sizeof(SERVER_ADDR));Char buf[Buf_size+1];int Str_len; while(1) {printf ("Send to Server (q/q to quit):"); Fgets (buf, Buf_size, stdin);if(strcmp (BUF,"Q\N") = = 0 | | strcmp (BUF,"Q\N") = = 0) { Break; } sendto (Server_sock, buf, strlen (BUF), 0, (struct sockaddr*) &server_addr,sizeof(SERVER_ADDR)); Server_size =sizeof(SERVER_ADDR); Str_len = Recvfrom (Server_sock, buf, Buf_size, 0, (struct sockaddr*) &server_addr, &server_size);            Buf[str_len] = 0; printf"from server:%s\N", buf); } close (Server_sock);

TCP/IP network programming (Transcription note 2)--UDP

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.