The experiment __arduino of network programming under Linux

Source: Internet
Author: User

(1) TCP programming

Server-Side TCP_SERVER.C

#include  <stdlib.h>   #include  <stdio.h>   #include  <errno.h >   #include  <string.h>   #include  <netdb.h>   #include  <sys/types.h>   #include  <netinet/in.h>   #include  <sys/ socket.h>     #define  portnumber 3333    Int main (int argc,  Char *argv[])    {       int sockfd,new_fd;        struct sockaddr_in server_addr;       struct  sockaddr_in client_addr;       int sin_size;        int nbytes;      char buffer[1024];               /*  server-side starts to establish SOCKFD descriptor  */        if ((SOCKFD=socket (af_inet,sock_stream,0)) ==-1)  // AF_INET:IPV4; sock_stream:tcp      {            fprintf (stderr, "Socket error:%s\n\a", Strerror (errno));            exit (1);       }         /*  Server-side filled &NBSP;SOCKADDR structure  */       bzero (&server_addr,sizeof struct  SOCKADDR_IN); //  initialization, placing 0      server_addr.sin_family=AF_INET;                  // internet   &NBSP;&NBSP;&NBSP;&NBSP;SERVER_ADDR.SIN_ADDR.S_ADDR=HTONL (Inaddr_any);  //  ( Converts long data on this machine to long data on the network and any host communication   //INADDR_ANY  indicates that data that can receive any IP address is bound to all ip     &NBSP;&NBSP;//SERVER_ADDR.SIN_ADDR.S_ADDR=INET_ADDR ("192.168.1.1");   //forBinding to a fixed ip,inet_addr for converting digitally formatted IP into a plastic ip      server_addr.sin_port=htons (portnumber);          //  (convert short data on this machine to short data on the network) port number              /*  Bundle SOCKFD Descriptor to IP address  */        if (Bind (SOCKFD, (struct sockaddr *) (&server_addr), sizeof (STRUCT&NBSP;SOCKADDR)) ==-1)         {           fprintf (stderr, " Bind error:%s\n\a ", Strerror (errno));           exit (1);        }         /*  set maximum number of clients allowed to connect  */        if (Listen (sockfd,5) ==-1)        {           fprintf (stderr, "Listen error:%s\n\a", Strerror (errno));    &NBSp;       exit (1);       }         while (1)        {            /*  server is blocked until client program establishes the connection  */            sin_size=sizeof (struct sockaddr_in);           if ( (New_fd=accept (SOCKFD, (struct sockaddr *) (&AMP;CLIENT_ADDR), &sin_size)) ==-1)             {                fprintf (stderr, "Accept error:%s\n\a", Strerror (errno));                exit (1);            }           fprintf (stderr, "server get  connection&Nbsp;from %s\n ", Inet_ntoa (CLIENT_ADDR.SIN_ADDR); //  translates network addresses into. String                      if (Nbytes=read (New_fd,buffer, 1024) ==-1)            {                fprintf (stderr, "read error:%s\n", Strerror (errno));                exit (1);            }                  buffer[nbytes]= ';  '         printf (" Server received %s\n ", buffer);                     /*  This newsletter is over  */           &nbSp;close (NEW_FD);           /*  cycle Next  */       }         /*  End Newsletter  */        close (SOCKFD);       exit (0);  }    

Client tcp_client.c

#include  <stdlib.h>   #include  <stdio.h>   #include  <errno.h >   #include  <string.h>   #include  <netdb.h>   #include  <sys/types.h>   #include  <netinet/in.h>   #include  <sys/ socket.h>     #define  portnumber 3333    Int main (int argc,  Char *argv[])    {       int sockfd;        char buffer[1024];       struct sockaddr_in server_ addr;       struct hostent *host;              /*  use hostname query host  name  */      if (argc!=2 )        {           fprintf ( stderr, "UsaGe:%s hostname \a\n ", argv[0]);           exit (1);        }         if (Host=gethostbyname (argv[1)) )        {            ==null) fprintf (stderr, "gethostname error\n");           exit (1) ;       }         /*  client program started to set up   SOCKFD Descriptor  */       if ((Sockfd=socket (af_inet,sock_stream,0)) ==-1)  //  AF_INET:Internet; sock_stream:tcp      {            fprintf (stderr, "socket error:%s\a\n", Strerror (errno));            exit (1);       }         /*  Client programs populate service-side information  */       bzero (&server_addr,sizeof (SERVER_ADDR));  //   initialization, placing 0      server_addr.sin_family=AF_INET;           // IPV4      server_addr.sin_port=htons (portnumber);   //  (convert short data on this machine to short data on the network) port number       server_addr.sin_addr=* ( struct in_addr *) host->h_addr);  // ip address              /*  Client initiated connection request  */       if (SOCKFD, struct  sockaddr *) (&AMP;SERVER_ADDR), sizeof (STRUCT&NBSP;SOCKADDR)) ==-1)        {            fprintf (stderr, "connect error:%s\a\n", Strerror (errno));           exit (1);        }        /*  Connection succeeded  */       printf ("please  input char:\n ");            /*  send data  */      fgets (Buffer,1024,stdin);       write (Sockfd,buffer, strlen (buffer);         /*  End Newsletter  */        close (SOCKFD);       exit (0);  }  

