这个套接字服务器有一些错误,很可能与pthread有关。如果这继续接受客户端连接,一段时间后它将开始挂起。它似乎不是内存泄漏,因为程序占用的内存保持不变,但是当我连接到telnet客户端时,它只会挂起并且什么都不做。
它仍然会到达 puts(“Handler assigned”); 部分,但它什么也没做。有什么想法会导致这种情况吗?
/*
C socket server example, handles multiple clients using threads
*/
#include<stdio.h>
#include<string.h> //strlen
#include<stdlib.h> //strlen
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h> //write
#include<pthread.h> //for threading , link with lpthread
//the thread function
void *connection_handler(void *);
int main(int argc , char *argv[])
{
int socket_desc , client_sock , c , *new_sock;
struct sockaddr_in server , client;
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
puts("Socket created");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );
//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
//print the error message
perror("bind failed. Error");
return 1;
}
puts("bind done");
//Listen
listen(socket_desc , 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
while( (client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)) )
{
puts("Connection accepted");
pthread_t sniffer_thread;
new_sock = malloc(1);
*new_sock = client_sock;
if( pthread_create( &sniffer_thread , NULL , connection_handler , (void*) new_sock) < 0)
{
perror("could not create thread");
return 1;
}
//Now join the thread , so that we dont terminate before the thread
//pthread_join( sniffer_thread , NULL);
puts("Handler assigned");
}
if (client_sock < 0)
{
perror("accept failed");
return 1;
}
return 0;
}
/*
* This will handle connection for each client
* */
void *connection_handler(void *socket_desc)
{
//Get the socket descriptor
int sock = *(int*)socket_desc;
int read_size;
char *message , client_message[2000];
//Send some messages to the client
message = "Greetings! I am your connection handler\n";
write(sock , message , strlen(message));
message = "Now type something and i shall repeat what you type \n";
write(sock , message , strlen(message));
//Receive a message from client
while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 )
{
//Send the message back to client
write(sock , client_message , strlen(client_message));
}
if(read_size == 0)
{
puts("Client disconnected");
fflush(stdout);
}
else if(read_size == -1)
{
perror("recv failed");
}
//Free the socket pointer
free(socket_desc);
return 0;
}
答案 0 :(得分:4)
sendbuf()
函数,该函数在循环中写入套接字并仅在缓冲区的所有字节都已发送时返回。pthread_create()
将线程创建为已分离。搜索使用pthread_attr_setdetachstate()
函数的教程,了解如何执行此操作。pthread_detach()
后告诉pthread库您不想加入该线程。setsockopt()
在服务器中设置SO_SNDTIMEO
和SO_RCVTIMEO
套接字选项的发送/接收超时,并可选择使用SO_SNDBUF
{{调整发送/接收缓冲区大小1}}如果你想要的选项,但我不会在没有理由的情况下设置后两个选项。SO_RCVBUF
代替read_size
。假设收到的块为零终止是错误的,即使客户端已发送零终止消息,因为您可能会将其作为碎片收到。