我试图实现客户端与客户端之间的通信,并与它们之间的服务器通信。服务器的功能是,当客户端假设客户端A向服务器发送消息时,服务器应该将该消息转发给其他客户端客户端B.当客户端B向应该转发的消息发送消息时到客户端A.这个程序只涉及两个客户端。 当我执行的代码我得到的错误是,它说:使用select()函数进行客户端与客户端通信c
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()'调用中的每一个都应该跟随一个清理和返回。 – EJP
客户端套接字如何被存储到“clientsocks”中? – immibis
@EJP你的意思是我应该打破循环,并返回? –