问题
I am new to socket programming, so I'm sorry if this is a noob question.
I have a program in C for Linux, which connects two sockets from "server.c" to two "client.c"s with the help of fork(). It works just fine most of the time, but there are times when the second client gets stuck before the first recv() function. On these occasions, the server doesn't accept() the connection and I am confused as to what is truly happening and how I can fix it.
Since I don't know much about socket programming, any explanation could be useful.
Here is the codes for the server and the client:
server.c
int main(){
int server_socket, p1_socket, p2_socket;
int pid;
pid = fork();
if(pid<0)
printf("Fork failed.\n");
else if(pid==0){
//PLAYER 1
socket_creation_and_connection(&server_socket, &p1_socket);
connect_to_player(&p1_socket, 1);
}
else{
//PLAYER 2
socket_creation_and_connection(&server_socket, &p2_socket);
connect_to_player(&p2_socket, 2);
}
printf("Hello message sent from %d\n", pid);
return 0;
}
void connect_to_player(int *socket, int playerNo){
char buffer[256];
while(1){
send(*socket , (int *)&playerNo , sizeof(int) , 0);
recv(*socket, (int *)&buffer, 1024, 0);
printf("PlayerNo %d Message received from client %s\n", playerNo, buffer);
}
}
void socket_creation_and_connection (int *server_socket, int *new_socket){
struct sockaddr_in address;
int addrlen = sizeof(address);
printf("creating sth\n");
if ((*server_socket = socket(AF_INET, SOCK_STREAM, 0)) == 0){
perror("Failed to create socket.");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( PORT );
if (bind(*server_socket, (struct sockaddr *)&address, sizeof(address))<0){
perror("Failed to bind.");
exit(EXIT_FAILURE);
}
if (listen(*server_socket, 3)<0){
perror("Failed to listen.");
exit(EXIT_FAILURE);
}
if ((*new_socket = accept(*server_socket, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0){
perror("Failed to accept.");
exit(EXIT_FAILURE);
}
printf("finished creating sth\n");
}
client.c
int main(){
int client_socket = 0;
char message[256];
socket_creation_and_connection(&client_socket);
int intbuf;
while(1){
recv(client_socket, (int *)&intbuf, sizeof(int), 0);
if(intbuf == 1)
printf("Received message from Player1\n");
else
printf("Received message from Player2\n");
printf("Type a string\n");
scanf("%s", message);
send(client_socket, message , strlen(message) , 0 );
sleep(1);
}
return 0;
}
void socket_creation_and_connection(int *client_socket){
struct sockaddr_in address;
if ((*client_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Socket creation error \n");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_port = htons(PORT);
if (connect(*client_socket, (struct sockaddr *)&address, sizeof(address)) < 0)
{
printf("Connection Failed.\n");
exit(EXIT_FAILURE);
}
}
Edit: PORT is defined along with the libraries like this.
#define PORT 8080
来源:https://stackoverflow.com/questions/59591415/the-second-connection-in-two-client-socket-program-is-rarely-not-accepted-from-t