• Linux c实现服务端与客户端聊天


    主要利用socket通信实现,具体代码如下

    客户端:

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 #include <sys/types.h>
     5 #include <sys/socket.h>
     6 #include <netinet/in.h>
     7 #include <arpa/inet.h>
     8 #include <unistd.h>
     9 #define MAXLINE 511
    10 
    11 
    12 char *EXIT_STRING = "exit";
    13 int input_and_send(int sd);
    14 int recv_and_print(int sd);
    15 
    16 
    17 
    18 
    19 int main(int argc, char *argv[]){
    20     pid_t pid;
    21      int s;
    22     static struct sockaddr_in servaddr;
    23     
    24     if(argc != 3){
    25         printf("사용법: %s server_ip port 
    ",argv[0]);
    26         exit(0);
    27     }
    28     if((s = socket(PF_INET, SOCK_STREAM, 0)) < 0){
    29         printf("Client: Can't open stream socket.
    ");
    30         exit(0);
    31     }
    32     
    33     bzero((char *)&servaddr,sizeof(servaddr));
    34     servaddr.sin_family = AF_INET;
    35     inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
    36     servaddr.sin_port = htons(atoi(argv[2]));
    37     
    38     if(connect(s,(struct sockaddr *)&servaddr,sizeof(servaddr)) < 0){
    39         printf("Client: can't connect to server.
    ");
    40         exit(0);
    41     }
    42     if( (pid=fork())>0 )
    43         input_and_sent1(s);
    44     else if(pid == 0)
    45         recv_and_print(s);
    46     close(s);
    47     return 0;
    48 }
    49 
    50 int input_and_sent1(int sd){
    51 
    52     char buf[MAXLINE+1];
    53     int nbyte;
    54     while(fgets(buf, sizeof(buf),stdin) != NULL){
    55         nbyte = strlen(buf);
    56         write(sd, buf, strlen(buf));
    57         if(strstr(buf,EXIT_STRING) != NULL){
    58             puts("Good Bye!");
    59             close(sd);
    60             exit(0);
    61         }
    62     }
    63      return 0;
    64 }
    65 
    66 int recv_and_print(int sd){
    67     char buf[MAXLINE+1];    
    68     int nbyte;
    69     while(1){
    70         if((nbyte=read(sd, buf,MAXLINE))<0){
    71             perror("read fail");
    72             close(sd);
    73             exit(0);
    74         }
    75     buf[nbyte] = 0;
    76     if(strstr(buf,EXIT_STRING) != NULL)
    77         break;
    78     printf("Girl: %s",buf);
    79     }
    80     return 0;
    81 }
    82     
    83     

    服务器端:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    
    char *EXIT_STRING = "exit";
    int recv_and_print(int sd);
    int input_and_send(int sd);
    #define MAXLINE 511
    
    int main(int argc, char *argv[])
    {
        struct sockaddr_in cliaddr, servaddr;
        int listen_sock,accp_sock,addrlen = sizeof(cliaddr);
        pid_t pid;
        if(argc != 2){
            printf("사용법: %s port
    ",argv[0]);
            exit(0);
        }
        
        if((listen_sock=socket(PF_INET, SOCK_STREAM, 0)) < 0){
            perror("socket fail");
            exit(0);
        }
        
        bzero((char *)&servaddr, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
        servaddr.sin_port = htons(atoi(argv[1]));
    
        if(bind(listen_sock, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0){
            perror("bind fail");
            exit(0);
        }
        puts("等待连接......");
        listen(listen_sock, 1);
    
        if((accp_sock = accept(listen_sock,(struct sockaddr *)&cliaddr, &addrlen)) < 0){
            perror("accept fail");
            exit(0);
        }
        
        puts("连接成功.....");
        if((pid = fork()) > 0)
            input_and_send(accp_sock);
        else if(pid == 0)
            recv_and_print(accp_sock);
        close(listen_sock);
        close(accp_sock);
        return 0;
    }
    
    int input_and_send(int sd){
        char buf[MAXLINE+1];
        int nbyte;
        while(fgets(buf, sizeof(buf),stdin) != NULL){
            nbyte = strlen(buf);
            write(sd, buf ,strlen(buf));
            if(strstr(buf, EXIT_STRING) != NULL){
                puts("Good Bye.");
                close(sd);
                exit(0);
            }
        }
        return 0;
    }
    
    int recv_and_print(int sd){
        char buf[MAXLINE+1];
        int nbyte;
        while(1){
            if((nbyte=read(sd, buf, MAXLINE)) < 0){
                perror("read fail");
                close(sd);
                exit(0);
            }
        buf[nbyte] = 0;
        if(strstr(buf, EXIT_STRING) != NULL)
            break;
        printf("Harley Gwak: %s",buf);
        }
        return 0;
    }


    第一次写博客,一直都是伸手党,今天就把我之前写过的东西分享下,代码正常运行的,仅供参考。

  • 相关阅读:
    gojs入门
    chartjs:改变图表的外观
    chart.js入门
    verilog与C语言的6点重大区别
    PCB布线原则【转】_神经火光_百度空间
    verilog中对同一个变量有判断条件的赋值
    同步复位与异步复位——异步复位同步释放
    如何利用TCL文件给FPGA分配引脚
    0欧姆电阻的作用
    独热码
  • 原文地址:https://www.cnblogs.com/harleygwak1206/p/3147509.html
Copyright © 2020-2023  润新知