我试图让服务器(SENSORSERVER)和客户端(CGI)使用send()函数进行通信。围绕SENSORSERVER的第一个循环发送字符串" Hello world"正确地对CGI,但在while循环周围的第二个循环中,CGI在recv函数上没有正确接收。
SENSORSERVER代码
int main() {
pthread_mutex_init(&mutex, NULL);
int welcomeSocket, newSocket;
char buffer[1024];
struct sockaddr_in serverAddr;
struct sockaddr_storage serverStorage;
socklen_t addr_size;
//pthread_create(&t0, NULL ,background(),(void *)"");
//pthread_detach(t0);
//int pthread_join();
pid = fork();
if(pid == -1){
printf("failed to fork");
}
if(pid == 0){
pthread_create(&t0, NULL, background(), (void*)"");
pthread_detach(t0);
} else {
/*---- Create the socket. The three arguments are: ----*/
/* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */
while(1){
welcomeSocket = socket(PF_INET, SOCK_STREAM, 0);
/*---- Configure settings of the server address struct ----*/
/* Address family = Internet */
serverAddr.sin_family = AF_INET;
/* Set port number, using htons function to use proper byte order */
serverAddr.sin_port = htons(7891);
/* Set IP address to localhost */
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
/* Set all bits of the padding field to 0 */
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
/*---- Bind the address struct to the socket ----*/
bind(welcomeSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
/*---- Listen on the socket, with 5 max connection requests queued ----*/
if(listen(welcomeSocket, 0) == 0)
printf("Listening\n");
else
printf("Error\n");
/*---- Accept call creates a new socket for the incoming connection ----*/
addr_size = sizeof serverStorage;
newSocket = accept(welcomeSocket, (struct sockaddr *) &serverStorage, &addr_size);
/*---- Send message to the socket of the incoming connection ----*/
int er = pthread_mutex_trylock(&mutex);
if (er == 0)
{
strcpy(buffer,"Hello World\n");
send(newSocket, buffer, sizeof(buffer), 0);
close(newSocket);
close(welcomeSocket);
pthread_mutex_unlock(&mutex);
}
}
}
return EXIT_SUCCESS;
}
CGI客户端代码
char buf[1024];
char buf2[2024];
int main(void) {
int clientSocket;
char buffer[1024];
struct sockaddr_in serverAddr;
socklen_t addr_size;
/*---- Create the socket. The three arguments are: ----*/
/* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */
clientSocket = socket(PF_INET, SOCK_STREAM, 0);
/*---- Configure settings of the server address struct ----*/
/* Address family = Internet */
serverAddr.sin_family = AF_INET;
/* Set port number, using htons function to use proper byte order */
serverAddr.sin_port = htons(7891);
/* Set IP address to localhost */
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
/* Set all bits of the padding field to 0 */
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
/*---- Connect the socket to the server using the address struct ----*/
addr_size = sizeof serverAddr;
connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size);
/*---- Read the message from the server into the buffer ----*/
int er;
er = recv(clientSocket, buffer, sizeof(buffer), 0);
/*---- Print the received message ----*/
printf("Data received: %s",buffer);
//close(clientSocket);
puts("<p>Hello <b>CGI</b</p>");
puts("</BODY>");
puts("</HTML>");
return EXIT_SUCCESS;
}
SENSORSERVER将进入accept()函数,然后当CGI进入接收时它将继续并继续,一切似乎都很好。但是,我需要能够在SENSORSERVER运行时一次又一次地调用CGI,并且服务器可以向客户端发送消息。它只发送一次!
第一轮循环输出 -
Data received: Hello World
<p>Hello <b>CGI</b</p>
</BODY>
</HTML>
logout
第二轮循环 -
Data received:
<p>Hello <b>CGI</b</p>
</BODY>
</HTML>
logout
任何人都可以看到问题所在吗?
答案 0 :(得分:2)
问题是您的服务器只接受单个连接(单accept
次呼叫),然后在发送消息后退出。因此,第二个客户端运行将导致连接失败(不再有人在套接字上侦听)并打印空白消息(当您忽略错误代码时)。
如果您希望服务器能够处理多个连接,则需要在循环中进行accept
调用。您希望如何做到这一点取决于您希望如何处理连接。最简单的方法是发送消息,关闭接受的连接,然后循环:
while (1) { /* infinite loop */
/*---- Accept call creates a new socket for the incoming connection ----*/
addr_size = sizeof serverStorage;
newSocket = accept(welcomeSocket, (struct sockaddr *) &serverStorage, &addr_size);
if (newSocket < 0) {
perror("accept");
break; }
/*---- Send message to the socket of the incoming connection ----*/
int er = pthread_mutex_trylock(&mutex);
if (er == 0) {
strcpy(buffer,"Hello World\n");
send(newSocket, buffer, sizeof(buffer), 0);
close(newSocket);
pthread_mutex_unlock(&mutex);
} else {
/* mutex lock failed (busy?) -- need to do something */
strcpy(buffer,"Error occurred\n");
send(newSocket, buffer, sizeof(buffer), 0);
close(newSocket);
}
}
close(welcomeSocket);
如果你想对传入连接做更复杂的事情,你可能想要分叉一个进程或线程来处理它,而不是直接在循环中进行,因为第二个连接在第一个连接之后才能被接受。已被处理,循环返回到接受调用。