gpt4 book ai didi

c - c linux中客户端服务器聊天应用程序中的多线程

转载 作者:太空宇宙 更新时间:2023-11-04 06:27:12 25 4
gpt4 key购买 nike

我想使用 linux 在 c 中创建一个客户端服务器聊天应用程序。我想在客户端和服务器程序中创建两个线程。一个用于发送,另一个用于接收 .. 我是线程的新手 .. 请让我知道我应该如何创建它?这是我的服务器代码..

#include <stdlib.h>
#include <stdio.h>
#include <string.h> //strlen
#include <sys/socket.h>
#include <arpa/inet.h> //inet_addr
#include <unistd.h> //write
#define port 8877


int main(int argc , char *argv[])
{
int socket_desc , client_sock , c , client_reply;
struct sockaddr_in server , client;
char client_message[5000];
char repltocli[6000];
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);

//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons( port);

//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 , 9);

//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);

//accept connection from an incoming client
client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (client_sock < 0)
{
perror("accept failed");
return 1;
}
puts("Connection accepted");

puts("press ctrl+c to terminate the application");

while(1)
{
//Receive a message from client
recv(client_sock , client_message , 5000 , 0);

puts("messege recived from client :");
puts(client_message);

memset(client_message, 0, sizeof(client_message));

printf("enter your message : ");
fgets(repltocli, 6000,stdin);

//Send some data
send(client_sock, repltocli , strlen(repltocli) , 0) ;

memset(repltocli, 0, sizeof(repltocli));


}

return 0;
}

最佳答案

为了处理线程你需要创建thread functions ,返回类型应该是 void * 并且有/没有参数。但是如果你使用参数,参数应该是 void *

接收消息的线程-

void *receive_message(){
while(1){
//Receive a message from client
recv(client_sock , client_message , 5000 , 0);
//puts("messege recived from client :"); // Comment this line out. Else it will annoye the user by printing every time
puts(client_message);

memset(client_message, 0, sizeof(client_message));
}
}

发送消息的线程-

void *send_message(){
while(1){
// printf("enter your message : "); // Comment this line out. Else it will annoye the user by printing every time
fgets(repltocli, 6000,stdin);
//Send some data
send(client_sock, repltocli , strlen(repltocli) , 0) ;

memset(repltocli, 0, sizeof(repltocli));
}
}

并全局声明repltocli数组,client_sockclient_message数组,因为你的线程也需要它!

在您的 main() 中为线程声明两个 pthread_d 变量-

int main(){

pthread_d thread_send, thread_recv;

// Do your stuff like socket, bind, listen and accept!

// Create these two threads and make sure that your main program should be alive-

pthread_create(&thread_send, NULL, send_message, NULL);
pthread_create(&thread_recv, NULL, receive_message, NULL);

while(1); // Press ctrl + C to terminate!
return 0;
}

对另一边也做同样的事情。

关于c - c linux中客户端服务器聊天应用程序中的多线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25542887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com