C++ socket编程 实现服务端与客户端的双向TCP通讯

运行环境:VS2015

基于https://blog.csdn.net/orange_xxx/article/details/7276868做出了简单修改,可以完全实现双向的数据通信;

TCP通信流程

【来自https://blog.csdn.net/u012801153/article/details/79363575

Server步骤:

  1. 加载套接字库,创建套接字(WSAStartup()/socket());
  2. 绑定套接字到一个IP地址和一个端口上(bind());
  3. 将套接字设置为监听模式等待连接请求(listen());
  4. 请求到来后,接受连接请求,返回一个新的对应于此次连接的套接字(accept());
  5. 用返回的套接字和客户端进行通信,接收来自client的信息(recv()),发送回复信息至client(send());
  6. 返回,等待另一连接请求;
  7. 关闭套接字,关闭加载的套接字库(closesocket()/WSACleanup())。

Client步骤:

  1. 加载套接字库,创建套接字(WSAStartup()/socket());
  2. 绑定套接字到一个IP地址和一个端口上(bind());
  3. 向服务器发出连接请求(connect());
  4. 和服务器端进行通信,发送请求信息至server(send());接收server的回复信息(recv());
  5. 关闭套接字,关闭加载的套接字库(closesocket()/WSACleanup())。

github代码:https://github.com/jiangxinyiba/NetworkCommu_demo/tree/master

服务端代码Server.cpp:

#include "winsock2.h"
#include <WS2tcpip.h>
#include <iostream>
 
#pragma comment(lib, "ws2_32.lib") // load the dll of winsock
using namespace std;

int main(int argc, char* argv[])
{
	const int BUF_SIZE = 64;
	WSADATA			wsd;			    //WSADATA PARAM
	SOCKET			sServer;		    //Server Socket
	SOCKET			sClient;		    //Client Socket
	SOCKADDR_IN		servAddr;		    //Server Addr
	SOCKADDR_IN     clientAddr;         //Client Addr
	SOCKADDR_IN     connect_Addr;       //the Addr that connect this socket (in this demo, connect_Addr = clientAddr)
	int             connectClientlen = sizeof(connect_Addr);
	char			bufSend[BUF_SIZE];	//the buffer area of data-send
	char			bufRecv[BUF_SIZE];  //the buffer area of data-recv
	int				retVal;			    //return value
	char*			closeSymbol = "0";  //symbol of closing communication
	char*           bufReq = "Request Reply";  // symbol of Request Reply
	char            str[INET_ADDRSTRLEN];

    // Server Addr
	servAddr.sin_family = AF_INET;
	inet_pton(AF_INET, "127.0.0.1", (void*)&servAddr.sin_addr.S_un.S_addr);
	servAddr.sin_port = htons((short)5000);

	// Client Addr
	clientAddr.sin_family = AF_INET;
	inet_pton(AF_INET, "127.0.0.1", (void*)&clientAddr.sin_addr.S_un.S_addr);
	clientAddr.sin_port = htons((short)4999);

	// Initialize the dll of winsock
	if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0)
	{
		cout << "WSAStartup failed !" << endl;
		return 1;
	}

	// Create the Server socket 
	sServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (INVALID_SOCKET == sServer)
	{
		cout << "socket failed!" << endl;
		WSACleanup();			 
		return  -1;
	}
	else
	{
		cout << "Server TCP Socket init!" << endl;
	}

	// Bind the IP and Port on Server Socket 
	retVal = bind(sServer, (LPSOCKADDR)&servAddr, sizeof(SOCKADDR_IN));
	if (SOCKET_ERROR == retVal)
	{
		cout << "bind failed!" << endl;
		closesocket(sServer);
		WSACleanup();
		Sleep(5000);
		return -1;
	}
	else
	{
		cout << "Server TCP Socket bind IP & Port !" << endl;
		cout << "Server Address = " << inet_ntop(AF_INET, &servAddr.sin_addr, str, sizeof(str)) << ":" << ntohs(servAddr.sin_port) << endl;
	}

	// Listen the Server socket 
	retVal = listen(sServer, 10);
	if (0 == retVal)
	{
		cout << "Server Socket is listening !" << endl;
	}
	else if (SOCKET_ERROR == retVal)
	{
		cout << "listen failed!" << endl;
		closesocket(sServer);	 
		WSACleanup();	
		Sleep(5000);
		return -1;
	}

	cout << "Server Socket is waiting accpetion !" << endl;
	sClient = accept(sServer, (sockaddr FAR*)&connect_Addr, &connectClientlen);
	if (INVALID_SOCKET == sClient)
	{
		cout << "accept failed!" << endl;
		closesocket(sServer);	
		WSACleanup();
		Sleep(5000);
		return -1;
	}
	else
	{
		cout << "Server get connection with client socket: [" << inet_ntop(AF_INET, &connect_Addr.sin_addr, str, sizeof(str)) << ":" << ntohs(connect_Addr.sin_port) << "]" << endl;
	}

	// loop for the data from the accpeted socket/send the data to the socket 
	while (TRUE) 
	{
		// Initialize the buffer 
		ZeroMemory(bufRecv, BUF_SIZE);

		// Recevice the buffer data from the client 
		retVal = recv(sClient, bufRecv, BUF_SIZE, 0);
		if (SOCKET_ERROR == retVal)
		{
			continue;
		}

		// Check the data from client 
		bufRecv[retVal] = '\0';			// Set the last bit as \0 to avoid the wrong data  
		cout << "Data recv from Client Socket [" << inet_ntop(AF_INET, &connect_Addr.sin_addr, str, sizeof(str)) << ":" << ntohs(connect_Addr.sin_port) << "] : " << bufRecv << endl;
		// When the data from client is ‘0’,exit the loop and finish TCP Communication
		if (!strcmp(bufRecv, closeSymbol))
		{
			cout << "Client Socket wants to finish this communication" << endl;
			closesocket(sServer);
			WSACleanup();
			Sleep(5000);
			break;
		}
		else
		{
			// Automatically send the request reply to client  
			send(sClient, bufReq, strlen(bufReq), 0);
		}
	}
	// Exit
	closesocket(sServer);	 
	WSACleanup();			 
	Sleep(5000);
	return 0;
}

