The __HTML5 of UDP programming under Linux

Source: Internet
Author: User

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;
 }

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.