Details about UDP communication instances in one of network programming in Linux

Source: Internet
Author: User

We recommend that you use eclipse for C ++ for development ~~~ Easy to use!

As for the specific communication process, there is a lot of online information.

No explanation. The detailed explanation is included in the code comment.

Client code:

Client. cpp

# Include <stdio. h> # include <sys/types. h> # include <sys/socket. h> # include <netinet/in. h> # include <unistd. h> # include <errno. h> # include <string. h> # include <stdlib. h> # include <sys/IOCTL. h> # include <net/if. h> # include <ARPA/inet. h> # define dest_ip "127.0.0.1" # define serv_port 8000int main () {/* socket file descriptor */INT sock_fd;/* Create UDP socket */sock_fd = socket (af_inet, sock_dgram, 0); If (sock_fd <0) {perror ("socket"); exit (1) ;}/ * defines a connection address structure */struct sockaddr_in addr_serv; int Len; memset (& addr_serv, 0, sizeof (struct sockaddr_in )); // ************* set the protocol family to IPv4 ************ // addr_serv.sin_family = af_inet; // ************** set the connection server port ************ // both the port number and IP address it is stored in network bytes, you need to re-Sort addr_serv.sin_port = htons (serv_port); // convert to the network byte order, small End-> Large End // ************** set the connection server address ************//// 0th writing methods, use inaddr_any to represent the server address/* inaddr_any to indicate that no matter which Nic receives the data, the program will receive the data as long as the port number corresponds * // addr_serv.sin_addr.s_addr = htonl (inaddr_any ); // 1st address conversion methods (Point-to-10 conversion to in_addr_t type) addr_serv.sin_addr.s_addr = inet_addr ("127.0.0.1 "); // error returned inaddr_none // 2nd // inet_aton ("127.0.0.1", & addr_serv.sin_addr ); // ************** set the connection server address ************ // Len = sizeof (addr_serv); // ************* when obtaining the MAC address of the local machine, you can ignore ************ // struct ifreq IFR; IFR. ifr_addr.sa_family = af_inet; strncpy (IFR. ifr_name, "eth0", ifnamsiz-1); IOCTL (sock_fd, siocgifhwaddr, & IFR); printf ("I Am a client, my mac address :"); printf ("%. 2x: %. 2x: %. 2x: %. 2x: %. 2x: %. 2x \ n ", (unsigned char) IFR. ifr_hwaddr.sa_data [0], (unsigned char) IFR. ifr_hwaddr.sa_data [1], (unsigned char) IFR. ifr_hwaddr.sa_data [2], (unsigned char) IFR. ifr_hwaddr.sa_data [3], (unsigned char) IFR. ifr_hwaddr.sa_data [4], (unsigned char) IFR. ifr_hwaddr.sa_data [5]); // the MAC address is obtained, and INT recv_num; int send_num; char send_buf [20] = "Hi, I am the client"; char recv_buf [20]; // receive Cache/**************** send data to the server *************/// /compared with TCP, UDP saves the connect function, namely three handshakes, and can directly send data // int sendto (INT socket, void * buffer. size_t size, int flags, struct sockaddr * ADDR, socklen_t length) send_num = sendto (sock_fd, send_buf, strlen (send_buf), 0 (struct sockaddr *) & addr_serv, Len ); printf ("client: % s \ n", send_buf); If (send_num <0) {perror ("sendto error"); exit (1 );} // *************** receive server data ************** // int recvfrom (int socket, void * buffer, size_t size, int flags, struct sockaddr * ADDR, socklen_t * length-PTR) recv_num = recvfrom (sock_fd, recv_buf, sizeof (recv_buf), 0, (struct sockaddr *) & addr_serv, (socklen_t *) Len); recv_buf [recv_num] = '\ 0'; printf ("received by the Client: % d Bytes: % s \ n ", recv_num, recv_buf); close (sock_fd); Return 0 ;}

Server code:

Server. cpp

# Include <stdio. h> # include <sys/types. h> # include <sys/socket. h> # include <netinet/in. h> # include <unistd. h> # include <errno. h> # include <string. h> # include <stdlib. h> # include <sys/IOCTL. h> # include <net/if. h> # define serv_port 8000int main () {/* socket file descriptor */INT sock_fd;/* Create UDP socket */sock_fd = socket (af_inet, sock_dgram, 0 ); if (sock_fd <0) {perror ("socket"); exit (1) ;}/ * Set address */struct sockaddr_ I N addr_serv; int Len; memset (& addr_serv, 0, sizeof (struct sockaddr_in); addr_serv.sin_family = af_inet; // htons meaning: H: Host N: Network s: short l: long L: longaddr_serv.sin_port = htons (serv_port); // converts it to the network byte sequence. The Small End-> large end/* inaddr_any indicates that no matter which network adapter receives data, as long as the port number corresponds, the program will receive */addr_serv.sin_addr.s_addr = htonl (inaddr_any); Len = sizeof (addr_serv);/* bind socket * // int BIND (INT socket, struct sockaddr * ADDR, socklen_t Length) if (BIND (sock_fd, (struct sockaddr *) & addr_serv, sizeof (addr_serv) <0) {perror ("BIND error:"); exit (1 );} int recv_num; int send_num; char send_buf [20] = "I am server! "; Char recv_buf [20]; // receives the cache struct sockaddr_in addr_client; // gets the local MAC address struct ifreq IFR; IFR. ifr_addr.sa_family = af_inet; strncpy (IFR. ifr_name, "eth0", ifnamsiz-1); IOCTL (sock_fd, siocgifhwaddr, & IFR); printf ("I Am a server, my mac address :"); printf ("%. 2x: %. 2x: %. 2x: %. 2x: %. 2x: %. 2x \ n ", (unsigned char) IFR. ifr_hwaddr.sa_data [0], (unsigned char) IFR. ifr_hwaddr.sa_data [1], (unsigned char) IFR. ifr_hwaddr.sa_data [2], (unsigned Char) IFR. ifr_hwaddr.sa_data [3], (unsigned char) IFR. ifr_hwaddr.sa_data [4], (unsigned char) IFR. ifr_hwaddr.sa_data [5]); // loop wait while (1) {printf ("server wait: \ n");/* recvfrom () receives data from UDP-based APIs, includes the IP address of the sender. Because the data packet may come from any host * flag = 0, regular reading, equivalent to the read () function. Flag = msg_peek, read data but do not refresh the read buffer. MSG--OOB, read out-of-band data, common data ignored */recv_num = recvfrom (sock_fd, recv_buf, sizeof (recv_buf), 0, (struct sockaddr *) & addr_client, (socklen_t *) & Len); If (recv_num <0) {perror ("recvfrom error:"); exit (1) ;} recv_buf [recv_num] = '\ 0 '; printf ("server received: % d Bytes: % s \ n", recv_num, recv_buf); // send data send_num = sendto (sock_fd, send_buf, recv_num, 0, (struct sockaddr *) & addr_client, Len); printf ("server Description: % s \ n", send_buf); If (send_num <0) {perror ("sendto error: "); exit (1) ;}close (sock_fd); 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.