客户端代码Client.cpp:

// Client.cpp : Defines the entry point for the console application.
#include "winsock2.h"
#include <WS2tcpip.h>
#include <iostream>
#pragma comment(lib, "ws2_32.lib")

#define _WINSOCK_DEPRECATED_NO_WARNINGS

using namespace std;

int main(int argc, char* argv[])
{
	const int BUF_SIZE = 64;
	WSADATA			wsd;			    //WSADATA Param
	SOCKET			sClient;		    //Server Socket 
	SOCKADDR_IN		servAddr;		    //Server Addr 
	SOCKADDR_IN     clientAddr;         //Client Addr
	SOCKADDR_IN     send_Data_Addr;     //the Addr that send the data(in this demo, send_Data_Addr = servAddr)
	int				nAddrLen = sizeof(clientAddr);
	int				nAddrLen_s = sizeof(servAddr);
	char			bufSend[BUF_SIZE];	//the buffer area of data-send
	char			bufRecv[BUF_SIZE];  //the buffer area of data-recv
	int				retVal;			    //return value
	char*			closeSymbol = "0";  //symbol of close

    // Server Addr
	servAddr.sin_family = AF_INET;
	inet_pton(AF_INET, "127.0.0.1", (void*)&servAddr.sin_addr.S_un.S_addr);
	servAddr.sin_port = htons((short)5000);
	int	nServAddlen = sizeof(servAddr);

	// Client Addr
	clientAddr.sin_family = AF_INET;
	inet_pton(AF_INET, "127.0.0.1", (void*)&clientAddr.sin_addr.S_un.S_addr);
	clientAddr.sin_port = htons((short)4999);
	int	nClientAddlen = sizeof(clientAddr);

	// Initialize the dll of winsock
	if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0)
	{
		cout << "WSAStartup failed!" << endl;
		return -1;
	}

	// Create the Server socket 
	sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (INVALID_SOCKET == sClient)
	{
		cout << "Socket failed !" << endl;
		WSACleanup();       
		Sleep(5000);
		return  -1;
	}
	else
	{
		cout << "Client Socket init !" << endl;
	}

	// Bind the client socket with IP and Port 
	retVal = bind(sClient, (LPSOCKADDR)&clientAddr, sizeof(SOCKADDR_IN));
	if (SOCKET_ERROR == retVal)
	{
		cout << "bind failed!" << endl;
		closesocket(sClient);
		WSACleanup();
		Sleep(5000);
		return -1;
	}
	else
	{
		cout << "Client UDP Socket bind IP & Port !" << endl;
		char str[INET_ADDRSTRLEN];
		cout << "Client Address = " << inet_ntop(AF_INET, &clientAddr.sin_addr, str, sizeof(str)) << ":" << ntohs(clientAddr.sin_port) << endl;
	}

	// client request connect server socket
	retVal = connect(sClient, (LPSOCKADDR)&servAddr, sizeof(servAddr));
	if (SOCKET_ERROR == retVal)
	{
		cout << "Client connect failed!" << endl;
		closesocket(sClient);	 
		WSACleanup();	
		Sleep(5000);
		return -1;
	}
	else
	{
		cout << "Client connect with Server socket" << endl;
	}

	// loop for the data from the connected socket/send the data to the server socket 
	while (true) {
		// Initialize the buffer
		ZeroMemory(bufSend, BUF_SIZE);

		// Send the data from client socket to Server Socket
		cout << "Data send to Server Socket: ";
		cin >> bufSend;
		retVal = send(sClient, bufSend, strlen(bufSend), 0);
		if (SOCKET_ERROR == retVal)
		{
			cout << "Send failed!" << endl;
			closesocket(sClient);	 
			WSACleanup();	
			Sleep(5000);
			return -1;
		}
		// When the data from another socket is ‘0’,exit the loop and finish TCP Communication	
		if (!strcmp(bufSend, closeSymbol))
		{
			cout << "Client Socket wants to finish this communication" << endl;
			break;
		}

		// Receive data from server socket
		retVal = recv(sClient, bufRecv, BUF_SIZE, 0);
		bufRecv[retVal] = '\0';
		cout << "Data recv from Server Socket: " << bufRecv << endl;
	}

	closesocket(sClient);	 
	WSACleanup();		

	Sleep(5000);
	return 0;
}

 


注意事项

  1. inet_pton(AF_INET, "127.0.0.1", (void*)&servAddr.sin_addr.S_un.S_addr);这句话是替代了inet_addr的作用,vs2015里面inet_addr好像不能用了,只能用inet_pton替代,之前需要添加#include <WS2tcpip.h>。这里是单机内两个进程间的通信,最后采用的是127.0.0.1的localhost作为IP地址。

  2. WSAStartup(MAKEWORD(2,2),&wsaData):其中MAKEWORD(2,2)表示使用WINSOCK2版本,wsaData用来存储系统传回的关于WINSOCK的资料;

  3. ZeroMemory(bufSend, BUF_SIZE);   //端口发送数据,需要在每次交互前,把之前的buf数据清空,避免缓冲区冗余;但是不能对这个端口的接受数据的缓冲bufRecv清空;这样会造成永久性的空接受情况。

  4. retVal = recv(sClient, bufRecv,BUF_SIZE , 0);   //端口接受另一个端口数据,字符位要选择允许的满字符,这里为BUF_SIZE,确保每次都能把另一个端口的数据全都接收到;其中retVal记录了这组数据的长度;

  5. 这里需要注意,不论是客户端还是服务端,recv和send的第一个参数都是sClient,即客户端的socket,即在TCP通信中recv和send函数的第一个参数应该是发起connect连接的socket。

  6. bufRecv[retVal] = '\0';  //每次接受到的数据因为没有初始化,初始结构都是“烫烫烫烫”,在接受到后,会把前面的若干位变成接受的数据,但是后面还是都为烫,因此需要这个语句将接受数据的缓冲收尾,接受有效的前面若干位数据,剔除烫烫烫烫。

  7. 最后通过输入0来结束本次通信,若客户端发送了一个"0",那就表示需要结束本次通信,两个进程会在5秒内自动关闭。

  8. 两个代码分别放于两个项目中,先运行启动服务端代码,然后运行客户端代码,就可以进行本机的两端通信调试。若先启动了客户端,由于代码保护,一旦客户端无法找到可以连接的服务端,客户端会马上关闭;而先打开服务端的话,服务端会一直处于等待连接状态。

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值