1. UDP通信流程
UDP就比较简单了,步骤比tcp要少一些。
连接过程图:
1.1 服务器
1.初始化套接字库
WORD wVersion;
WSADATA wsaData;
int err;
wVersion = MAKEWORD(1, 1);
2.创建套接字
SOCKET sockSrv = socket(AF_INET, SOCK_DGRAM, 0);
3.绑定
// SOCKADDR_IN addrSrv; 省略了定义和赋值
bind(sockSrv, (SOCKADDR *)&addrSrv, sizeof(SOCKADDR));
4.接收数据
char recvBuf[100];
recvfrom(sockSrv, recvBuf, 100, 0, (SOCKADDR *)&addrCli, &len);
5.发送数据
char sendBuf[] = "hello Client,I'm Server!\n";
sendto(sockSrv, sendBuf, strlen(sendBuf) + 1, 0, (SOCKADDR *)&addrCli, len);
6.关闭
closesocket(sockSrv);
WSACleanup();
服务器具体实现代码:
#include <WinSock2.h>
#include <iostream>
#pragma comment(lib, "ws2_32.lib")
int main(void) {
// 初始化套接字库
WORD wVersion;
WSADATA wsaData;
int err;
wVersion = MAKEWORD(1, 1);
err = WSAStartup(wVersion, &wsaData);
if (err != 0) {
return err;
}
if (LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1) {
WSACleanup();
return -1;
}
// 创建套接字
SOCKET sockSrv = socket(AF_INET, SOCK_DGRAM, 0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(6001);
// 绑定到本机6001端口
bind(sockSrv, (SOCKADDR *)&addrSrv, sizeof(SOCKADDR));
// 接收请求,处理请求
SOCKADDR_IN addrCli;
int len = sizeof(SOCKADDR);
char sendBuf[] = "hello Client,I'm Server!\n";
char recvBuf[100];
std::cout << "start UDP server with port 6001" << std::endl;
while (1) {
// 接收数据
recvfrom(sockSrv, recvBuf, 100, 0, (SOCKADDR *)&addrCli, &len);
std::cout << "Recv:" << recvBuf << std::endl;
// 发送数据
sendto(sockSrv, sendBuf, strlen(sendBuf) + 1, 0, (SOCKADDR *)&addrCli, len);
std::cout << "Send:" << sendBuf << std::endl;
}
// 关闭
closesocket(sockSrv);
WSACleanup();
return 0;
}
1.2 客户端
1.初始化套接字库
WORD wVersion;
WSADATA wsaData;
int err;
wVersion = MAKEWORD(1, 1);
2.创建UDP套接字
SOCKET sockCli = socket(AF_INET, SOCK_DGRAM, 0);
SOCKADDR_IN addrSrv;
3.接收数据
char recvBuf[100];
recvfrom(sockCli, recvBuf, 100, 0, (SOCKADDR *)&addrCli, &len);
4.发送数据
char sendBuf[] = "hello Client,I'm Server!\n";
sendto(sockCli, sendBuf, strlen(sendBuf) + 1, 0, (SOCKADDR *)&addrSrv, len);
5.关闭
closesocket(sockSrv);
WSACleanup();
客户端具体实现代码:
#include <WinSock2.h>
#include <iostream>
#pragma comment(lib, "ws2_32.lib")
int main(void) {
// 初始化套接字库
WORD wVersion;
WSADATA wsaData;
int err;
wVersion = MAKEWORD(1, 1);
err = WSAStartup(wVersion, &wsaData);
if (err != 0) {
return err;
}
if (LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1) {
WSACleanup();
return -1;
}
// 创建UDP套接字
SOCKET sockCli = socket(AF_INET, SOCK_DGRAM, 0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(6001);
SOCKADDR_IN addrCli;
int len = sizeof(SOCKADDR);
char sendBuf[] = "hello, I'm Client!\n";
char recvBuf[100];
// 发送数据
std::cout << "send to Server: " << sendBuf << std::endl;
sendto(sockCli, sendBuf, strlen(sendBuf) + 1, 0, (SOCKADDR *)&addrSrv, len);
// 接收数据
recvfrom(sockCli, recvBuf, 100, 0, (SOCKADDR *)&addrCli, &len);
std::cout << "recv from: " << recvBuf << std::endl;
// 关闭
closesocket(sockCli);
WSACleanup();
system("pause");
return 0;
}
运行效果:
2. 总结
socket的具体细节用法我不太清楚,现阶段也只是熟悉TCP的一些简单操作,UDP的话也还不是太懂,不懂的是不知道在具体项目中该如何进行使用它们。
那个TCP的小项目也只是自己琢磨搞出来的,不知掉具体项目会不会这样去写!
————————————————
版权声明:本文为CSDN博主「cpp_learners」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/cpp_learner/article/details/121798854
标签:SOCKADDR,UDP,sendBuf,err,通信,C++,wsaData,wVersion,addrSrv From: https://www.cnblogs.com/zhuchunlin/p/17757668.html