UDP socket programming under Linux

Source: Internet
Author: User

I. Related processes and knowledge please see my other blog, "Winsock Sockets Programming", which is no longer described here.



Two. Related code:

SERVER.C:

 1 /****************************************                                                                                                        2      > File Name:server.c  3     >  Author:xiaoxiaohui  4     > mail:[email protected]   5     > created time:2016 May 21   Saturday  15 05 minutes 23 seconds    6 ************/  7   8  #include <stdio.h>  9 # include<stdlib.h> 10  #include <sys/types.h> 11  #include <sys/socket.h>  12  #include <unistd.h> 13  #include <arpa/inet.h> 14  #include < netinet/in.h> 15  #include <string.h> 16  17 const int port  = 9090; 18 const int LEN = 1024; 19 int serverSock;  20 struct sockaddr_in local; 21 struct sockaddr_in client; 22   23  24 int main ()  25 { 26      Serversock = socket (af_inet, sock_dgram, 0); 27  28      local.sin_family = AF_INET; 29     //local.sin_addr.s_addr  = htonl (inaddr_any);  30&nbsP;    local.sin_port = htons (port); 31      LOCAL.SIN_ADDR.S_ADDR&NBSP;=&NBSP;INET_ADDR ("127.0.0.1");  32     bind (ServerSock ,  (struct sockaddr*) &local, sizeof (local)); 33  34      char buf[len]; 35     while (1)  36     {  37         int ret = 0; 38          socklen_t  len = sizeof (client); 39          memset (buf,  ',  len); 40          ret = recvfrom (Serversock, buf, len - 1,  0,  (struct sockaddr*) &client, &len); 41  42          printf ("ret is %d",  ret);  43         if (ret  == 0)  44         { 45              printf ("client is closed!\n");  46              exit (2); 47          } 48         else  if (ret < 0)  49         { 50              perror ("Recvfrom"); 51              continue; 52          } 53         else  54         { 55              buf[ret] =  '; 56              printf ("client[ip:%s][port:%d]# %s\n",  inet_ntoa (CLIENT.SIN_ADDR),  57                       ntohs (Client.sin_port),  buf); 58              fflush (stdout); 59         } 60   61         if (Strstr (buf,  "quit")  != null)  62         { 63              close (Serversock); 64              return 0; 65         } 66      } 67     return 0; 68 }

Client.c:

1 /****************************************                                                                                                        2      > File Name:client.c  3     >  author:xiaoxiaohui  4     > mail:[email protected]   5     > created time:2016 May 21   Saturday  15 35 minutes, 32 seconds   6  ******************/  7   8  #include <stdio.h>  9 # include<sys/types.h> 10  #include <sys/socket.h> 11  #include <unistd.h>  12  #include <arpa/inet.h> 13  #include <netinet/in.h> 14  #include < string.h> 15  16 const int port = 9090; 17 const  char* ip =  "127.0.0.1"; 18 //const char* ip =  "192.168.0.145";  19 const int len = 1024; 20 int clientsock; 21 struct  sockaddr_in server; 22  23 int main ()  24 { 25  26      clientsock = socket (af_inet, sock_dgram, 0); 27   28     server.sin_family = AF_INET; 29      server.sin_addr.s_addr&nbsP;=&NBSP;INET_ADDR (IP);  30     server.sin_port = htons (port);  31   32     char buf[len]; 33     while (1)  34     { 35         memset ( buf,  ' ",  len);  36         printf (" please  input:  ");  37         fflush (stdout); 38          gets (BUF); 39  40          int ret = sendto (Clientsock, buf, strlen (BUF), 0,  ( struct sockaddr*) &server, sizeof (server)); 41          if (ret <= 0)  42         { 43     &nBsp;        perror ("SendTo"); 44              continue; 45          } 46  47         if (strcmp (buf,  " Quit ")  == 0)  48         { 49              close (Clientsock); 50              return 0; 51          } 52     } 53  54      return 0; 55 }

Makefile:

 1 . phony:all                                                                                                                                         2 all:server client  3   4 server: Server.c  5     gcc -o [email protected] $^ -g   6 client:client.c  7     gcc -o [email protected] $^  -g  8   9 . Phony:clean 10 clean: 11     rm -f server client

Execution Result:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/80/E9/wKioL1dEYt2zwgETAABFK7Fs5Cw552.png "title=" 2016-05-24 22-14-39 screen. png "alt=" Wkiol1deyt2zwgetaabfk7fs5cw552.png "/>


650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/80/E9/wKioL1dEYuzA-1OzAAB70infQRk075.png "title=" 2016-05-24 22-14-50 screen. png "alt=" Wkiol1deyuza-1ozaab70infqrk075.png "/>



Three. Summary:

UDP socket programming is not necessary to establish a link, so the server does not have listen and accept, the client does not have Connect,recvfrom and sendto in each other's socket information.

Because it is link-oriented, UDP is more efficient than TCP in transmitting data and is suitable for streaming media or applications with low reliability requirements.

This article is from "Narcissus" blog, please make sure to keep this source http://10704527.blog.51cto.com/10694527/1782717

UDP socket programming under Linux

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.