<title>TCP/IP network programming (Transcription Note 2) –udp</title> TCP/IP network programming (Transcription Note 2) –udptable of Contents
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
- Do not create a new socket for each UDP connection
- 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