Linux C 语言 Socket 聊天程序(一对一)收发消息采用多线程

运行截图:?

Linux C 语言 Socket 聊天程序(一对一)收发消息采用多线程_第1张图片

温馨提示:

使用 gcc 编译时应该用下面命令:?(加入多线程库)

gcc server.c -o server -lpthread

服务端代码:?

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define BUFSIZE 1024

char buffer_send[BUFSIZE] = "connect stablish!";
char buffer_recv[BUFSIZE] = {0};

// send to client
void * sendMsg(void *socket){
	int * client_socket = (int *)socket;
	while(1){
		// send data to client
		scanf("%s", buffer_send);
		send(*client_socket, buffer_send, strlen(buffer_send), 0);

		// is exit
		if(!strncasecmp(buffer_send, "quit", 4)){
			break;
		}
	}
	pthread_exit(NULL);
}

// recv from client
void * recvMsg(void *socket){
	int * client_socket = (int *)socket;
	while(1){
		// clear buffer_recv
		bzero(buffer_recv, BUFSIZE);
		
		// read data from client
		if(recv(*client_socket, buffer_recv, BUFSIZE, 0) > 0){
			printf("Message from client:%s\n", buffer_recv);
		}

		// is exit
		if(!strncasecmp(buffer_send, "quit", 4)){
			break;
		}
	}
	pthread_exit(NULL);
}

int main(){
	// create socket
	int server_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	
	// bind ip port to socket
	struct sockaddr_in server_addr;
	memset(&server_addr, 0, sizeof(server_addr));// use zero fill
	server_addr.sin_family = AF_INET;// use IPV4 address
	server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");// ip
	server_addr.sin_port = htons(6666); // port
	bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr));

	// listen
	listen(server_socket, 20);
	
	// accept client request
	struct sockaddr_in client_addr;
	socklen_t client_addr_size = sizeof(client_addr);
	int client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &client_addr_size);

	// send stablish info to client
	send(client_socket, buffer_send, strlen(buffer_send), 0);
	
	// create send and recv thread
	pthread_t send_thread, recv_thread;
	
	pthread_create(&send_thread, NULL, sendMsg,(void *)&client_socket);
	pthread_create(&recv_thread, NULL, recvMsg,(void *)&client_socket);
	
	// wait send and recv thread over
	pthread_join(send_thread, NULL);
	pthread_join(recv_thread, NULL);
	
	// close
	close(client_socket);
	close(server_socket);

	return 0;
}

客户端代码:?

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define BUFSIZE 1024

char buffer_send[BUFSIZE] = {0};
char buffer_recv[BUFSIZE] = {0};

// send to server
void * sendMsg(void *socket){
	int * client_socket = (int *)socket;
	while(1){
		// send data to server
		scanf("%s", buffer_send);
		send(*client_socket, buffer_send, strlen(buffer_send), 0);

		// is exit
		if(!strncasecmp(buffer_send, "quit", 4)){
			break;
		}
	}
	pthread_exit(NULL);
}

// recv from server
void * recvMsg(void *socket){
	int * client_socket = (int *)socket;
	while(1){
		// clear buffer_recv
		bzero(buffer_recv, BUFSIZE);
		
		// read data from client
		if(recv(*client_socket, buffer_recv, BUFSIZE, 0) > 0){
			printf("Message from server:%s\n", buffer_recv);
		}
		// is exit
		if(!strncasecmp(buffer_send, "quit", 4)){
			break;
		}
	}
	pthread_exit(NULL);
}

int main(){
	// create socket
	int client_socket = socket(AF_INET, SOCK_STREAM, 0);
	
	// requset server
	struct sockaddr_in server_addr;
	memset(&server_addr, 0, sizeof(server_addr));// fill zero
	server_addr.sin_family = AF_INET;// use IPV4 address
	server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");// ip
	server_addr.sin_port = htons(6666);// port
	connect(client_socket, (struct sockaddr*)&server_addr, sizeof(server_addr));
	
	// clear buffer_recv
	bzero(buffer_recv, BUFSIZE);			
	
	// recv data from server
	if(recv(client_socket, buffer_recv, BUFSIZE, 0) > 0){
		printf("Message from server:%s\n", buffer_recv);
	}

	// create send and recv thread
	pthread_t send_thread, recv_thread;
	
	pthread_create(&send_thread, NULL, sendMsg, (void *)&client_socket);
	pthread_create(&recv_thread, NULL, recvMsg, (void *)&client_socket);

	// wait send and recv thread over
	pthread_join(send_thread, NULL);
	pthread_join(recv_thread, NULL);
	
	// close
	close(client_socket);
	
	return 0;
}

 

你可能感兴趣的:(linux)