Embedded Linux network Programming (III)--UDP programming model

Source: Internet
Author: User
Tags strcmp server port htons

embedded Linux Network Programming (III.)--UDP programming model

UDP programming Model:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/83/F8/wKioL1eCUczx3fu_AACwf5ri25Q159.png "title=" Picture 6.png "alt=" Wkiol1ecuczx3fu_aacwf5ri25q159.png "/>

The UDP loop server model is:

Socket (...); Bind (...);   while (1) {recvfrom (...);   Process (...); SendTo (...);}

server.c code:

#include  <stdio.h> #include  <stdlib.h> #include  <string.h> #include  < unistd.h> #include  <sys/socket.h> #include  <netinet/in.h> #include  <arpa/inet.h >  #define  port  8888#define buffer_size 1024 int main (INT&NBSP;ARGC ,  char *argv[]) {int sockfd;sockfd = socket (af_inet, sock_dgram, 0);  //   Create a UDP socket if (sockfd < 0) {fprintf (stderr,  "socket function failed.\n"); exit (-1) ;} Struct sockaddr_in server_addr;bzero (&server_addr, sizeof (SERVER_ADDR)); Server_addr.sin_ Family = af_inet;// ipv4server_addr.sin_port   = htons (port);//  Port server_addr.sin_addr.s_addr = htonl (inaddr_any); // ip//  binding int err;err =  bind (sockfd,  (struct sockaddr*) &server_addr, sizeof (SERVER_ADDR)); if (err !=  0) {fprintf (STDERR,&NBSP; " Bind function failed.\n "); Close (SOCKFD); exit (-1);} fprintf (stdout,  "server runiing on %d\n",  port); char recvbuf[buffer_size];struct  sockaddr_in client_addr;while (1) {Int recvlen;bzero (&recvbuf, sizeof (RECVBUF)); Socklen _t len = sizeof (CLIENT_ADDR); Bzero (&client_addr, sizeof (CLIENT_ADDR)); if (strcmp ( Recvbuf, "exit\n") ==0)             break;         if (strcmp (Recvbuf, "q\n") ==0)              break;        if (strcmp (Recvbuf, " quit\n ") ==0)             break;//  Receive client Data Recvlen = recvfrom (sockfd, recvbuf, sizeof (recvbuf), 0,  (struct  sockaddr*) &client_addr, &len) fprintf (stdout,  "HAVE&NBSP;A&NBSP;NEW&NBSp;client :%s, port:%d\n ",  inet_ntoa (CLIENT_ADDR.SIN_ADDR),  ntohs (Client_addr.sin_port));  //  Client's ipprintf ("message: %s\n",  recvbuf); SendTo (Sockfd, recvbuf, recvlen,  0,  (struct sockaddr*) &client_addr, len);} Close (SOCKFD); return 0;}


CLIENT.C Code:

#include  <sys/types.h> #include  <sys/socket.h> #include  <stdio.h> #include   <netinet/in.h> #include  <arpa/inet.h> #include  <unistd.h> #include  <string.h > #include  <stdlib.h> #include  <fcntl.h> #include  <sys/shm.h>  #define  port  8888#define buffer_size 1024 int main () {    /// Definition Sockfd    int clientsockfd = socket (af_inet, sock_dgram, 0);     ///definition sockaddr_in    struct sockaddr_in servaddr;     bzero (&servaddr, sizeof (SERVADDR));     servaddr.sin_family  = af_inet;    servaddr.sin_port = htons (port);   ///Server port &NBSP;&NBSP;&NBSP;&NBSP;SERVADDR.SIN_ADDR.S_ADDR&NBSP;=&NBSP;INET_ADDR ("192.168.0.200");   ///server IP     ///Connection ServiceSuccessfully returned 0, error returned -1    if  (Connect (clientsockfd,  (struct sockaddr *) & Servaddr, sizeof (SERVADDR))  < 0)     {         fprintf (stderr,  "connect function failed.\n");         exit ( -1);    }    char sendbuf[buffer_size];     char recvbuf[buffer_size];    bzero (Sendbuf, sizeof ( SENDBUF));     bzero (Recvbuf, sizeof (RECVBUF));    while  (1)     {    fgets (Sendbuf, sizeof (SENDBUF),  stdin);     if (strcmp (SendBuf, "exit\n") ==0)              break;        if (strcmp (SendBuf, "q\n") ==0)              break;        if (strcmp (SendBuf, "quit\n") ==0)             break;         send (Clientsockfd, sendbuf, strlen (SENDBUF), 0);  ///send      &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RECV (clientsockfd, recvbuf,  sizeof (RECVBUF), 0);  ///reception                  fprintf (stdout,  "%s\n",  recvbuf); Bzero (Sendbuf, sizeof (sendbuf));         bzero (recvbuf, sizeof (recvbuf));    }     close (CLIENTSOCKFD);     return 0;}


This article from "Endless life, Struggle not only" blog, reprint please contact the author!

Embedded Linux network Programming (III)--UDP programming model

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.