【问题标题】:Client to Client communication using select() function in c使用 c 中的 select() 函数进行客户端到客户端的通信
【发布时间】:2016-12-08 13:34:14
【问题描述】:

我试图实现客户端到客户端的通信,并在它们之间使用服务器。服务器的功能是,当客户端假设客户端 A 向服务器发送消息时,服务器应该将该消息转发给另一个客户端,客户端 B。同样,当客户端 B 向服务器发送消息时,它应该被转发给客户 A。这个程序只涉及两个客户。 我执行代码时遇到的错误是它说:

Socket Operation on Non-socket

当收到的来自客户端 A 的消息转发给客户端 B 时,我收到此错误。我认为问题是由于将接收到的客户端 B 的地址存储到客户端 A 的地址。我不确定。

到目前为止我的服务器代码。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <sys/time.h>
#define SERVER_PORT 5009


int main(){
unsigned int sockfd, c,c1,c2, clientlen, clientfd;
struct sockaddr_in server;
struct sockaddr_in client1;
int clientsocks[2];
char rmsg1[100], msg1[100],rmsg2[100], msg2[100];
char w_msg[] = "Connection to server established";

fd_set readfds; // For temp file descriptor list.

clientsocks[0] = 0 ;
clientsocks[1] = 0 ;
//Socket Creation Process.
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if( sockfd < 0){
    perror("Socket cannot be created");
}

//For reusing the socket.
int on = 1;
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
//Socket address
bzero((char *) &server, sizeof(server));
server.sin_family = AF_INET; // IPv4 internet Protocols
inet_aton("127.0.0.1", &server.sin_addr);
server.sin_port = htons(SERVER_PORT);

//Binding socket to address.
if (bind( sockfd, (struct sockaddr*)&server, sizeof (server) ) < 0 ){
    perror("Bind Error");
    exit(EXIT_FAILURE);
}

//Listen to accept connection.
if( listen(sockfd, SOMAXCONN) < 0 ){
    perror("Error in Listen");
    exit(EXIT_FAILURE);
}

unsigned int new_sock;
clientlen =sizeof(client1);
int activity;
while(1){

    //Clear socket set.
    FD_ZERO(&readfds);

    //Adding main sockfd to the socket set.
    FD_SET(sockfd, &readfds);
    unsigned int max_sd = sockfd;

    //Add child sockets to set.
    for(int i=0 ; i<2; i++){
           c = clientsocks[i];
        if(c > 0)
            FD_SET(c, &readfds);
        if(c > max_sd)
            max_sd = c;
    }

    activity = select(max_sd + 1, &readfds, NULL, NULL, NULL);
    if(activity < 0){
        perror("Error in select()");
        exit(EXIT_FAILURE);
    }

    //Incoming connection when something happens on sockfd.
    if( FD_ISSET(sockfd, &readfds)){
        new_sock = accept(sockfd, (struct sockaddr *) &client1, &clientlen);
        if(new_sock > 0){
            for(int i=0; i<2; i++){
                if(clientsocks[i] == 0){
                    clientsocks[i] = new_sock;
                    break;
                }
            }
        }
        if(  new_sock < 0){
            perror("Error Accepting");
            exit(EXIT_FAILURE);
        }
        if( send(new_sock, w_msg, strlen(w_msg), 0) != strlen(w_msg)){
            perror("Welcome message");
            exit(EXIT_FAILURE);
        }
    c1 = clientsocks[0];
    c2 = clientsocks[1];
    FD_SET(c1, &readfds);
    FD_SET(c2, &readfds);
    }

    //Else if its not a new incoming connection.
    if(FD_ISSET(c1, &readfds)){
        if(recv(c1, rmsg1, 100, 0) < 0){
            perror("Receive 1");
            exit(EXIT_FAILURE);
        }
        printf("Client1 >> %s\n", rmsg1);
        //Forwarding to Client B.
        if( send(c2, rmsg1, 100, 0) < 0){
            perror("Error forwarding to 2");
            exit(EXIT_FAILURE);
        }
    }
    if(FD_ISSET(c2, &readfds)){
        if(recv(c2, rmsg2, 100, 0) < 0){
            perror("Receive 2");
            exit(EXIT_FAILURE);
        }
        printf("Client2 >> %s\n", rmsg2);
        if( send(c1, rmsg2, 100, 0) < 0 ){
            perror("Error Forwarding to 1");
            exit(EXIT_FAILURE);
        }
    }
}
close(sockfd);
return 1;
}

我的问题只涉及两个客户。如果您也能指出其他一些改进,我将不胜感激。

【问题讨论】:

  • 如果出现错误,让执行继续进行是不可接受的,就好像它没有发生一样。每个perror() 调用之后都应该进行清理和返回。
  • 客户端套接字如何存储到clientsocks
  • @EJP 你的意思是我应该打破循环并返回?
  • @immibis 我使用 clientsocks[i] 来存储正在接受的传入连接的套接字描述符。但我不确定如何将传入连接正确存储到 clientsocks 数组中。
  • @VishnuNK “我使用 clientsocks[i] 来存储正在接受的传入连接的套接字描述符” - 太好了!但是,在哪里您将它们存储在该数组中?因为我没看到。

标签: c sockets select client-server ipc


【解决方案1】:

您遇到了一些结构性问题,这些问题并不是 StackOverflow 成员真正需要解决的工作。

  1. 确定每个客户端是要连接到同一个套接字还是他们自己唯一的套接字,然后进行相应的监听。
  2. 如果您有一个共享连接,看起来您正在使用...共享连接我的意思是您正在侦听一个端口并执行多个接受,那么在给定接受之后您不能假设谁已经连接直到您从端口读取一些数据。您应该让每个客户端发送一些信息,以便您知道是 A 连接然后 B 还是 B 连接然后 A。
  3. 您的 setsockopt 周围的代码似乎您忘记了{}
  4. 保存客户端时,您可以使用来自客户端的信息来确定要使用的阵列槽。也许您有 100 个客户端,它们每个都连接然后发送一个 32 位字(其中包含它们的客户端编号,介于 1-100 之间)。
  5. 一旦您可以读取来自不同客户端的消息(现在可能希望将它们打印出来以便查看发生了什么),您就可以构建各种消息。目标是让客户端 A 能够向服务器请求客户端 B 的联系信息,以便 A 可以直接连接到 B 并向 B 发送消息。
  6. 或者,客户端 26 应该能够向服务器发送消息,而不是 #5,表明它是针对客户端 45 的,服务​​器应该能够检查已签入的客户端数组,然后发送消息到阵列插槽 45 中的客户端。
  7. 客户端需要具有唯一的 ID/编号,以便服务器可以将客户端 ID 映射到 clientarray 套接字。
  8. 其中一些错误最好直接处理而不是导致服务器退出。也许您可以关闭与导致错误的套接字索引关联的套接字。您的错误消息应该指出哪个客户端/套接字索引导致了错误。通常,您需要更多调试消息和消息中的更多信息。

【讨论】:

  • 抱歉回复晚了。但这是一个很棒的答案。下次我问的时候会处理这些事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-14
  • 1970-01-01
  • 2020-12-16
  • 1970-01-01
  • 1970-01-01
  • 2015-02-22
相关资源
最近更新 更多