c中使用TCP的多用户聊天服务器。如何将数据发送到多个客户端?

问题描述 投票:0回答:2

我想在C中使用TCP创建一个多用户聊天服务器,当客户端向服务器发送消息时。它将被发送给所有其他客户。

我找不到任何方法将数据发送到多个客户端,是否有一些方法迭代连接到套接字的所有客户端或某种方法向所有客户端广播消息。

这是我的代码

server.c

#include <stdio.h>
#include <sys/socket.h> //for sock()
#include <string.h>//for using memset
#include <arpa/inet.h>// for inet_addr()
#include <unistd.h>//for using write() function


int main(){


int sock=0, client_conn=0,counter=0,pid;
char data_send[1024],data_received[1024];
struct sockaddr_in ServerIp;
sock = socket(AF_INET, SOCK_STREAM, 0);


 memset(&ServerIp,'0',sizeof(ServerIp) );
 ServerIp.sin_family = AF_INET;
 ServerIp.sin_port = htons(1234);
 ServerIp.sin_addr . s_addr = inet_addr("127.0.0.1");


 if(   bind( sock,(struct sockaddr* )&ServerIp, sizeof(ServerIp)) == -1 )
    printf("\n Socket binding failed ");

if( listen(sock,20) == -1) 
    printf("Error\n");
else    
    printf("\n Server started\n");

for(;;){
    label:

    client_conn = accept( sock, (struct sockaddr*)NULL, NULL);
    pid = fork();
    if( pid < 0 )
        printf("\n Process creation failed ");
    else if( pid > 0 ){
        counter++;
        ///close(client_conn); 
        goto label;
    }   
    else{
        counter++;
        if( recv(client_conn, data_received, 1024, 0 ) == -1 )
            printf(" Error !! cannot get response \n");

        printf(" data from client is %s\n",data_received);
        sprintf(data_send," Hi client %d !! from server ",counter);
        write( client_conn, data_send, sizeof(data_send) );

    }

}   


close( sock );

return 0;
}

client.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h> //for sock()
#include <string.h>//for using memset
#include <arpa/inet.h>// for inet_addr()
#include <unistd.h>//for using write() function

int main(){
enter code here
    char data_received[1024],data_send[1024];
int sock=0;
struct sockaddr_in ServerIp;


if( (sock = socket(AF_INET, SOCK_STREAM ,0 )) == -1 )
    printf(" socket creation failed ");


 ServerIp.sin_family = AF_INET;
 ServerIp.sin_port = htons(1234);
 ServerIp.sin_addr . s_addr = inet_addr("127.0.0.1");   


 if( (connect( sock, (struct sockaddr *)&ServerIp, sizeof(ServerIp) )) == -1 ){
    printf("\n connection to the socket failed ");
    exit(0);
}
 else
    printf("\n connected to socket \n");

strcpy(data_send,"hello server");
if( send(sock,data_send,sizeof(data_send),0) == -1 )
    printf("sending failed ");


while(1){
 printf("\n waiting for respose !! \n");    


 if( recv(sock, data_received, 1024, 0 ) == -1 )
    printf(" Error !! cannot get response |n");
 else
    printf("\n Response Received is : %s", data_received );
    }

}

有什么建议??

c tcp client-server
2个回答
1
投票

通过保持所有连接的客户端及其文件描述符的列表更新来完成广播,并通过迭代它来向每个客户端发送消息(您不能像在UDP中那样在TCP中进行广播,因为TCP是连接的协议,而UDP不是)。


如果它可以有任何用途,这里是一个带有多用户TCP服务器的仓库,我在C中为学校项目制作:https://github.com/DatPenguin/TCPServer。它不是聊天,但TCP部分应该是相同的。

client文件夹中是客户端的代码,而服务器端在server文件夹中。


0
投票

我发现使用pthread做一个简单易用的方法。这是代码

服务器代码

#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <pthread.h>

pthread_mutex_t mutex;
int clients[20];
int n=0;

void sendtoall(char *msg,int curr){
    int i;
    pthread_mutex_lock(&mutex);
    for(i = 0; i < n; i++) {
        if(clients[i] != curr) {
            if(send(clients[i],msg,strlen(msg),0) < 0) {
                printf("sending failure \n");
                continue;
            }
        }
    }
    pthread_mutex_unlock(&mutex);
}

void *recvmg(void *client_sock){
    int sock = *((int *)client_sock);
    char msg[500];
    int len;
    while((len = recv(sock,msg,500,0)) > 0) {
        msg[len] = '\0';
        sendtoall(msg,sock);
    }

}

int main(){
    struct sockaddr_in ServerIp;
    pthread_t recvt;
    int sock=0 , Client_sock=0;

    ServerIp.sin_family = AF_INET;
    ServerIp.sin_port = htons(1234);
    ServerIp.sin_addr.s_addr = inet_addr("127.0.0.1");
    sock = socket( AF_INET , SOCK_STREAM, 0 );
    if( bind( sock, (struct sockaddr *)&ServerIp, sizeof(ServerIp)) == -1 )
        printf("cannot bind, error!! \n");
    else
        printf("Server Started\n");

    if( listen( sock ,20 ) == -1 )
        printf("listening failed \n");

    while(1){
        if( (Client_sock = accept(sock, (struct sockaddr *)NULL,NULL)) < 0 )
            printf("accept failed  \n");
        pthread_mutex_lock(&mutex);
        clients[n]= Client_sock;
        n++;
        // creating a thread for each client 
        pthread_create(&recvt,NULL,(void *)recvmg,&Client_sock);
        pthread_mutex_unlock(&mutex);
    }
    return 0; 

}

客户代码

#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <pthread.h>

char msg[500];

void *recvmg(void *my_sock)
{
    int sock = *((int *)my_sock);
    int len;
    // client thread always ready to receive message
    while((len = recv(sock,msg,500,0)) > 0) {
        msg[len] = '\0';
        fputs(msg,stdout);
    }
}

int main(int argc,char *argv[]){
    pthread_t recvt;
    int len;
    int sock;
    char send_msg[500];
    struct sockaddr_in ServerIp;
    char client_name[100];
    strcpy(client_name, argv[1]);
    sock = socket( AF_INET, SOCK_STREAM,0);
    ServerIp.sin_port = htons(1234);
    ServerIp.sin_family= AF_INET;
    ServerIp.sin_addr.s_addr = inet_addr("127.0.0.1");
    if( (connect( sock ,(struct sockaddr *)&ServerIp,sizeof(ServerIp))) == -1 )
        printf("\n connection to socket failed \n");

    //creating a client thread which is always waiting for a message
    pthread_create(&recvt,NULL,(void *)recvmg,&sock);

    //ready to read a message from console
    while(fgets(msg,500,stdin) > 0) {
        strcpy(send_msg,client_name);
        strcat(send_msg,":");
        strcat(send_msg,msg);
        len = write(sock,send_msg,strlen(send_msg));
        if(len < 0) 
            printf("\n message not sent \n");
    }

    //thread is closed
    pthread_join(recvt,NULL);
    close(sock);
    return 0;
}

要查看输出,请参阅https://codingile.com/multiuser-chat-server-in-c/

© www.soinside.com 2019 - 2024. All rights reserved.