系统相关
首页 > 系统相关> > Windows下socket编程(console非MFC)

Windows下socket编程(console非MFC)

作者:互联网

console控制台:使用<winsock2.h> 和 ws2_32.lib

////// TcpSrv.cpp /////////

#define _CRT_SECURE_NO_WARNINGS //用sprintf,而不必强制使用sprintf_s 不报错

#include <winsock2.h>
#include <iostream>
using namespace std;

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

void main()
{
	WORD wVersionRequested;
	WSADATA wsaData;
	int err;
	wVersionRequested = MAKEWORD(1, 1);
	//WSAStartup() initiates use of Ws2_32.dll by a process.
	err = WSAStartup(wVersionRequested, &wsaData);
	if (err != 0) {
		return;
	}

	if (LOBYTE(wsaData.wVersion) != 1 ||
		HIBYTE(wsaData.wVersion) != 1) {
		WSACleanup();
		return;
	}

	//1.创建流式套接字,基于TCP(SOCK_STREAM)
	SOCKET sockSrv = socket(AF_INET, SOCK_STREAM, 0);
	//socket地址结构体
	SOCKADDR_IN addrSrv;//除了sin_family,其余都需要转换为网络字节序
	addrSrv.sin_family = AF_INET;
	addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);//转换Unsigned long型为网络字节序格式, INADDR_ANY表示接受多个IP地址的回应信息
	addrSrv.sin_port = htons(6000);//端口6000,转换unsigned short为网络字节序
	//2.将套接字绑定到一个端口号和本地地址上
	bind(sockSrv, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
	//3.监听
	listen(sockSrv, 5);
	//
	SOCKADDR_IN addrClient;//定义用来接收客户端Socket的结构体
	int len = sizeof(SOCKADDR);//初始化参数,这个参数必须进行初始化
	//4.循环等待接受客户端发送请求
	while (true)
	{
		//等待客户请求到来;当请求到来后,接受连接请求,
        //返回一个新的对应于此次连接的套接字(sockConnect)。
		//接受Client请求,阻塞
		SOCKET sockConnect = accept(sockSrv, (SOCKADDR*)&addrClient, &len);
		//发送数据
		char buf[100];//inet_ntoa(addrClient.sin_addr)将IP地址转换为字符串,像 "192.168.0.5"
		sprintf(buf, "Welcome %s to my little world.", inet_ntoa(addrClient.sin_addr));
		send(sockConnect, buf, strlen(buf) + 1, 0);//多发一个,为了\0结尾
		//接收数据
		char recvBuf[100] = { 0 };
		recv(sockConnect, recvBuf, sizeof(recvBuf), 0);

		cout << recvBuf << endl;

		//5.关闭socket
		closesocket(sockConnect);
	}

//	WSACleanup();

	return ;
}

//////////////////////////////////////////////////////////
///////// TcpClient.cpp  ////////////

#define _CRT_SECURE_NO_WARNINGS //用sprintf,而不必强制使用sprintf_s 不报错

#include <winsock2.h>
#include <iostream>
using namespace std;

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

void main()
{
	WORD wVersionRequested;
	WSADATA wsaData;
	int err;
	wVersionRequested = MAKEWORD(1, 1);
	//WSAStartup() initiates use of Ws2_32.dll by a process.
	err = WSAStartup(wVersionRequested, &wsaData);
	if (err != 0) {
		return;
	}

	if (LOBYTE(wsaData.wVersion) != 1 ||
		HIBYTE(wsaData.wVersion) != 1) {
		WSACleanup();
		return;
	}

	//1.创建Client端的Socket
	SOCKET sockClient = socket(AF_INET, SOCK_STREAM, 0);
	//2.连接Server地址
	SOCKADDR_IN addrSrv;
	addrSrv.sin_family = AF_INET;
	addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");//inet_addr 将IP字符串转为ULONG类型数值
	addrSrv.sin_port = htons(6000);//Port:6000
	connect(sockClient, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
	//接收
	char recvBuf[100] = { 0 };
	recv(sockClient, recvBuf, sizeof(recvBuf), 0);
	cout << recvBuf << endl;
	//发送
	char* buf = "This is lisi.";
	send(sockClient, buf, strlen(buf) + 1, 0);
	//3.关闭套接字
	closesocket(sockClient);
	WSACleanup();//终止套接字库的使用
}

 MFC的Socket通信:使用#include <afxsock.h>中MFC封装的类CSocket

参见:MFC中Socket网络通讯

 

标签:SOCKADDR,wVersionRequested,console,socket,Windows,wsaData,addrSrv,sin,addr
来源: https://www.cnblogs.com/htj10/p/11255733.html