UDP Simple Introduction
There are two kinds of protocol models used in transport layer, one is TCP protocol and the other is UDP protocol. The TCP protocol dominates the network communication, and the vast majority of network communication uses the TCP protocol to complete the data transmission. But UDP is also an indispensable communication means in network communication.
The form of UDP communication is more like texting than TCP. You do not need to establish or maintain a connection before data transfer. Just concentrate on getting the data. The process of three handshake is omitted, the communication speed can be greatly improved, but the stability and accuracy of the communication with it is not guaranteed. Therefore, we call UDP "non-connected unreliable message delivery".
So what are the advantages and disadvantages of UDP compared to what we know about TCP? Because there is no need to create a connection, the UDP cost is small, the data transfer speed is fast, the real time is strong. More for the real-time requirements of high-demand communication occasions, such as video conferencing, conference calls and so on. But with the data transmission is unreliable, the accuracy of transmission data, transmission sequence and traffic are not controlled and guaranteed. So, usually, the use of UDP protocol for data transmission, in order to ensure the correctness of the information, we need to add a secondary verification protocol in the application layer to compensate for the lack of UDP, in order to achieve the purpose of reliable data transmission.
As shown, the simple UDP CS model communication process, because the UDP does not need to maintain the connection, the program logic is a lot simpler, but the UDP protocol is not reliable, guarantees the communication reliability the mechanism needs to implement in the application layer.
Code implementation:
Server.c
1#include <stdio.h>2#include <unistd.h>3#include <stdlib.h>4#include <sys/socket.h>5#include <string.h>6#include <arpa/inet.h>7#include <ctype.h>8 9 #defineServer_port 8000Ten One intMainintAGRC,Char*argv[]) A { - intsockfd; - structsockaddr_in servaddr,clieaddr; the socklen_t Clieaddr_len; - CharBuf[bufsiz]; - CharStr[inet_addrstrlen]; - intI,n; + -Sockfd=socket (Af_inet,sock_dgram,0); +Bzero (&SERVADDR,sizeof(SERVADDR)); Aservaddr.sin_family=af_inet; atServaddr.sin_addr.s_addr=htonl (inaddr_any); -servaddr.sin_port=htons (server_port); - -Bind (SOCKFD, (structsockaddr*) &servaddr,sizeof(SERVADDR)); - -printf"acceptint Connections ... \ n"); in while(1) - { toclieaddr_len=sizeof(CLIEADDR); +N=recvfrom (Sockfd,buf,bufsiz,0,(structsockaddr*) &clieaddr,&Clieaddr_len); - if(n==-1) the { *Perror ("recvfrom Error"); $ }Panax Notoginsengprintf"received from%s at PORT%d\n", Inet_ntop (AF_INET,&CLIEADDR.SIN_ADDR,STR,sizeof(str)), Ntohs (Clieaddr.sin_port)); - for(i=0; i<n;i++) the { +buf[i]=ToUpper (Buf[i]); A } theN=sendto (Sockfd,buf,n,0,(structsockaddr*) &clieaddr,sizeof(CLIEADDR)); + if(n==-1) - { $Perror ("sendto Error"); $ } - } - Close (SOCKFD); the - return 0;Wuyi}
Client.c
1#include <stdio.h>2#include <string.h>3#include <unistd.h>4#include <arpa/inet.h>5#include <ctype.h>6 7 #defineServer_port 80008 9 intMainintargcChar*argv[])Ten { One structsockaddr_in servaddr; A intSockfd,n; - CharBuf[bufsiz]; - theSOCKFD = socket (Af_inet,sock_dgram,0); - -Bzero (&SERVADDR,sizeof(SERVADDR)); -servaddr.sin_family =af_inet; +Inet_pton (Af_inet,"0.0.0.0",&servaddr.sin_addr); -Servaddr.sin_port =htons (server_port); + ABind (SOCKFD, (structsockaddr*) &servaddr,sizeof(SERVADDR)); at - while(Fgets (buf,bufsiz,stdin)! =NULL) - { -n = sendto (Sockfd,buf,strlen (BUF),0,(structsockaddr*) &servaddr,sizeof(SERVADDR)); - if(n = =-1) - { inPerror ("sendto Error"); - //printf ("*********************\n"); to } +n = recvfrom (Sockfd,buf,bufsiz,0Null0);//NULL: Does not care about the peer information - if(n = =-1) the { *Perror ("recvfrom Error"); $ }Panax Notoginseng write (stdout_fileno,buf,n); - } the Close (SOCKFD); + return 0; A}
Test results:
1. Start the service-side program first
2. Start the client program again
3. Enter a string in the client carriage return
4. The client can see the service-side conversion of uppercase string writeback to the client screen
5. Server can see the Client connection information (IP and port number)
UDP protocol----SIMPLE CS model implementation