• Linux以下基于TCP多线程聊天室(client)


        不怎么会弄这个博客的排版,就直接将代码附上:

    主要是使用多线程去等待接受数据和发送数据。以下是client的代码:


    tcpsed.h文件

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
    #ifndef RTPSED_H
    #define RTPSED_H
    #define BUFFSIZE 512
    int runcond;
    int socketfd;
    typedef struct TCP_send_arg
    {
    char *tcpserver_addr;
    int tcp_port;
    }TCP_send_arg_t;
    void tcp_stophandler(int signum);
    void *tcppacketsend(void *arg);
    void quit(int signum);
    #endif

    tcpsed.c文件

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    #include "tcpsed.h"
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <sys/ioctl.h>
    #include <sys/param.h>
    #include <arpa/inet.h>
    #include <errno.h>
    #include <signal.h>
    #include <fcntl.h>
    #include <ctype.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/mman.h>
    #include <pthread.h>
    int socketfd;
    void tcp_stophandler(int signum)
    {
    runcond = 0;
    }
    void quit(int signum)
    {
    close(socketfd);
    printf("Bye~Bye! ");
    pthread_exit(NULL);
    }
    void *pth_read(void *arg)
    {
    int runcond = 1;
    int result;
    char pth_buf[BUFFSIZE];
    while(runcond)
    {
    if(0 > (result = read(socketfd,pth_buf,BUFFSIZE - 1)))
    {
    fprintf(stderr,"Read Error:%s ",strerror(errno));
    }
    pth_buf[result] = '';
    printf("%s ",pth_buf);
    }
    pthread_exit(NULL);
    }
    void *tcppacketsend(void *arg)
    {
    runcond = 1;
    struct sockaddr_in server_addr;
    char buf[BUFFSIZE] = {0};
    int connfd;
    pthread_t tid;
    TCP_send_arg_t *send_arg = (TCP_send_arg_t *)arg;
    memset(&server_addr,0,sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons((*send_arg).tcp_port);
    server_addr.sin_addr.s_addr = inet_addr((*send_arg).tcpserver_addr);
    if(-1 == (socketfd = socket(AF_INET,SOCK_STREAM,0)))
    {
    fprintf(stderr,"Socket Error:%s ",strerror(errno));
    pthread_exit(NULL);
    }
    if(-1 == (connfd = connect(socketfd,(struct sockaddr*)&server_addr,sizeof(server_addr))))
    {
    fprintf(stderr,"Connect Error:%s ",strerror(errno));
    pthread_exit(NULL);
    }
    printf("You can use "Ctrl + D" to quit. ");
    if(SIG_ERR == signal(SIGUSR1,quit))
    {
    fprintf(stderr,"Can't set SIGUSR1 signal action! ");
    pthread_exit(NULL);
    }
    if(0 != pthread_create(&tid,NULL,(void *)pth_read,(void *)buf))
    {
    fprintf(stderr,"Create pthread Error:%s ",strerror(errno));
    pthread_exit(NULL);
    }
    while(NULL != (fgets(buf,BUFFSIZE,stdin)))
    {
    write(socketfd,buf,strlen(buf) - 1);
    }
    pthread_cancel(tid);
    pthread_exit(NULL);
    }
    client.c文件

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    #include "tcpsed.h"
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <sys/ioctl.h>
    #include <sys/param.h>
    #include <arpa/inet.h>
    #include <errno.h>
    #include <signal.h>
    #include <fcntl.h>
    #include <ctype.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/mman.h>
    #include <pthread.h>
    int socketfd;
    void tcp_stophandler(int signum)
    {
    runcond = 0;
    }
    void quit(int signum)
    {
    close(socketfd);
    printf("Bye~Bye! ");
    pthread_exit(NULL);
    }
    void *pth_read(void *arg)
    {
    int runcond = 1;
    int result;
    char pth_buf[BUFFSIZE];
    while(runcond)
    {
    if(0 > (result = read(socketfd,pth_buf,BUFFSIZE - 1)))
    {
    fprintf(stderr,"Read Error:%s ",strerror(errno));
    }
    pth_buf[result] = '';
    printf("%s ",pth_buf);
    }
    pthread_exit(NULL);
    }
    void *tcppacketsend(void *arg)
    {
    runcond = 1;
    struct sockaddr_in server_addr;
    char buf[BUFFSIZE] = {0};
    int connfd;
    pthread_t tid;
    TCP_send_arg_t *send_arg = (TCP_send_arg_t *)arg;
    memset(&server_addr,0,sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons((*send_arg).tcp_port);
    server_addr.sin_addr.s_addr = inet_addr((*send_arg).tcpserver_addr);
    if(-1 == (socketfd = socket(AF_INET,SOCK_STREAM,0)))
    {
    fprintf(stderr,"Socket Error:%s ",strerror(errno));
    pthread_exit(NULL);
    }
    if(-1 == (connfd = connect(socketfd,(struct sockaddr*)&server_addr,sizeof(server_addr))))
    {
    fprintf(stderr,"Connect Error:%s ",strerror(errno));
    pthread_exit(NULL);
    }
    printf("You can use "Ctrl + D" to quit. ");
    if(SIG_ERR == signal(SIGUSR1,quit))
    {
    fprintf(stderr,"Can't set SIGUSR1 signal action! ");
    pthread_exit(NULL);
    }
    if(0 != pthread_create(&tid,NULL,(void *)pth_read,(void *)buf))
    {
    fprintf(stderr,"Create pthread Error:%s ",strerror(errno));
    pthread_exit(NULL);
    }
    while(NULL != (fgets(buf,BUFFSIZE,stdin)))
    {
    write(socketfd,buf,strlen(buf) - 1);
    }
    pthread_cancel(tid);
    pthread_exit(NULL);
    }

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <pthread.h>
    #include <errno.h>
    #include "tcpsed.h"
    #define PORT 8888
    #define IP "192.168.1.220"
    TCP_send_arg_t send_arg;
    int main(int argc,char *argv[])
    {
    pthread_t tid;
    void *tret;
    send_arg.tcp_port = PORT;
    send_arg.tcpserver_addr = IP;
    if(0 != pthread_create(&tid,NULL,(void *)tcppacketsend,(void *)&send_arg))
    {
    fprintf(stderr,"Create pthread error:%s ",strerror(errno));
    pthread_exit(NULL);
    }
    printf("OK! ");
    if(0 != pthread_join(tid,&tret))
    {
    fprintf(stderr,"Join thread error:%s ",strerror(errno));
    pthread_exit(NULL);
    }
    return 0;
    }

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    #include "tcpsed.h"
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <sys/ioctl.h>
    #include <sys/param.h>
    #include <arpa/inet.h>
    #include <errno.h>
    #include <signal.h>
    #include <fcntl.h>
    #include <ctype.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/mman.h>
    #include <pthread.h>
    int socketfd;
    void tcp_stophandler(int signum)
    {
    runcond = 0;
    }
    void quit(int signum)
    {
    close(socketfd);
    printf("Bye~Bye! ");
    pthread_exit(NULL);
    }
    void *pth_read(void *arg)
    {
    int runcond = 1;
    int result;
    char pth_buf[BUFFSIZE];
    while(runcond)
    {
    if(0 > (result = read(socketfd,pth_buf,BUFFSIZE - 1)))
    {
    fprintf(stderr,"Read Error:%s ",strerror(errno));
    }
    pth_buf[result] = '';
    printf("%s ",pth_buf);
    }
    pthread_exit(NULL);
    }
    void *tcppacketsend(void *arg)
    {
    runcond = 1;
    struct sockaddr_in server_addr;
    char buf[BUFFSIZE] = {0};
    int connfd;
    pthread_t tid;
    TCP_send_arg_t *send_arg = (TCP_send_arg_t *)arg;
    memset(&server_addr,0,sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons((*send_arg).tcp_port);
    server_addr.sin_addr.s_addr = inet_addr((*send_arg).tcpserver_addr);
    if(-1 == (socketfd = socket(AF_INET,SOCK_STREAM,0)))
    {
    fprintf(stderr,"Socket Error:%s ",strerror(errno));
    pthread_exit(NULL);
    }
    if(-1 == (connfd = connect(socketfd,(struct sockaddr*)&server_addr,sizeof(server_addr))))
    {
    fprintf(stderr,"Connect Error:%s ",strerror(errno));
    pthread_exit(NULL);
    }
    printf("You can use "Ctrl + D" to quit. ");
    if(SIG_ERR == signal(SIGUSR1,quit))
    {
    fprintf(stderr,"Can't set SIGUSR1 signal action! ");
    pthread_exit(NULL);
    }
    if(0 != pthread_create(&tid,NULL,(void *)pth_read,(void *)buf))
    {
    fprintf(stderr,"Create pthread Error:%s ",strerror(errno));
    pthread_exit(NULL);
    }
    while(NULL != (fgets(buf,BUFFSIZE,stdin)))
    {
    write(socketfd,buf,strlen(buf) - 1);
    }
    pthread_cancel(tid);
    pthread_exit(NULL);
    }
  • 相关阅读:
    深入理解类的填充规则
    深入理解C++虚函数表
    Java接入图灵机器人,实现与机器人聊天
    flex弹性布局学习笔记
    gulp插件gulp-usemin简单使用
    js基础知识温习:Javascript中如何模拟私有方法
    一次体验很不爽的面试经历
    JavaScript中in操作符(for..in)、Object.keys()和Object.getOwnPropertyNames()的区别
    js基础知识温习:构造函数与原型
    js基础知识温习:js中的对象
  • 原文地址:https://www.cnblogs.com/lxjshuju/p/7025849.html
Copyright © 2020-2023  润新知