发送端向接收端发送数据,ip地址和端口号要与接收端一致
文章来源:https://www.toymoban.com/news/detail-514423.html
cpp_UDP客户端,发送端文章来源地址https://www.toymoban.com/news/detail-514423.html
#include <winsock2.h>
#include <WS2tcpip.h>
#include <iostream>
using namespace std;
//添加动态库的lib
#pragma comment(lib, "ws2_32.lib")
int main() {
SOCKET m_Socket;
SOCKADDR_IN m_RemoteAddress; //远程地址
int m_RemoteAddressLen;
// socket环境
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
cout << "WSAStartup error:" << GetLastError() << endl;
return false;
}
// socket对象
m_Socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (m_Socket == INVALID_SOCKET)
{
closesocket(m_Socket);
m_Socket = INVALID_SOCKET;
return false;
}
// 远端地址
const char* ip = "172.22.64.148";
int port = 9800;
m_RemoteAddress.sin_family = AF_INET;
m_RemoteAddress.sin_port = htons(port);
m_RemoteAddressLen = sizeof(m_RemoteAddress);
inet_pton(AF_INET, ip, &m_RemoteAddress.sin_addr);
// 接收和发送
char recvBuf[1024] = { 0 };
char sendBuf[1024] = "Nice to meet you!我的";
while (1) {
int sendLen = sendto(m_Socket, sendBuf, strlen(sendBuf), 0, (sockaddr*)&m_RemoteAddress, m_RemoteAddressLen);
if (sendLen > 0) {
std::printf("发送到远程端连接, 其ip: %s, port: %d\n", inet_ntoa(m_RemoteAddress.sin_addr), ntohs(m_RemoteAddress.sin_port));
cout << "发送到远程端的信息: " << sendBuf << endl;
}
int recvLen = recvfrom(m_Socket, recvBuf, 1024, 0, NULL, NULL);
if (recvLen > 0) {
std::printf("接收到一个连接, 其ip: %s, port: %d\n", inet_ntoa(m_RemoteAddress.sin_addr), ntohs(m_RemoteAddress.sin_port));
cout << "接收到一个信息: " << recvBuf << endl;
}
}
closesocket(m_Socket);
WSACleanup();
return true;
}
cpp_UDP服务端,接收端
#include <winsock2.h>
#include <WS2tcpip.h>
#include <iostream>
using namespace std;
//添加动态库的lib
#pragma comment(lib, "ws2_32.lib")
int main() {
SOCKET m_Socket;
SOCKADDR_IN m_BindAddress; //绑定地址
SOCKADDR_IN m_RemoteAddress; //远程地址
int m_RemoteAddressLen;
// socket环境
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
cout << "WSAStartup error:" << GetLastError() << endl;
return false;
}
// socket对象
m_Socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (m_Socket == INVALID_SOCKET)
{
closesocket(m_Socket);
m_Socket = INVALID_SOCKET;
return false;
}
// 绑定占用<ip, port>
const char* ip = "172.22.64.42";
int port = 9800;
m_BindAddress.sin_family = AF_INET;
m_BindAddress.sin_addr.S_un.S_addr = inet_addr(ip);
m_BindAddress.sin_port = htons(port);
auto ret = bind(m_Socket, (sockaddr*)&m_BindAddress, sizeof(SOCKADDR));
if (ret == SOCKET_ERROR)
{
closesocket(m_Socket);
m_Socket = INVALID_SOCKET;
return false;
}
// 接收和发送
char recvBuf[1024] = { 0 };
char sendBuf[1024] = "Nice to meet you, too!";
m_RemoteAddressLen = sizeof(m_RemoteAddress);
std::printf("已设置绑定占用的连接, 其ip: %s, port: %d\n", inet_ntoa(m_BindAddress.sin_addr), ntohs(m_BindAddress.sin_port));
while (1) {
int recvLen = recvfrom(m_Socket, recvBuf, 1024, 0, (sockaddr*)&m_RemoteAddress, &m_RemoteAddressLen);
if (recvLen > 0) {
std::printf("接收到一个连接, 其ip: %s, port: %d\n", inet_ntoa(m_RemoteAddress.sin_addr), ntohs(m_RemoteAddress.sin_port));
cout << "接收到一个信息: " << recvBuf << endl;
}
int sendLen = sendto(m_Socket, sendBuf, strlen(sendBuf), 0, (sockaddr*)&m_RemoteAddress, m_RemoteAddressLen);
if (sendLen > 0) {
cout << "发送到远程端的信息: " << sendBuf << endl;
}
}
closesocket(m_Socket);
WSACleanup();
return true;
}
到了这里,关于C++实现简单UDP通信的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!