VS2013中C++方式的进程间通信---socket

服务器进程:

#include <Windows.h>

#include <iostream>
using namespace std;

#pragma comment(lib, "ws2_32.lib")

SOCKET ServerSocket;
SOCKET CientSocket;

#define  IP_ADDRESS "127.0.0.1"

#define BUF_MAX_LEN      500*1024   

int main(int argc, char *argv[])
{
	WSADATA  Ws;
	struct sockaddr_in LocalAddr, ClientAddr;
	int Ret = 0;
	int AddrLen = 0;
	HANDLE hThread = NULL;

	//初始化Windows socket网络库
	if (WSAStartup(MAKEWORD(2, 2), &Ws) != 0)  //初始化win socket网络库,申请2.2的版本,并判断是否初始化成功  
	{
		cout << "Init windows socket failed::" << GetLastError() << endl;
		return 1;
	}

	//创建socket    
	ServerSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (ServerSocket == INVALID_SOCKET)
	{
		cout << "Create socket failed::" << GetLastError() << endl;
		return 1;
	}

	LocalAddr.sin_family = AF_INET;  //sin_family指代协议族,在socket编程中只能是AF_INET  
	LocalAddr.sin_addr.s_addr = inet_addr(IP_ADDRESS);  //sin_addr存储IP地址,使用in_addr这个数据结构  
	LocalAddr.sin_port = htons(0); //sin_port存储端口号(使用网络字节顺序)   
	memset(LocalAddr.sin_zero, 0x00, 8);

	//绑定socket
	Ret = ::bind(ServerSocket, (struct sockaddr*)&LocalAddr, sizeof(LocalAddr));
	if (Ret != 0)
	{
		cout << "Bind socket failed::" << GetLastError() << endl;
		return 1;
	}

	int iLen = sizeof(LocalAddr);
	getsockname(ServerSocket, (struct sockaddr *)&LocalAddr, &iLen);//得到本地的IP地址和端口号
	int port = LocalAddr.sin_port;

	//监听该socket
	Ret = listen(ServerSocket, 10);
	if (Ret != 0)
	{
		cout << "Listen socket failed::" << GetLastError() << endl;
		return 1;
	}

	while (true)
	{
		AddrLen = sizeof(ClientAddr);
		//等待客户端的连接请求
		CientSocket = accept(ServerSocket, (struct sockaddr*)&ClientAddr, &AddrLen);
		if (CientSocket == INVALID_SOCKET)
		{
			cout << "Accept Failed::" << GetLastError() << endl;
			break;
		}

		while (true)
		{
			int Ret = 0;
			char recv_buff[BUF_MAX_LEN] = { 0 };

			//从socket中读取数据
			Ret = recv(CientSocket, recv_buff, BUF_MAX_LEN, 0);

			if (Ret == 0 || Ret == SOCKET_ERROR)
			{
				//连接断开,客户端掉线
				break;
			}

			//处理数据
		}

		closesocket(ServerSocket);
		closesocket(CientSocket);
		WSACleanup();
	}

	return 0;
}

客户端进程:

#include <Windows.h>

#include <iostream>
using namespace std;

#pragma comment(lib, "ws2_32.lib")

SOCKET CientSocket;

#define  IP_ADDRESS "127.0.0.1"
#define BUF_SIZE 8
#define BUF_MAX_LEN      500*1024   

int main(int argc, char *argv[])
{
	WSADATA  Ws;
	struct sockaddr_in ServerAddr;
	int Ret = 0;
	int AddrLen = 0;
	HANDLE hThread = NULL;

	//初始化Windows Socket库    
	if (WSAStartup(MAKEWORD(2, 2), &Ws) != 0)
	{
		cout << "Init windows socket failed::" << GetLastError() << endl;
		return 1;
	}

	//创建socket    
	CientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (CientSocket == INVALID_SOCKET)
	{
		WSACleanup();
		cout << "Create Socket Failed::" << GetLastError() << endl;
		return 1;
	}

	ServerAddr.sin_family = AF_INET;
	ServerAddr.sin_addr.s_addr = inet_addr(IP_ADDRESS);
	int port = 0; //此处要为服务器绑定的端口号
	ServerAddr.sin_port = port;
	memset(ServerAddr.sin_zero, 0x00, BUF_SIZE);

	Ret = ::connect(CientSocket, (struct sockaddr*)&ServerAddr, sizeof(ServerAddr));
	if (Ret == SOCKET_ERROR)
	{
		cout << "Connect Error::" << GetLastError() << endl;
		closesocket(CientSocket);
		WSACleanup();
		return 1;
	}

	while (true)
	{
		int Ret = 0;

		char recv_buff[BUF_MAX_LEN] = { 0 };

		Ret = recv(CientSocket, recv_buff, BUF_MAX_LEN, 0);
		if (Ret == 0 || Ret == SOCKET_ERROR)
		{
			cout << "Client disconect::" << GetLastError() << endl; 
			break;
		}
		
		//处理接收到的数据
	}

	return 0;
}

发送数据的代码:

	int Ret = 0;
	char data[] = { 100 };
	//CientSocket为客户端的socket,不管是服务器像客户端发消息还是客户端向服务器发消息
	Ret = send(CientSocket, (char *)data, sizeof(data), 0);   
	if (Ret == SOCKET_ERROR)
	{
		cout << "Send msg failed::" << GetLastError() << endl;
		return;
	}

 

展开阅读全文

没有更多推荐了,返回首页