windows系统环境c++(c) socket编程实例代码(含服务端和客户端)

本代码是windows系统环境c++(c) socket编程实例。包含客户端和服务端,可以实现客户端发送消息,服务端接收消息并写入日志。 server.cpp为服务端。 client.cpp为客户端。

服务端代码server.cpp:


//by whg 2018-5-23

#include 
#include 
#include 

#include
#include
#pragma comment(lib, "ws2_32.lib")
#define CONNECT_NUM_MAX 10

using namespace std;


//日志记录
int write_log (char *format) {


	FILE* pFile = fopen("e:\\log.txt", "a");
    fprintf(pFile, "recv:%s\n",format);
	fflush(pFile);
    fclose(pFile);

    return 1;
}


int main()
{
	//加载套接字库
	WSADATA wsaData;
	int iRet = 0;
	iRet = WSAStartup(MAKEWORD(2, 2), &wsaData);
	if (iRet != 0)
	{
		cout << "WSAStartup(MAKEWORD(2, 2), &wsaData) execute failed!" << endl;;
		return -1;
	}
	if (2 != LOBYTE(wsaData.wVersion) || 2 != HIBYTE(wsaData.wVersion))
	{
		WSACleanup();
		cout << "WSADATA version is not correct!" << endl;
		return -1;
	}

	//创建套接字
	SOCKET serverSocket = socket(AF_INET, SOCK_STREAM, 0);
	if (serverSocket == INVALID_SOCKET)
	{
		cout << "serverSocket = socket(AF_INET, SOCK_STREAM, 0) execute failed!" << endl;
		return -1;
	}

	//初始化服务器地址族变量
	SOCKADDR_IN addrSrv;
	addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
	addrSrv.sin_family = AF_INET;
	addrSrv.sin_port = htons(6000);

	//绑定
	iRet = bind(serverSocket, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
	if (iRet == SOCKET_ERROR)
	{
		cout << "bind(serverSocket, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR)) execute failed!" << endl;
		return -1;
	}


	//监听
	iRet = listen(serverSocket, CONNECT_NUM_MAX);
	if (iRet == SOCKET_ERROR)
	{
		cout << "listen(serverSocket, 10) execute failed!" << endl;
		return -1;
	}

	printf("server is start,waiting for client.\n");

	//等待连接_接收_发送
	SOCKADDR_IN clientAddr;
	int len = sizeof(SOCKADDR);


	SOCKET connSocket;
	connSocket = accept(serverSocket, (SOCKADDR*)&clientAddr, &len);
	char recvBuf[100];

	//while (recvBuf != "endBuf")
	//{
		
		if (connSocket == INVALID_SOCKET)
		{
			cout << "accept(serverSocket, (SOCKADDR*)&clientAddr, &len) execute failed!" << endl;
			return -1;
		}

		recv(connSocket, recvBuf, 100, 0);
		printf("%s\n", recvBuf);

		//写入日志
		write_log(recvBuf);
		
	//}
	
	closesocket(connSocket);
	printf("close socket.\n");
	
	system("pause");
	return 0;
}



客户端代码client.cpp:

// by whg 2018-5-23

#include
#include 
using namespace std;

#pragma comment(lib, "ws2_32.lib")
int main()
{
	//加载套接字库
	WSADATA wsaData;
	int iRet =0;
	iRet = WSAStartup(MAKEWORD(2, 2), &wsaData);
	if (iRet != 0)
	{
		cout << "WSAStartup(MAKEWORD(2, 2), &wsaData) execute failed!" << endl;
		return -1;
	}
	if (2 != LOBYTE(wsaData.wVersion) || 2 != HIBYTE(wsaData.wVersion))
	{
		WSACleanup();
		cout << "WSADATA version is not correct!" << endl;
		return -1;
	}

	//创建套接字  TCP
	SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, 0);
	if (clientSocket == INVALID_SOCKET)
	{
		cout << "clientSocket = socket(AF_INET, SOCK_STREAM, 0) execute failed!" << endl;
		return -1;
	}

	//初始化服务器端地址族变量
	SOCKADDR_IN srvAddr;
	srvAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
	srvAddr.sin_family = AF_INET;
	srvAddr.sin_port = htons(6000);

	//连接服务器
	iRet = connect(clientSocket, (SOCKADDR*)&srvAddr, sizeof(SOCKADDR));
	if (0 != iRet)
	{
		cout << "connect(clientSocket, (SOCKADDR*)&srvAddr, sizeof(SOCKADDR)) execute failed!" << endl;
		return -1;
	}

 	//发送消息
	char sendBuf[100];
	sprintf_s(sendBuf, "Hello, This is client %s", "wang");
	send(clientSocket, sendBuf, strlen(sendBuf)+1, 0);

	//清理
	closesocket(clientSocket);
	WSACleanup();
	
	system("pause");
	return 0;
}

测试:

首先,需要准备好C++环境。

先编译运行server.cpp,显示:“server is start,waiting for client.”表示启动成功。
再编译运行client.cpp,这时再看服务端,显示:“Hello, This is client whg”,表示收到客户端发来的消息。

 

你可能感兴趣的:(C,C++,socket,c语言socket编程,WinSock2.h,ws2_32.lib,套接字编程)