欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

windows socket基本操作

程序员文章站 2022-03-04 21:43:10
...

以下例子使用的VS2015-MFC

client端:

//MFC基本设置:
工程右键-->属性-->常规-->MFC的使用改为在静态库中使用MFC
工程右键-->属性-->C/C++-->代码生成-->运行库-->改为多线程调试(/Mtd)
//如果报inet_addr不让使用旧函数,请设置//工程右键-->属性-->C/C++-->常规-->SDL检查-->改为否
#include "afxwin.h"	//引了afxwin.h就不用再引winsock2.h + #pragma comment(lib, "ws2_32.lib"), TRACE()也可以直接用了

int main()
{
	int err = 0;
	WORD wVersionRequested;//typedef unsigned short WORD
	WSADATA wsaData;//这个结构被用来存储 被WSAStartup函数调用后返回的 Windows Sockets数据
	wVersionRequested = MAKEWORD(1, 1);
	err = WSAStartup(wVersionRequested, &wsaData);
	int no = err;
	TRACE("%d\n", no);

	SOCKET sockClient = socket(AF_INET, SOCK_STREAM, 0);

	SOCKADDR_IN addrSrv;
	addrSrv.sin_family = AF_INET;
	addrSrv.sin_addr.s_addr = inet_addr("127.0.0.1");
	addrSrv.sin_port = htons(1024);

	err = connect(sockClient, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));

	if (err == INVALID_SOCKET)
	{
		TRACE("connect() fail. err=%d",  WSAGetLastError());
		return -1;
	}

	char sendBuf[1024], hostname[100];
	if (gethostname(hostname, 100) != 0)	//get host name
		strcpy_s(hostname, "None");
	strcpy_s(sendBuf, hostname);

	err = send(sockClient, sendBuf, strlen(sendBuf) + 1, 0);

	char recvBuf[1024] = "\0";
	int revLen = recv(sockClient, recvBuf, 1024, 0);
	if (revLen <= 0)
	{
		TRACE("server disconnect. err=%d", WSAGetLastError());
	}
	else
	{
		recvBuf[revLen] = '\0';
	}
	closesocket(sockClient);

	WSACleanup();
	system("PAUSE");
	return 0;
}

 

 

 

 

server端:

//MFC基本设置:
//工程右键-->属性-->常规-->MFC的使用改为在静态库中使用MFC
//工程右键-->属性-->C/C++-->代码生成-->运行库-->改为多线程调试(/Mtd)
//如果报inet_addr不让使用旧函数,请设置//工程右键-->属性-->C/C++-->常规-->SDL检查-->改为否
#include "afxwin.h"	//引了afxwin.h就不用再引winsock2.h + #pragma comment(lib, "ws2_32.lib"), TRACE()也可以直接用了


int main()
{
	bool bConn = false;
	int err = 0;
	WORD wVersionRequested;//typedef unsigned short WORD
	WSADATA wsaData;//这个结构被用来存储 被WSAStartup函数调用后返回的 Windows Sockets数据
	wVersionRequested = MAKEWORD(1, 1);
	err = WSAStartup(wVersionRequested, &wsaData);
	int no = err;
	TRACE("%d\n", no);

	SOCKET SockServer = socket(AF_INET, SOCK_STREAM, 0);
	// 设定本地服务器的地址
	SOCKADDR_IN addrSrv;
	addrSrv.sin_family = AF_INET;
	addrSrv.sin_port = htons(1024); //1024以上的端口号
	addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);

	int retVal = bind(SockServer, (LPSOCKADDR)&addrSrv, sizeof(SOCKADDR_IN));
	if (retVal == SOCKET_ERROR) {
		TRACE("Failed bind:%d\n", WSAGetLastError());
		return -1;
	}

	if (listen(SockServer, 10) == SOCKET_ERROR) {
		TRACE("Listen failed:%d", WSAGetLastError());
		return -1;
	}

	SOCKADDR_IN addrClient;
	int len = sizeof(SOCKADDR);
	char sendbuf[1024] = { 0 };

	while (1)
	{
		//等待客户请求到来
		SOCKET SockClient = accept(SockServer, (SOCKADDR *)&addrClient, &len);
		if (SockServer == SOCKET_ERROR) {
			TRACE("Accept failed:%d", WSAGetLastError());
			break;
		}
		else 
		{
			bConn = true;
			TRACE("Accept client IP:[%s]\n", inet_ntoa(addrClient.sin_addr));
		}

		while (1)
		{
			char recvBuf[MAXBYTE];
			memset(recvBuf, 0, sizeof(recvBuf));
			// //接收数据
			int recLen = recv(SockClient, recvBuf, sizeof(recvBuf), 0);
			if (recLen <= 0) {
				TRACE("%s:%d disconnenct.\n", inet_ntoa(addrClient.sin_addr), addrClient.sin_port);
				bConn = false;
				closesocket(SockClient);
				break;
			}
			printf("recv:%s\n", recvBuf);
		}
	}

	closesocket(SockServer);
	return 0;
}

 

相关标签: windows环境 c++