想喝羊肉鲜汤 2022-11-21 15:15 采纳率: 100%
浏览 9
已结题

socket多线程服务端与多客户端通信

基于socket的客户端与服务端聊天程序,当使用子进程时可完成,把fork()更改为进程之后,只能实现一对一通信,可以检测到多客户端接入,但是返回信息都只能发回到第一个接入的客户端。

//server.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <pthread.h>

pthread_mutex_t mutex[3];

struct handlerBuf
{
    int fd;
    int serial;
};

void *writeHandler(void *hb)
{
    struct handlerBuf *hb_w = (struct handlerBuf *)hb;
    char sndbuf[128];
    char rev[128];
    sprintf(rev,"get connect,you'r NO.%d\n",hb_w->serial+1);
    write(hb_w->fd,rev,128);
    while(1){
        getchar();
        pthread_mutex_lock(&mutex[hb_w->serial]);
        memset(sndbuf,0,128);
        printf(">");
        scanf("%s",sndbuf);
        write(hb_w->fd,sndbuf,sizeof(sndbuf));
        printf("seng over\n");
        pthread_mutex_unlock(&mutex[hb_w->serial]);
        getchar();
    }
}

void *readHandler(void *hb)
{
    int n_read;
    struct handlerBuf *hb_r = (struct handlerBuf *)hb;
    char readbuf[128];
    while(1){
        n_read = read(hb_r->fd,readbuf,128);
        pthread_mutex_lock(&mutex[hb_r->serial]);
        if(n_read > 0)
        {
            printf("get from NO.%d:%s\n",hb_r->serial+1,readbuf);
        }
        pthread_mutex_unlock(&mutex[hb_r->serial]);
    }
}

void *clientHandler(void *hb)
{
    pthread_t newthread;
    pthread_create(&newthread,NULL,readHandler,hb);
    writeHandler(hb);
}

int main(int argc,char **argv)
{
    int s_fd;
    int c_fd;
    int n_read;
    pthread_t thread[3];
    int i = 0;
    int j = 0;
    char readbuf[128];
    char msg[128];
    
    struct handlerBuf hb[3];

    struct sockaddr_in s_addr;
    struct sockaddr_in c_addr;

    memset(&s_addr,0,sizeof(struct sockaddr_in));
    memset(&c_addr,0,sizeof(struct sockaddr_in));

    if(argc != 3)
    {
        printf("ip addr failed\n");
    }


    s_fd = socket(AF_INET,SOCK_STREAM,0);
    if(s_fd == -1)
    {
        perror("socket");
        exit(-1);
    }
    
    s_addr.sin_family = AF_INET;
    s_addr.sin_port = htons(atoi(argv[2]));
    inet_aton(argv[1],&s_addr.sin_addr);

    bind(s_fd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));
    listen(s_fd,3);

    int clen = sizeof(struct sockaddr_in);

    while(1)
    {
        c_fd = accept(s_fd,(struct sockaddr *)&c_addr,&clen);


        if(c_fd == -1)
        {
            perror("accept");
        }else{
            printf("NO.%d client connect\n",j+1);
            pthread_mutex_init(&mutex[j],NULL);
            hb[j].fd = c_fd;
            hb[j].serial = j;
            pthread_create(&thread[j],NULL,clientHandler,(void **)&hb);
            j++;
            if(j == 3)
            {
                j = 0;
            }
        }
    }


    return 0;
}




//client.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>

pthread_mutex_t mutex;


void *writeHandler(void *fd)
{
    int w_fd = *(int *)fd;
    char sndbuf[128];
    while(1)
    {
        getchar();
        memset(sndbuf,0,128);
        pthread_mutex_lock(&mutex);
        printf(">");
        scanf("%s",sndbuf);
        write(w_fd,sndbuf,sizeof(sndbuf));
        printf("seng over---\n");
        pthread_mutex_unlock(&mutex);
        getchar();
    }
}

void *readHandler(void *fd)
{
    int n_read;
    int r_fd = *(int *)fd;
    char readbuf[128];
    while(1)
    {
        memset(readbuf,0,128);
        n_read = read(r_fd,readbuf,128);
        pthread_mutex_lock(&mutex);
        if(n_read > 0)
        {
            printf("---get:%s\n",readbuf);
        }
        pthread_mutex_unlock(&mutex);
    }
}



int main(int argc,char **argv)
{
    int c_fd;
    int n_read;
    pid_t pid;
    char readbuf[128];
    char msgn[128];

    struct sockaddr_in c_addr;

    pthread_mutex_init(&mutex,NULL);

    memset(&c_addr,0,sizeof(struct sockaddr_in));


    c_fd = socket(AF_INET,SOCK_STREAM,0);
    if(c_fd == -1)
    {
        perror("socket");
        exit(-1);
    }
    
    c_addr.sin_family = AF_INET;
    c_addr.sin_port = htons(atoi(argv[2]));
    inet_aton(argv[1],&c_addr.sin_addr);

    connect(c_fd,(struct sockaddr *)&c_addr,sizeof(struct sockaddr));

    pthread_t newthread;
    pthread_create(&newthread,NULL,readHandler,(void *)&c_fd);
    writeHandler((void *)&c_fd);
    while(1);


    return 0;
}


运行结果及报错内容

编译无报错,运行后发现服务器无法与多客户端通信

img

我的解答思路和尝试过的方法

没有解题思路

我想要达到的结果

客户端发送消息服务器可以接收到,并且返回客户端序号在客户端显示

  • 写回答

2条回答 默认 最新

  • 於黾 2022-11-21 15:22
    关注

    accept之后,你要启动一个线程来与这个客户端通信,主线程则继续accept等待其他线程的接入
    不要在主线程里recieve,那不把线程堵塞了吗,其他线程没法接入了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • CSDN-Ada助手 CSDN-AI 官方账号 2022-11-21 18:05
    关注
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月29日
  • 已采纳回答 11月21日
  • 修改了问题 11月21日
  • 创建了问题 11月21日

悬赏问题

  • ¥15 Linux操作系统中的,管道通信问题
  • ¥15 请问这张multisim图的原理是什么,这是一个交通灯,是课程要求,明天要进行解析,但是我们组没一个人会,所以急要,今天要
  • ¥15 ansible tower 卡住
  • ¥15 等间距平面螺旋天线方程式
  • ¥15 通过链接访问,显示514或不是私密连接
  • ¥100 系统自动弹窗,键盘一接上就会
  • ¥50 股票交易系统设计(sql语言)
  • ¥15 调制识别中这几个数据集的文献分别是什么?
  • ¥15 使用c语言对日志文件处理
  • ¥15 请大家看看报错原因,为啥会这样