Compared with TCP, we know that UDP is oriented to connectionless and unreliable communication, it is suitable for data transmission data and the reliability of the data is not high, because it is connectionless, so it's high efficiency of data transmission, but also for real-time requirements of higher occasions such as: Audio and video chat.
This article will introduce how to program UDP under Linux, the full text through the Code annotated form to explain.
Under Linux, UDP programming can be analogous to texting:
Programming Process:
Server:
1. Create a Datagram socket (socket (SOCK_DGRAM)) ——————-got a cell phone.
2. Specify local network information (struct sockaddr_in) ——————-have the number
3. Bound Network information (bind ()) ———————————— – Binding number
4. Receive/Send the information recvfrom ()/sendto () ————————— receiving/sending short interest
5. Closes socket close () ——————————————-send and receive
Client:
1. Create a Datagram socket (socket (SOCK_DGRAM)) ——————— cell phone
2. Specify the server's network information (SOCKADDR_IN) ——————— have the offset number
3. Send/Receive information (SendTo ()/recvfrom ()) —————— – Send/Receive SMS
4. Closes socket close () ——————————————-send and receive
******************* server-side ***************************
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <errno.h> #include <sys/ socket.h> #include <sys/types.h> #include <arpa/inet.h> #include <netinet/in.h> #define N 32// Error handling macro function #define ERR_LOG (log) \ do{\ perror (log); \ exit (1); \} while (0) typedef struct SOCKADDR SA;
Defines the general network information structure Body int main (int argc, const char *argv[]) {struct sockaddr_in serveraddr,clientaddr;
int sockfd;
Char buf[n]={0};
socklen_t len=sizeof (SA);
1. Create a Datagram socket if ((Sockfd=socket (af_inet,sock_dgram,0)) <0) {Err_log ("fail to Socket:"); //2. Populate the local network information serveraddr.sin_family=af_inet;
IPv4 Network Protocol serveraddr.sin_port=htons (Atoi (argv[2));//port number Serveraddr.sin_addr.s_addr=inet_addr (argv[1));//IP Address
3. Bind Network Information if (Bind (SOCKFD, (sa*) &serveraddr,len) <0) {Err_log ("fail to bind:"); //4. Send and receive information while (1) {recvfrom (sockfd,buf,sizeof) (bUF), 0, (sa*) &clientaddr,&len); Receive data from the client printf ("%s\n", buf); Print out the Received Data memset (buf,0,sizeof (BUF)); Clear Data buffer strcpy (buf, "Hello World"); Copy data to data buffer SendTo (sockfd,buf,sizeof (BUF), 0, (sa*) &clientaddr,len);
Send packet to client} return 0;
}
******************* client ***************************
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <errno.h> #include <sys/ socket.h> #include <sys/types.h> #include <arpa/inet.h> #include <netinet/in.h> #define N 32// Error handling macro function #define ERR_LOG (log) \ do{\ perror (log); \ exit (1); \} while (0) typedef struct SOCKADDR SA;
Defines the general network information structure Body int main (int argc, const char *argv[]) {struct sockaddr_in serveraddr;
int sockfd;
Char buf[n]={0};
socklen_t len=sizeof (SA);
1. Create a Datagram socket if ((Sockfd=socket (af_inet,sock_dgram,0)) <0) {Err_log ("fail to Socket:"); //2. Populate server-side network information serveraddr.sin_family=af_inet;
IPv4 Network Protocol serveraddr.sin_port=htons (Atoi (argv[2));//port number Serveraddr.sin_addr.s_addr=inet_addr (argv[1));//IP Address 3. Receive and send information while (1) {fgets (buf,sizeof (BUF), stdin),//Standard input read Data sendto (sockfd,buf,sizeof (BUF), 0, ( sa*) &serveraddr,len); Send packets to the server side (if you don't care about the client's network information the last two parameters can be set to NULL) bzero (buf,sizeof (BUF)); Empty data buffer Recvfrom (sockfd,buf,sizeof (BUF), 0, (sa*) &serveraddr,&len); Receive data puts (BUF) from the server side;
Print out the received data} return 0;
}