c language file server

socket network programming of C language under Linux

As for the detailed steps of server establishment and the knowledge of socket, I have mentioned it in the article of python socket programming. You can refer to that blog about socket programming. Because of the C-related API, the socket API based on C language is used here to write the relevant network programming content. The specific implementation is as follows: , passed the debugging. Article link: http://www.cnblogs.com/uestc-mm/p/7296083.html

Server.c program content:

  1 #include <sys/types.h>
  2 #include <sys/socket.h>
  3 #include <netinet/in.h>
  4 #include <arpa/inet.h>
  5 #include <netdb.h>
  6 #include <stdio.h>
  7 #include <errno.h>
  8 #include <stdlib.h>
  9 #include <string.h>
 10 #include <unistd.h>
 11 /************************************************************************************************************************
 12 1,int socket(int family,int type,int protocol)
 13 family:
 14     Specifies the protocol cluster to use: AF ﹣ INET (IPv4) AF ﹣ inet6 (IPv6) AF ﹣ local (UNIX protocol) AF ﹣ route (routing socket) AF ﹣ key (secret key socket)
 15 type:
 16     Specifies the type of socket to use: sock'stream'sock'dgram
 17 protocol:
 18     If the socket type is not the original socket, this parameter is 0
 19 2,int bind(int sockfd, struct sockaddr *myaddr, int addrlen)
 20 sockfd:
 21     socket Socket descriptor returned by function
 22 myaddr:
 23     Is the structure pointer to the local IP address
 24 myaddrlen:
 25     Structural length
 26 struct sockaddr{
 27     unsigned short sa_family; //Communication protocol type family AF
 28     char sa_data[14];  //14 Byte Protocol address, including the IP address and port number of the socket
 29 };
 30 struct sockaddr_in{
 31     short int sin_family; //Communication protocol type family
 32     unsigned short int sin_port; //Port number
 33     struct in_addr sin_addr; //IP address
 34     unsigned char si_zero[8];  //Fill 0 to keep the same length as the sockaddr structure
 35 };
 36 3,int connect(int sockfd,const struct sockaddr *serv_addr,socklen_t addrlen)
 37 sockfd:
 38     socket Function returns socket descriptor
 39 serv_addr:
 40     Server IP address structure pointer
 41 addrlen:
 42     Length of structure pointer
 43 4,int listen(int sockfd, int backlog)
 44 sockfd:
 45     socket Socket descriptor after bind function binding
 46 backlog:
 47     Sets the maximum number of connections that can be connected to clients, and receives the effect of this value when multiple clients request from the server. Default value 20
 48 5,int accept(int sockfd,struct sockaddr *cliaddr,socklen_t *addrlen)
 49 sockfd:
 50     socket Socket descriptor after function listen
 51 cliaddr:
 52     Address structure of client socket interface
 53 addrlen:
 54     Client address structure length
 55 6,int send(int sockfd, const void *msg,int len,int flags)
 56 7,int recv(int sockfd, void *buf,int len,unsigned int flags)
 57 sockfd:
 58     socket Socket descriptor for function
 59 msg:
 60     Pointer to send data
 61 buf:
 62     Buffer for receiving data
 63 len:
 64     Length of data, set flags to 0
 65 *************************************************************************************************************************/
 66 int main(int argc, char *argv[])
 67 {
 68     int fd, new_fd, struct_len, numbytes,i;
 69     struct sockaddr_in server_addr;
 70     struct sockaddr_in client_addr;
 71     char buff[BUFSIZ];
 72 
 73     server_addr.sin_family = AF_INET;
 74     server_addr.sin_port = htons(8000);
 75     server_addr.sin_addr.s_addr = INADDR_ANY;
 76     bzero(&(server_addr.sin_zero), 8);
 77     struct_len = sizeof(struct sockaddr_in);
 78 
 79     fd = socket(AF_INET, SOCK_STREAM, 0);
 80     while(bind(fd, (struct sockaddr *)&server_addr, struct_len) == -1);
 81     printf("Bind Success!\n");
 82     while(listen(fd, 10) == -1);
 83     printf("Listening....\n");
 84     printf("Ready for Accept,Waitting...\n");
 85     new_fd = accept(fd, (struct sockaddr *)&client_addr, &struct_len);
 86     printf("Get the Client.\n");
 87     numbytes = send(new_fd,"Welcome to my server\n",21,0); 
 88     while((numbytes = recv(new_fd, buff, BUFSIZ, 0)) > 0)
 89     {
 90         buff[numbytes] = '\0';
 91         printf("%s\n",buff);
 92             if(send(new_fd,buff,numbytes,0)<0)  
 93             {  
 94                 perror("write");  
 95                 return 1;  
 96             }  
 97     }
 98     close(new_fd);
 99     close(fd);
100     return 0;
101 }

Client.c program content:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>

int main(int argc,char *argv[])
{
    int sockfd,numbytes;
    char buf[BUFSIZ];
    struct sockaddr_in their_addr;
    printf("break!");
    while((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1);
    printf("We get the sockfd~\n");
    their_addr.sin_family = AF_INET;
    their_addr.sin_port = htons(8000);
    their_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
    bzero(&(their_addr.sin_zero), 8);
    
    while(connect(sockfd,(struct sockaddr*)&their_addr,sizeof(struct sockaddr)) == -1);
    printf("Get the Server~Cheers!\n");
    numbytes = recv(sockfd, buf, BUFSIZ,0);//Receiving server information  
    buf[numbytes]='\0';  
    printf("%s",buf);
    while(1)
    {
        printf("Entersome thing:");
        scanf("%s",buf);
        numbytes = send(sockfd, buf, strlen(buf), 0);
            numbytes=recv(sockfd,buf,BUFSIZ,0);  
        buf[numbytes]='\0'; 
        printf("received:%s\n",buf);  
    }
    close(sockfd);
    return 0;
}

Use gcc compiler to compile and interpret client programs and server programs:

gcc -o Master Server.c
gcc -o Slave Client.c

The compiled results are as follows:

At this time, please run the Master program first, and then the Slave program:

Enter content to send in Client client:

Q1: we need to pay attention to the use of scanf (""% s ", buf") when the Client side uses scanf function to let the Client input information. If we use scanf (""% s \ n, buf "), the output result of the server side will lag the content of the Client side input by one time. You can experiment for specific reasons http://www.cnblogs.com/uestc-mm/p/7644370.html

Q2: in the process of writing the server program, I used while ((numbytes = recv (new_fd, buff, bufsiz, 0))! = - 1) to wait for the data to be received. It seems that there is a problem. As a result, I can't get the desired output all the time, so I changed it to while ((numbytes = recv (new_fd, buff, bufsiz, 0)) > 0). The specific reason is not clear ~ I'll experiment later, Ji Record here.

Published 6 original articles, won praise 3, visited 2314
Private letter follow

Tags: socket Programming network C

Posted on Thu, 06 Feb 2020 05:23:51 -0800 by mikewhy