简单的Linux C/S套接字与pthread

时间:2021-04-02 16:14:28

so i would like to connect several clients to server with socket under linux kernel 2.6.x. when a client is connected,the server will send a welcome message to the client,in a manner of displaying on the standard output or anything.so when the connection is on,one thread is given to a function that may deliver welcome message to the very client.when i was using another processor to accomplish this job,it is just fine.however,when i switched to pthread way,the clients could not get anything from the server.at this point,i checked netstat,the connection was still on.so there comes my code:

因此,我希望在linux内核2.6.x下将几个客户机连接到服务器。当客户端被连接时,服务器将向客户端发送一条欢迎消息,其方式类似于在标准输出或任何东西上显示。因此,当连接处于打开状态时,一个线程被分配给一个函数,该函数可以将欢迎消息传递给这个客户端。当我使用另一个处理器来完成这项工作时,这是可以的。但是,当我切换到pthread方式时,客户端无法从服务器获取任何信息。此时,我检查了netstat,连接仍然处于打开状态。我的密码是:

client.c

client.c

// to make long story short,
// i'll just omit most of declarations and stuff that is irrelavent to the problem
#define PORT 12345
#define IP_ADDR "127.0.0.1"
void *process_to_server(int socket);
int main(){
   // i'll skip building socket for your convinence
   process_to_server(socket_server);

}
void *process_to_server(int socket){
   ssize_t size=0;
   char buffer[1024];
   size=read(socket,buffer,1024);
   write(1,buffer,size+1);// output the welcome message to the client's standard output
}

server.c

server.c

#define PORT 12345
void *process_to_client(int socket);
int main(){
   // same spirit as above
   while(1){
     int addrlen=sizeof(struct sockaddr);
     socket_client = accept(socket_server,(struct sockaddr*)&client_addr,&addrlen);
     if(socket_client<0) continue;

     pthread_t pth;
     close(socket_server);
     pthread_create(&pth,NULL,(void*)&process_to_client,&socket_client);
     }
}

void *process_to_client(int socket){
    char buffer[1024];
    sprintf(buffer,"Hello There\n");
    write(socket,buffer,1024);
}

2 个解决方案

#1


1  

You are transferring &socket_client to worker thread, but using the thread function parameter as if it was socket_client

您正在将&socket_client转移到工作线程,但是使用thread函数参数时,就好像它是socket_client一样

You can do this:

你可以这样做:

int main() {
/*...*/
pthread_create(&pth,NULL,(void*)&process_to_client,(void*)socket_client);
/*...*/
}

void *process_to_client(void* pvsocket) {
int socket = (int) pvsocket;
/* same as before */
}

Also closing the listening socket will keep the accepted connection open. You will be able to continue communication with connected client but further incoming connection will not be accepted. The worker thread should close socket_client when communication is done.

同时关闭监听套接字将保持所接受的连接打开。您将能够继续与连接的客户端通信,但是进一步的连接将不被接受。当通信完成时,工作线程应该关闭socket_client。

#2


1  

The argument to your thread function is a pointer, but you use it directly as an integer. Change your thread function like this:

线程函数的参数是一个指针,但是您可以直接将它用作一个整数。改变你的线程函数如下:

void *process_to_client(void *socket_pointer){
    int socket = *(int *) socket_pointer;
    char buffer[1024];
    sprintf(buffer,"Hello There\n");
    write(socket,buffer,1024);
}

#1


1  

You are transferring &socket_client to worker thread, but using the thread function parameter as if it was socket_client

您正在将&socket_client转移到工作线程,但是使用thread函数参数时,就好像它是socket_client一样

You can do this:

你可以这样做:

int main() {
/*...*/
pthread_create(&pth,NULL,(void*)&process_to_client,(void*)socket_client);
/*...*/
}

void *process_to_client(void* pvsocket) {
int socket = (int) pvsocket;
/* same as before */
}

Also closing the listening socket will keep the accepted connection open. You will be able to continue communication with connected client but further incoming connection will not be accepted. The worker thread should close socket_client when communication is done.

同时关闭监听套接字将保持所接受的连接打开。您将能够继续与连接的客户端通信,但是进一步的连接将不被接受。当通信完成时,工作线程应该关闭socket_client。

#2


1  

The argument to your thread function is a pointer, but you use it directly as an integer. Change your thread function like this:

线程函数的参数是一个指针,但是您可以直接将它用作一个整数。改变你的线程函数如下:

void *process_to_client(void *socket_pointer){
    int socket = *(int *) socket_pointer;
    char buffer[1024];
    sprintf(buffer,"Hello There\n");
    write(socket,buffer,1024);
}