unix网络编程 服务器和客户端通信

Posted on 2019-12-07 16:48  辉仔猿  阅读(237)  评论(0编辑  收藏  举报

服务器代码:

//server.c

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

#define MAX 5000
#define LISTENQ 1024

int main(int argc,char *argv[])
{
     int sockfd,cliefd;
     pid_t childpid;
     struct sockaddr_in servaddr,cliaddr;
     char cli[MAX],ser[MAX];
     
     if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
     {
         printf("socket error\n");
         exit(0);
     }
    
     bzero(&servaddr,sizeof(servaddr));
     servaddr.sin_family=AF_INET;
     servaddr.sin_port=htons(8080);
     servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
     
    int opt = 1;
    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) != 0)
    {           
    perror("Server setsockopt failed");
    return 1;
    }
    if((bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr)))<0)
     {
           printf("bind error:%s\n",strerror(errno));
           exit(0);
     }
     
     listen(sockfd,LISTENQ);

    while(1)
   { 
     printf("waiting for connecting.\n");
     if((cliefd=accept(sockfd,(struct sockaddr*)NULL,NULL))<0)
    {
        printf("connect faild\n");
        exit(1);
    }
    else
    {
        printf("connect success\n");
    }
    printf("welcome to chatting rome!!\n");
    
    if((childpid=fork())==0)
    {
      close(sockfd);
      while(1)
     {
      int m;
      recv(cliefd,cli,500,0);
       m=strlen(cli);
       cli[m]='\0';
      if(cli[0]=='#') 
      {
        printf("From client:%s\n",cli);
        printf("Over\n");
        exit(0);
       }
      else
      {
        printf("From client:%s\n",cli);
       // bzero(&cli,sizeof(cli));
        printf("To client:");
        fgets(ser,500,stdin);
        send(cliefd,ser,500,0);
        if(ser[0]=='#')
        {
          printf("Over\n");
      bzero(ser,0);
          bzero(&cliefd,sizeof(cliefd));
          return(-1);
        }
       }
      }
    close(cliefd);
     }
     }
    close(sockfd);
   return 0;
}          

 

客户端代码:

//client.c

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

#define MAX 1500

int main(int argc,char *argv[])
{
    int sockfd;
    struct sockaddr_in servaddr;
    char ser[MAX],cli[MAX];

    if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
    {
      printf("soket error\n");
      exit(0);
    }
   
    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family=AF_INET;
    servaddr.sin_port=htons(8080);
    inet_pton(AF_INET,argv[1],&servaddr.sin_addr);
    
    if(connect(sockfd,(struct sockaddr*)&servaddr,sizeof(struct sockaddr))<0)
    {
      printf("connect error:%s\n",errno);
      exit(0);
    }
    else
    {
       printf("connect success\n");
       printf("welcome to chatting rome!\n");
    }
    
    while(1)
   {
     int m;
     printf("To server:");
     fgets(cli,500,stdin);
     //m=strlen(cli);
     //cli[m]='\0';
     send(sockfd,cli,500,0);
     if(cli[0]=='#')
     {
        printf("Over\n");
        exit(0);
     }
     else
     {  
        bzero(&cli,sizeof(cli));
        recv(sockfd,ser,500,0);
        m=strlen(ser);
        ser[m]='\0';
        printf("From server:%s\n",ser);
        if(ser[0]=='#')
        {
           printf("Over\n");
           exit(0);
        }
        bzero(&ser,sizeof(ser));
      }
    }
    close(sockfd);
    return 0;
}

 

服务器运行结果:

客户端运行结果: