UDP 示例运行时失败
UDP sample runtime failed
#include<stdio.h>
#include<winsock2.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
#define SERVER "127.0.0.1"
#define BUFLEN 512 //Max length of buffer
#define PORT 8888 //The port on which to listen for incoming data
int main()
{
SOCKET s;
struct sockaddr_in server, si_other;
int slen, recv_len;
char buf[BUFLEN];
WSADATA wsa;
slen = sizeof(si_other);
//Initialise winsock
printf("nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
{
printf("Failed. Error Code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
printf("Initialised.n");
//Create a socket
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET)
{
printf("Could not create socket : %d", WSAGetLastError());
}
printf("Socket created.n");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(PORT);
si_other.sin_addr.S_un.S_addr = inet_addr(SERVER);
//Bind
if (bind(s, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR)
{
printf("Bind failed with error code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
puts("Bind done");
//keep listening for data
while (1)
{
printf("Waiting for data...");
fflush(stdout);
//clear the buffer by filling null, it might have previously received data
memset(buf, ' ', BUFLEN);
//try to receive some data, this is a blocking call
if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen)) == SOCKET_ERROR)
{
printf("recvfrom() failed with error code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
//print details of the client/peer and the data received
printf("Received packet from %s:%dn", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
printf("Data: %sn", buf);
//now reply the client with the same data
if (sendto(s, buf, recv_len, 0, (struct sockaddr*) &si_other, slen) == SOCKET_ERROR)
{
printf("sendto() failed with error code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
}
closesocket(s);
WSACleanup();
return 0;
}
我使用此示例来学习 c++ 中的 UDP,但是当我实现它时。
C:Userscc>ncat -vv -u localhost 8888
Ncat: Version 7.12 ( https://nmap.org/ncat )
NCAT DEBUG: Using trusted CA certificates from C:Program Files (x86)Nmapca-bundle.crt.
libnsock nsock_iod_new2(): nsock_iod_new (IOD #1)
libnsock nsock_connect_udp(): UDP connection requested to ::1:8888 (IOD #1) EID 8
libnsock nsock_trace_handler_callback(): Callback: CONNECT SUCCESS for EID 8 [::1:8888]
Ncat: Connected to ::1:8888.
libnsock nsock_iod_new2(): nsock_iod_new (IOD #2)
libnsock nsock_read(): Read request from IOD #1 [::1:8888] (timeout: -1ms) EID 18
libnsock nsock_readbytes(): Read request for 0 bytes from IOD #2 [peer unspecified] EID 26
j
libnsock nsock_trace_handler_callback(): Callback: READ SUCCESS for EID 26 [peer unspecified] (2 bytes): j.
libnsock nsock_write(): Write request for 2 bytes to IOD #1 EID 35 [::1:8888]
libnsock nsock_trace_handler_callback(): Callback: WRITE SUCCESS for EID 35 [::1:8888]
libnsock nsock_readbytes(): Read request for 0 bytes from IOD #2 [peer unspecified] EID 42
libnsock nsock_trace_handler_callback(): Callback: READ ERROR [远程主机强迫关闭了一个现有的连接。 (10054)] for EID 18 [::1:8888]
Ncat: 远程主机强迫关闭了一个现有的连接。 .
出现许多调试信息。然后我输入一个字符"j",结果与我预期的不一样。当我检查我的网络状态时,我发现端口 8888 已连接到 ip 0.0.0.0。我真的希望你能帮助我,谢谢。
UDP 0.0.0.0:8500 *:*
UDP 0.0.0.0:8888 *:*
UDP 0.0.0.0:51754 *:*
查看 ncat
命令的调试输出时,您将看到它尝试"连接"到地址::1:8888
这是 localhost
的 IPv6 地址。但是您的服务器正在等待 IPv4 地址上的数据以进行localhost
。
您需要告诉ncat
使用 IPv4 地址,方法是添加选项-4
强制使用 IPv4:
ncat -4 -vv -u localhost 8888
或者明确告诉它连接到 IPv4 地址进行localhost
:
ncat -vv -u 127.0.0.1 8888
相关文章:
- 运行时检查失败 #2 变量"A"周围的堆栈已损坏
- 运行时检查失败 #0 用于运行时重新编译
- 运行时检查失败 #2 - 变量"e"周围的堆栈已损坏。发生
- 运行时检查失败 #2 MSVC 仅使用 utf8proc 进行调试
- std::copy 导致运行时检查失败 #2
- 运行时检查失败 #2 - 变量周围的堆栈'...'已损坏
- 运行时检查失败 #2 - 变量"l1"周围的堆栈已损坏
- C++:此代码可以编译,但引发运行时检查失败 #2 - 围绕变量周围的堆栈'num'已损坏。发生
- 运行时检查失败 - 变量周围的堆栈已损坏
- C++,弹出调试断言失败窗口,我得到矢量迭代器不兼容的错误运行时
- 调试:运行时检查失败 #2 - 变量"LoggerThread"周围的堆栈已损坏
- NVCC编译了特征库,并在运行时失败的结构中的MatrixxD大小
- 运行时检查失败 #2 - 变量周围的堆栈'k'已损坏
- 运行时检查失败 #2 - 变量"数字选择"周围的堆栈已损坏
- 通过功能指针通过DLL进行回调功能,运行时检查失败#0错误
- 运行时检查失败#2 - 变量周围的堆栈'myArray'已损坏.- 似乎无法弄清楚如何删除此错误
- 运行时检查失败 #2 - 变量周围的堆栈'normalIndex'已损坏
- 错误:运行时检查失败 #2 - 变量周围的堆栈已损坏
- 运行时检查失败 #2 - 变量周围的堆栈'sample'已损坏
- 运行时检查失败 #2 - 变量周围的堆栈'ap'已损坏