gpt4 book ai didi

c++ - UDP winsock 服务器 C++

转载 作者:行者123 更新时间:2023-11-28 05:57:25 25 4
gpt4 key购买 nike

我正在使用 UDP 连接进行一些套接字编程。我已经编写了我的服务器(它也需要一个 NTP 服务器的偏移量),但是当我对其进行单元测试时,它在 recvfrom 函数中失败了。我是 winsock 编程和一般网络编程的新手,所以我不确定从这里到哪里去。这是我的代码:

NtpServer::NtpServer(u_short portnum, const std::chrono::nanoseconds desiredOffset) : portnum(0), client_length(0), bytes_received(0), current_time(0), desiredOffset(0)
{
WORD wVersionRequested;
wVersionRequested = MAKEWORD(2, 2);
WSADATA wsaData;

int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (WSAStartup(wVersionRequested, &wsaData) != 0)
{

std::cerr << "Could not open Windows connection" << std::endl;
exit(0);
}

sd = socket(AF_INET, SOCK_DGRAM, 0);
if (sd == INVALID_SOCKET)
{
std::cerr << "Could not create socket." << std::endl;
WSACleanup();
exit(0);
}

//blocking enabled
u_long iMode = 0;

iResult = ioctlsocket(sd, FIONBIO, &iMode);
if (iResult != NO_ERROR)
std::cerr << "ioctlsocket failed with error: " << iResult << std::endl;


memset((void *)&server, '\0', sizeof(struct sockaddr_in));
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons(portnum);


if (bind(sd, reinterpret_cast<SOCKADDR *>(&server),
sizeof(server)) == -1)
{
std::cerr << "Could not bind name to socket" << std::endl;
closesocket(sd);
WSACleanup();
exit(0);
}
getResult(desiredOffset);
}

和 getResult 函数:

void NtpServer::getResult(const std::chrono::nanoseconds desiredOffset)
{
ntp_data ntpData = ntp_data();
//struct for timeout
struct timeval tv;
tv.tv_sec = 10; // 10 Secs Timeout
setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval));

while (1)
{
client_length = (int)sizeof(struct sockaddr_in);

/* Receive bytes from client */
bytes_received = recvfrom(sd, sendBuffer, NTP_PACKET_MAX, 0, (struct sockaddr *)&client, &client_length);
if (bytes_received < NTP_PACKET_MIN)
{
std::cerr << "Could not receive datagram." << std::endl;
closesocket(sd);
WSACleanup();
exit(0);
}



/* Check for time request */
if (strcmp(readBuffer, "GET TIME\r\n") == 0)
{
/* Get current time */
system_clock::time_point now = std::chrono::system_clock::now();
auto timepointoffset = (now + desiredOffset).time_since_epoch();
double current_value = std::chrono::duration_cast<std::chrono::duration<double>>(timepointoffset).count();

unpack_ntp(&ntpData, (unsigned char*)readBuffer, bytes_received);
make_packet(&ntpData, NTP_CLIENT, current_value);
pack_ntp((unsigned char *)sendBuffer, NTP_PACKET_MIN, &ntpData);


/* Send data back */
if (sendto(sd, sendBuffer,
(int)sizeof(sendBuffer), 0,
(struct sockaddr *)&client, client_length) !=
(int)sizeof(current_time))
{
std::cerr << "Error sending datagram." << std::endl;
closesocket(sd);
WSACleanup();
exit(0);
}
}
}
closesocket(sd);
WSACleanup();


}

最佳答案

您必须测试来自 recvfrom() 的错误——您正在设置 SO_RCVTIMEO,因此您可以预期接收超时。

真的想要启用读取超时吗?如果是这样,当您设置 SO_RCVTIMEO 时,设置 tv.tv_usec = 0。你离开它未初始化。如果 tv_usec 恰好是一个无效值(小于 0 或大于 999,999),则 setsockopt() 调用应该失败。

关于c++ - UDP winsock 服务器 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33878856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com