(2) UDP programming

Server-Side UDP_SERVER.C

#include  <stdlib.h>  #include  <stdio.h>  #include  <errno.h>  # include <string.h>  #include  <unistd.h>  #include  <netdb.h>  # include <sys/socket.h>  #include  <netinet/in.h>  #include  <sys/types.h >  #include  <arpa/inet.h>    #define  SERVER_PORT 8888   #define  MAX_MSG_SIZE 1024     Void udps_respon (INT&NBSP;SOCKFD)    {       struct sockaddr_in addr;       int addrlen ,n;       char msg[MAX_MSG_SIZE];          while (1)        {   /*  read from the network and write to the network  */            bzero (msg,sizeof (msg)); //  initialization, clear 0           addrlen = sizeof (struct sockaddr);            n=recvfrom (sockfd,msg,max_msg_size,0, struct sockaddr*) &addr,& Addrlen); //  receive messages from the client           msg[n]=0;           /*  Display Server has received information  */            fprintf (stdout, "server have received %s", msg); //  display message       }  }     int main (void)    {       int sockfd;       struct sockaddr_in addr;          /*  server-side start to build socket descriptor  */        sockfd=socket (af_inet,sock_dgram,0);       if (sockfd<0)         {           fprintf (stderr, "socket error:%s\n", Strerror ( errno);           exit (1);        }         /*  server-side fill  sockaddr architecture  */        bzero (&addr,sizeof (struct sockaddr_in));        addr.sin_family=af_inet;       addr.sin_addr.s_addr=htonl (INADDR_ANY);        addr.sin_port=htons (server_port);         /*   Bundle SOCKFD Descriptor  */       if (Bind sockfd, (struct sockaddr *) & Addr,sizeof (struct sockaddr_in) <0)        {            fprintf (stderr, "bind error:%s\n", Strerror (errno));         &Nbsp;  exit (1);       }         udps_ Respon (SOCKFD); //  for read-write operations       close (SOCKFD);  }  

Client udp_client.c

#include  <stdlib.h>  #include  <stdio.h>  #include  <errno.h>  # include <string.h>  #include  <unistd.h>  #include  <netdb.h>  # include <sys/socket.h>  #include  <netinet/in.h>  #include  <sys/types.h >  #include  <arpa/inet.h>    #define  SERVER_PORT 8888   #define  MAX_BUF_SIZE 1024     Void udpc_requ (int sockfd,const struct  Sockaddr_in *addr,int len)    {       char buffer[max_buf_ size];       int n;       while (1)        {   /*  read from keyboard, write to service side  */            printf ("please input char:\n");            fgets (Buffer,max_buf_size,stdin);           sendto (SOCKFD, Buffer,strlen (buffer), 0,addr,len);           bzero (Buffer,MAX _buf_size);       }  }     Int main (INT&NBSP;ARGC, CHAR&NBSP;**ARGV)    {       int sockfd;        struct sockaddr_in addr;         if (argc!=2)         {           fprintf (stderr, " Usage:%s server_ip\n ", argv[0]);           exit (1);        }        /*  establish &NBSP;SOCKFD descriptor  */        sockfd=socket (af_inet,sock_dgram,0);       if ( sockfd<0)        {           fprintf (stderr, " Socket error:%s\n ", Strerror (errno));           exit (1);        }         /*  filling service-side information  */        bzero (&addr,sizeof (struct sockaddr_in));        addr.sin_family=AF_INET;       addr.sin_port=htons (Server_port);      if (Inet_aton (argv[1],&addr.sin_addr) <0)   /*inet_ The Aton function is used to convert a string-type IP address into a network 2 digital */       {            fprintf (stderr, "ip error:%s\n", Strerror (errno));            exit (1);       }          udpc_requ (sockfd,&addr,sIzeof (struct sockaddr_in)); //  for read-write operations       close (SOCKFD);  }   

(3) Concurrent server design (TCP needs to establish a connection, so for the loop server, by creating a subprocess fork () to implement; UDP can be concurrent)

Server-side tcp_server_fork.c, client ditto

#include <stdlib.h> #include <stdio.h> #include <errno.h>&

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.