SOCKET 多线程双向通信

谭池暝
2023-12-01

 Server.c

#include <stdio.h>
#include <winsock2.h>
#include <process.h>
#include <pthread.h>
#pragma comment (lib, "ws2_32.lib")  //加载 ws2_32.dll
void *sendmess(){
	printf("server send ...\n");
	SOCKET server = socket(AF_INET,SOCK_STREAM,0);
	SOCKADDR_IN server_add;
	server_add.sin_family = AF_INET;
	server_add.sin_addr.S_un.S_addr = htonl(ADDR_ANY);
	server_add.sin_port = htons(5000);
	bind(server,(SOCKADDR*)&server_add,sizeof(SOCKADDR));
	listen(server,20);
	SOCKADDR_IN client;
	int len = sizeof(client);
	char buff[100];
	SOCKET server_gin = accept(server,(SOCKADDR*)&client,&len);
	while(1){
		//printf(":");
		//scanf("%s",buff);
		gets(buff);
		send(server_gin,buff,100,0);
	}
	closesocket(server_gin);
	closesocket(server);
	
}
void *recvmess(){
	printf("server recv ...\n");
	SOCKET server = socket(AF_INET,SOCK_STREAM,0);
	SOCKADDR_IN server_add;
	server_add.sin_family = AF_INET;
	server_add.sin_addr.S_un.S_addr = htonl(ADDR_ANY);
	server_add.sin_port = htons(6000);
	bind(server,(SOCKADDR*)&server_add,sizeof(SOCKADDR));
	listen(server,20);
	SOCKADDR_IN client;
	int len = sizeof(client);
	SOCKET server_gin = accept(server,(SOCKADDR*)&client,&len);
	int error;
	char buff[100];
	while(1){
		error = recv(server_gin,buff,100,0);
		if(error!=0) printf("%s\n",buff);
	}
	closesocket(server_gin);
	closesocket(server);
}
int main(){
	WSADATA wsaData;
	int error = WSAStartup( MAKEWORD(2, 2), &wsaData);
	if(!error) {printf("初始化成功.\n");}
	
	pthread_t t1,t2;
	pthread_create(&t1,NULL,sendmess,NULL);
	pthread_create(&t2,NULL,recvmess,NULL);
	pthread_join(t1,NULL);
	pthread_join(t2,NULL);
	/*
	_beginthread(sendmess,0,NULL);
	_beginthread(recvmess,0,NULL);
	*/
	//while(1);
	return 0;
}

Client.c

#include <stdio.h>
#include <stdlib.h>
#include <WinSock2.h>
#include <pthread.h>
#pragma comment(lib, "ws2_32.lib")  //加载 ws2_32.dll
void *recvmess(){
	printf("client recv ...\n");
	SOCKET client = socket(AF_INET,SOCK_STREAM,0);
	SOCKADDR_IN server_add;
	server_add.sin_family = AF_INET;
	server_add.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
	server_add.sin_port = htons(5000);
	int flag = connect(client,(SOCKADDR*)&server_add,sizeof(server_add));
	if(flag==-1) {printf("recvclient 连接失败\n");}
	else {printf("recvclient 连接成功\n");}
	int error;
	char buff[100];
	while(1){
		error = recv(client,buff,100,0);
		if(error!=0) printf("%s\n",buff);
	}
	closesocket(client);
}
void *sendmess(){
	printf("client send ...\n");
	SOCKET client = socket(AF_INET,SOCK_STREAM,0);
	SOCKADDR_IN server_add;
	server_add.sin_family = AF_INET;
	server_add.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
	server_add.sin_port = htons(6000);
	int flag = connect(client,(SOCKADDR*)&server_add,sizeof(server_add));
	if(flag == -1) printf("sendclient 连接失败\n");
	else printf("sendclient连接成功\n");
	char buff[100];
	while(1){
		//printf(":");
		//scanf("%s",buff);
		gets(buff);
		send(client,buff,100,0);
	}
	closesocket(client);
}
int main(){
	//初始化DLL
	WSADATA wsaData;
	WSAStartup(MAKEWORD(2, 2), &wsaData);
	pthread_t t1,t2;
	pthread_create(&t2,NULL,recvmess,NULL);
	pthread_create(&t1,NULL,sendmess,NULL);
	pthread_join(t1,NULL);
	pthread_join(t2,NULL);
	//while(1);
	return 0;
}

 

 类似资料: