在使用C语言的套接字编程中的select()之间,如何通过服务器进行客户端到客户端的通信?

我已经编写了一个程序,用于使用C语言的SELECT()将多个客户端连接到一台服务器。现在,我想通过服务器实现客户端到客户端的通信。

我以为我只是将消息通过服务器从Client_A传递到Client_B,但这没有发生,因为仅当该客户端向服务器发送一些数据时,服务器才连接到一个客户端。

但是我不确定如何实现?那么,有人可以帮我吗?提前致谢。

服务器和客户端的代码在下面供参考。

---------------------------- SERVER.C ------------------- -----------------

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#include<sys/types.h>
#include<sys/socket.h>
#include<sys/time.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<errno.h>

int main(){

    int server_sock, client_sock[30], max_sd, sd, new_sock,activity;
    char msg[]="Welcome to the server.";
    char Cli_Msg[256], Ser_Msg[256];

    struct sockaddr_in server_addr;

    //Initialize all the client sockets to zero............
    for(int i=0;i<30;i++)
        client_sock[i]=0;

    //Create a server socket............
    server_sock=socket(AF_INET,SOCK_STREAM,0);
    if(server_sock<0){
        perror("Socket Creation Failed");
        exit(1);
    }

    //Assign IP address
    server_addr.sin_family=AF_INET;
    server_addr.sin_port=htons(8000);
    server_addr.sin_addr.s_addr= INADDR_ANY;

    printf("Waiting for connection.....................................\n\n");
    if(bind(server_sock,(struct sockaddr*)&server_addr,sizeof(server_addr))<0){
        perror("Bind Failed.");
        exit(1);
    }

    if(listen(server_sock,5)<0){
        perror("Listen");
        exit(1);
    }



    //Creation of Socket Drescriptor.........
    fd_set ready_sock;

    while(1){

        FD_ZERO(&ready_sock);
        FD_SET(server_sock,&ready_sock);

        max_sd=server_sock;
        for(int i=0;i<30;i++){
            sd=client_sock[i];
            if(sd>0)
                FD_SET(sd,&ready_sock);
            if(sd>max_sd)
                max_sd=sd;

        }

        activity=select(max_sd+1,&ready_sock,NULL,NULL,NULL);
        if(activity<0){
            perror("Select Failed.");
            exit(1);
        }
        //If server socket is getting signal then that's an incoming connection..............
        if(FD_ISSET(server_sock,&ready_sock)){

            new_sock=accept(server_sock,NULL,NULL);
            if(new_sock<0){
                perror("Accept.");
                exit(1);
            }
            send(new_sock,msg,sizeof(msg),0);
            //Adding new socket to the array of sockets
            for(int i=0;i<30;i++){
                if(client_sock[i]==0){
                    client_sock[i]=new_sock;
                    break;
                }
            }
        }

        else{
            for(int i=0;i<30;i++){
                sd=client_sock[i];
                if(FD_ISSET(sd,&ready_sock)){

                        recv(sd,&Cli_Msg,sizeof(Cli_Msg),0);
                        printf("Client: ");
                        printf("%s",Cli_Msg);
                        printf("\n");
                        printf("Server: ");
                        fgets(Ser_Msg,256,stdin);
                        send(sd,Ser_Msg,sizeof(Ser_Msg),0);
                        if(strcmp(Ser_Msg,Cli_Msg)==0){
                            printf("Connection end...\n");
                            close(sd);
                            client_sock[i]=0;
                            break;
                        }       
                }
            }   

        }

    }

    return 0;
}

--------------------------------- CLIENT.C -------------- ------------------

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<sys/time.h>
#include<sys/select.h>

#define PORT_NO 8000

int main(){

    int client_sock;
    struct sockaddr_in ser_addr;
    char msg[256],Cli_Msg[256],Ser_Msg[256];

    client_sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if(client_sock<0)
        printf("Error in creating socket.\n");

    ser_addr.sin_family=AF_INET;
    ser_addr.sin_port=htons(PORT_NO);
    ser_addr.sin_addr.s_addr=INADDR_ANY;

    int conn_stat;
    conn_stat=connect(client_sock,(struct sockaddr*)&ser_addr,sizeof(ser_addr));

    if(conn_stat<0){
        printf("Error in connecting the server.\n");
        exit(1);
    }
    recv(client_sock, &msg,sizeof(msg),0);

    printf("The data sent from the server:\n");
    printf("%s\n",msg);
    printf("You can start the conversation.\n");

    while(1){
        printf("Client : ");
        fgets(Cli_Msg,256,stdin);
        send(client_sock,Cli_Msg,sizeof(Cli_Msg),0);
        recv(client_sock,&Ser_Msg,sizeof(Ser_Msg),0);
        printf("Server : ");
        printf("%s\n",Ser_Msg);
        if(strcmp(Cli_Msg, Ser_Msg)==0){
            printf("Connection end.\n");
            close(client_sock);
            exit(1);
        }
    }

    close(client_sock);
    printf("Connection ended.\n");
    return 0;


}
评论