问题标题: 封装好的 winsock 网络通信函数

0
1
已解决
薛乘志
薛乘志
初级启示者
初级启示者

可以实现在局域网内通信,局域网IP地址可以通过在 命令提示符 内输入 ipconfig 查看

编译时需要另外添加编译参数 -lwsock32

网络通信的函数延时较大,容易造成程序效率下降,建议使用多线程:

https://www.runoob.com/cplusplus/cpp-multithreading.html(需要编译参数 -lpthread )

将该文件命名为 cdt_winsock.cpp,并放置到与你的源代码同一目录,

即可使用 #include"cdt_winsock.cpp" 将其加入到你的程序中(或者直接将其复制到你的代码里)

发送/接收数据的长度锁定为BYTE_LEN=1024(防止丢包/粘包)

#include <winsock2.h> //需要添加编译参数:-lwsock32 位置:工具->编译选项->编译时加入以下参数
#pragma comment(lib, "ws2_32.lib")

using namespace std;

const int BYTE_LEN = 1024; //发送/接受数据的长度(务必为1024,否则丢包/粘包)

void SOCK_INIT() { //初始化程序,请在其他运行其它函数前运行此函数
	WSADATA wsaData;
	WSAStartup( MAKEWORD(2, 2), &wsaData);
	return;
}

void SOCK_CLOSE() { //停止使用DLL,请在程序结束时调用
	WSACleanup();
	return;
}

struct SOCK_CLIENT {
	SOCKET connect_ip(char ip[], u_short port) { //使用一个port(0-65535)连接到一个IP,返回一个套接字
		SOCKET sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
		sockaddr_in sockAddr;
		memset(&sockAddr, 0, sizeof(sockAddr));
		sockAddr.sin_family = PF_INET;
		sockAddr.sin_addr.s_addr = inet_addr(ip);
		sockAddr.sin_port = htons(port);
		connect(sock, (SOCKADDR*)&sockAddr, sizeof(SOCKADDR));
		return sock;
	}
	void read(SOCKET sock, char reads[]) { //读取一个套接字内的内容(返回字符数组长度:1024)
		recv(sock, reads, 1024, 0);
		return;
	}
	void write(SOCKET sock, char writes[]) { //发送信息给特定的套接字(发送字符数组长度:1024)
		send(sock, writes, 1024, 0);
		return;
	}
	bool detect_on(SOCKET sock) { //检测sock是否在线,在线返回1,下线返回0(该函数会堵塞)
		char *reads;
		return recv(sock, reads, 1024, 0);
	}
	void disconnect(SOCKET sock) { //关闭一个套接字,但不清除套接字
		shutdown(sock, SD_BOTH);
		return;
	}
	void close(SOCKET sock) { //关闭一个套接字,同时清除套接字
		closesocket(sock);
		return;
	}
};

struct SOCK_SERVER {
	SOCKET init(u_short port) { //初始化服务端以开始接受客户端申请的连接,返回一个可用于连接客户端套接字
		SOCKET sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
		sockaddr_in sockAddr;
		memset(&sockAddr, 0, sizeof(sockAddr));
		sockAddr.sin_family = PF_INET;
		sockAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
		sockAddr.sin_port = htons(port);
		bind(sock, (SOCKADDR*)&sockAddr, sizeof(SOCKADDR));
		listen(sock, 20);
		return sock;
	}
	SOCKET wait_connect(SOCKET sock) { //等待客户端的连接(该函数会堵塞直到有请求),并返回一个可用于发送接收数据的套接字
		SOCKADDR clntAddr;
		int nSize = sizeof(SOCKADDR);
		SOCKET clntSock = accept(sock, (SOCKADDR*)&clntAddr, &nSize);
		return clntSock;
	}
	void read(SOCKET sock, char reads[]) { //读取一个套接字内的内容(返回字符数组长度:1024)
		recv(sock, reads, 1024, 0);
		return;
	}
	void write(SOCKET sock, char writes[]) { //发送信息给特定的套接字(发送字符数组长度:1024)
		send(sock, writes, 1024, 0);
		return;
	}
	bool detect_on(SOCKET sock) { //检测sock是否在线,在线返回1,下线返回0(该函数会堵塞)
		char *reads;
		return recv(sock, reads, 1024, 0);
	}
	void disconnect(SOCKET sock) { //关闭一个套接字,但不清除套接字
		shutdown(sock, SD_BOTH);
		return;
	}
	void close(SOCKET sock) { //关闭一个套接字,同时清除套接字
		closesocket(sock);
		return;
	}
};

//多线程(运行函数不堵塞):https://www.runoob.com/cplusplus/cpp-multithreading.html

下面是一个简单的样例:

server.cpp

#include <bits/stdc++.h>
#include "cdt_winsock.cpp" //载入上面提供的代码
#include <windows.h>
#include <conio.h>
using namespace std;
SOCK_SERVER server;
int main() {
	SOCK_INIT();//初始化
	SOCKET sock = server.wait_connect(server.init(1234));//等待客户端连接
	char inf1[1024] = "Hello World!";
	server.write(sock, inf1);//发送inf1字符数组
	char inf2[1024] = "Connect Successfully!";
	server.write(sock, inf2);
	server.close(sock);//关闭套接字
	SOCK_CLOSE();//停止使用DLL
	system("pause");
	return 0;
}

client.cpp

#include <bits/stdc++.h>
#include "cdt_winsock.cpp"
#include <windows.h>
#include <conio.h>
using namespace std;
SOCK_CLIENT client;
int main() {
	SOCK_INIT();
	SOCKET sock = client.connect_ip("127.0.0.1", 1234);//连接到127.0.0.1(本机IP)
	char s[1024];
	client.read(sock, s);//从127.0.0.1读入信息
	cout << s << endl;
	client.read(sock, s);
	cout << s << endl;
	client.close(sock);
	SOCK_CLOSE();
	system("pause");
	return 0;
}

 

薛乘志在2021-08-18 13:33:00追加了内容

这个代码可以直接使用,无需和我说

薛乘志在2021-08-18 13:33:37追加了内容

终于有一次代码里没有**了...

薛乘志在2021-08-18 15:01:11追加了内容

不懂的在下面问即可


0
1
0
0
王泽轩
王泽轩
资深天翼
资深天翼

 

NBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNB

NBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNB

NBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNB

NBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNB

 

NBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNB

NBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNB

NBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNB

NBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBNBN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