面向连接的socket通信

 Tcp_Socket通信流程(阻塞)

面向连接的socket通信_第1张图片

 

IDE环境:vc6.0

client.cpp

#include 
#include 
#include 
#include 
#include 
#pragma comment(lib,"ws2_32.lib")
#define buf_size 30
int main(){
	
	char recvbuf[buf_size];
	char sendbuf[buf_size];
	SOCKET sHost; 
	int retval;
	int retVal;
	//初始化
	WSADATA wsaData;
	if( WSAStartup(MAKEWORD(2,2),&wsaData)!=0){
		printf("WSAStarup 失败!");
		return 0;
	}
	
	//创建socket
	sHost = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	if(sHost == INVALID_SOCKET){
		printf("socket error!\n");
		WSACleanup();
		return -1;
	}
	//初始化服务器ip及其端口
	SOCKADDR_IN servaddr;
	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
	servaddr.sin_port = htons(8888);
	int servaddrlen = sizeof(servaddr);
	//连接服务器
	retval = connect(sHost,(LPSOCKADDR)&servaddr,sizeof(servaddr));
	Sleep(200);
	if(SOCKET_ERROR == retval){
		printf("connect failed!\n");
		closesocket(sHost);
		WSACleanup();
		return -1;
	}
	//循环
	while(1){
		//发送
		printf("input to send:");
		gets(sendbuf);
		retval = send(sHost,sendbuf,strlen(sendbuf),0);
		if(strcmp(sendbuf,"quit") == 0){
			printf("quit!\n");
			break;
		}
		//接收
		retval = recv(sHost,recvbuf,buf_size,0);
		if(retval == SOCKET_ERROR){
			printf("recv failed!\n");
			closesocket(sHost);
			WSACleanup();
			return -1;
			}
		printf("from server:%s\n",recvbuf);
	}
	closesocket(sHost);
	WSACleanup();
	system("pause");
	return 0;
}

server.cpp

#include 
#include 
#include 
#pragma comment(lib,"ws2_32.lib")
#define buf_size 30
int main(){
	
	char recvbuf[buf_size];
	char sendbuf[buf_size];
	SOCKET sServer; 
	SOCKET sClient;
	int retval;
	int retVal;
	//初始化
	WSADATA wsaData;
	if( WSAStartup(MAKEWORD(2,2),&wsaData)!=0){
		printf("WSAStarup 无法初始化!");
		return 0;
	}

	//创建套接字sServer
	sServer = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	if(sServer == INVALID_SOCKET){
		printf("socket error!\n");
		WSACleanup();
		return -1;
	}

	//设置监听端口,地址
	SOCKADDR_IN addserv;
	addserv.sin_family = AF_INET;
	addserv.sin_port = htons(8888);
	addserv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
	
	//绑定
	retVal = bind(sServer,(const struct sockaddr*)&addserv,sizeof(SOCKADDR_IN));
	if(retVal == SOCKET_ERROR){
		printf("bind failed!\n");
		closesocket(sServer);
		WSACleanup();
		return -1;
	}

	//监听

	retVal = listen(sServer,3);
	if(retVal == SOCKET_ERROR){
		printf("listen failed!\n");
		closesocket(sServer);
		WSACleanup();
		return -1;
	}


	printf("Server ready\n");
	//初始化客户端,及accept
	SOCKADDR_IN addrClient;
	int addrClientlen = sizeof(addrClient);
	sClient = accept(sServer,(sockaddr FAR*)&addrClient,&addrClientlen);
	if(sClient == INVALID_SOCKET){
		printf("accept error!\n");
		closesocket(sServer);
		WSACleanup();
		return -1;
	}

	//循环
	while(1){
		//接收
		ZeroMemory(recvbuf,buf_size);
		retval = recv(sClient,recvbuf,buf_size,0);
		if(SOCKET_ERROR == retval){
			printf("recv failed!\n");
			closesocket(sServer);
			closesocket(sClient);
			WSACleanup();
			return -1;
		}
		printf("from client: %s\n",recvbuf);

		//发送
		printf("input to send: ");
		ZeroMemory(sendbuf,buf_size);
		gets(sendbuf);
		strcat(sendbuf,"\0");
		retval = send(sClient,sendbuf,strlen(sendbuf)+1,0);
		if(strcmp(sendbuf,"quit") == 0){
			printf("quit!\n");
			break;
	
		}
	}
	if(WSACleanup() == SOCKET_ERROR)
		printf("WSACleanup出错!");

}

效果:

面向连接的socket通信_第2张图片

转载于:https://my.oschina.net/u/3331172/blog/1819516

你可能感兴趣的:(面向连接的socket通信)