UDP示例运行时失败

Shiyi Han
#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, '\0', 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:%d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
        printf("Data: %s\n", 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:\Users\cc>ncat -vv -u localhost 8888
Ncat: Version 7.12 ( https://nmap.org/ncat )
NCAT DEBUG: Using trusted CA certificates from C:\Program Files (x86)\Nmap\ca-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命令的debug-output时,您会发现它尝试“连接”到地址::1:8888,该地址是的IPv6地址localhost但是您的服务器正在等待的IPv4地址上的数据localhost

您需要ncat通过添加-4强制使用IPv4的选项来告知要使用IPv4地址

ncat -4 -vv -u localhost 8888

或明确告诉它连接到IPv4地址localhost

ncat -vv -u 127.0.0.1 8888

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

运行时journalctl失败

来自分类Dev

Java中的运行时多态示例?

来自分类Dev

运行时间计算示例

来自分类Dev

Qt QThread运行时立即失败

来自分类Dev

Android从运行时失败卸载apk

来自分类Dev

运行时Unity IoC突然失败

来自分类Dev

创建内容运行时失败

来自分类Dev

Nan::AsyncQueue 在运行时失败

来自分类Dev

在运行时加载程序集,Autocad插件示例

来自分类Dev

包调整示例中的运行时错误-MATLAB

来自分类Dev

java protobuf运行时正式示例不起作用

来自分类Dev

运行时静脉-lte omnet++ 示例的问题

来自分类Dev

调整TCP / UDP服务器运行时

来自分类Dev

Excel VBA:运行时错误(对象“范围”的方法“值”失败),但仅在连续运行时

来自分类Dev

Junit测试在程序包上运行时失败,但在文件上运行时成功

来自分类Dev

Excel VBA:运行时错误(对象“范围”的方法“值”失败),但仅在连续运行时

来自分类Dev

关联类型族失败,当独立运行时可以工作

来自分类Dev

递归函数中的运行时检查失败

来自分类Dev

Apache Storm应用程序在运行时失败

来自分类Dev

通过SQL Job Agent运行时SSIS包失败

来自分类Dev

在终端中命令正常运行时,system()失败

来自分类Dev

运行时检查失败#2-围绕变量堆叠

来自分类Dev

通过docker运行时react-scripts构建失败

来自分类Dev

Pystan,运行时错误-初始化失败

来自分类Dev

运行时错误'1004:Range类的PasteSpecial方法失败

来自分类Dev

VC ++运行时错误:调试声明失败

来自分类Dev

类对象的运行时检查失败#2

来自分类Dev

当使用XCTool与logicTestBucketSize运行时,测试失败

来自分类Dev

通过触发器运行时SpreadsheetApp getRange失败

Related 相关文章

热门标签

归档