Linux阻塞udp套接字无法接收

发布于 2024-09-08 07:12:30 字数 198 浏览 0 评论 0原文

我正在 ubuntu 机器上打开 udp 阻塞套接字,它成功(没有返回错误)。

当从另一台机器向该机器和端口发送数据时,接收不会中断,并且在wireshark嗅探器中我看到Icmp错误“端口无法访问”。

我认为这可能是 iptables 问题并打开接受端口。

还有其他建议如何调试这个吗?

谢谢。

提米

I am openning a udp blocking socket on an ubuntu machine, it goes successful (no errors returned).

When sending data to that machine and port from another machine the receive doesnt breaches and in a wireshark sniffer I see and Icmp error "port unreachable".

I though it might be an iptables problem and opened the port for accept.

Any other suggestions how to debug this?

Thanks.

Timmy

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

白馒头 2024-09-15 07:12:30

我通常使用 netcat 来确定问题是来自网络/防火墙还是来自我自己的代码,

尝试使用 netcat 运行测试服务器:
例如。

nc -l -u -p 9999 

将打开并侦听 udp 套接字,端口 9999。

现在,您可以尝试使用“然后输入一些内容”从同一台计算机或另一台计算机发送数据包

nc -u <ipaddress> 9999

,看看它是否到达第一台计算机。

netcat 中还有很多其他很酷的东西,请查看手册。

I usually use netcat to figure out if the problem comes from the network/firewall or from my own code

try running a test server with netcat :
eg.

nc -l -u -p 9999 

will open and listen an udp socket, port 9999.

Now you can try to send a packet from the same or from another computer using

nc -u <ipaddress> 9999

Then type something and see if it reaches the first computer.

There are a lot of other cool stuffs in netcat, have a look on the manual.

绿萝 2024-09-15 07:12:30

您是否使用 bind() 将套接字正确绑定到本地端口?

您是否记得通过 htons() 传递本地端口号以将其转换为网络字节顺序?

bind() 返回什么值?

Are you using bind() to correctly bind the socket to the local port?

Did you remember to pass the local port number through htons() to convert it to network byte order?

What value did bind() return?

尹雨沫 2024-09-15 07:12:30

您应该展示一个最小的测试用例。

您在 中看到您的流程吗

sudo netstat -4lp

?它的套接字绑定到哪个本地地址(由 netstat 报告)?

You should show a minimal test case.

Do you see your process in

sudo netstat -4lp

? What local address is its socket bound to (as reported by netstat)?

狂之美人 2024-09-15 07:12:30

尝试这个简单的服务器,看看它是否适合您:

#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define BUFSZ 4096
#define PORTNUM 1099
char buffer[BUFSZ];

int main( int argc, char* argv[] )
{
    int fd;
    struct sockaddr_in servaddr, cliaddr;
    socklen_t clilen = sizeof( cliaddr );
    ssize_t nread;

    if (( fd = socket( AF_INET, SOCK_DGRAM, 0 )) == -1 )
        err( 1, "socket" );

    bzero( &cliaddr, sizeof( cliaddr ));
    bzero( &servaddr, sizeof( servaddr ));

    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl( INADDR_ANY );
    servaddr.sin_port = htons( PORTNUM );

    if ( bind( fd, ( struct sockaddr* )&servaddr, sizeof( servaddr )) == -1 )
        err( 1, "bind" );

    printf( "bound to %s:%d\n", inet_ntoa( servaddr.sin_addr ),
        ntohs( servaddr.sin_port ));

    while (( nread = recvfrom( fd, buffer, BUFSZ, 0,
        ( struct sockaddr* )&cliaddr, &clilen )) != -1 )
    {
        printf( "received %lu bytes from %s:%d\n", nread,
            inet_ntoa( cliaddr.sin_addr ),
            ntohs( cliaddr.sin_port ));
    }

    return 1;
}

看看您的代码中是否包含所有必需的步骤。

Try this simple server and see if it works for you:

#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define BUFSZ 4096
#define PORTNUM 1099
char buffer[BUFSZ];

int main( int argc, char* argv[] )
{
    int fd;
    struct sockaddr_in servaddr, cliaddr;
    socklen_t clilen = sizeof( cliaddr );
    ssize_t nread;

    if (( fd = socket( AF_INET, SOCK_DGRAM, 0 )) == -1 )
        err( 1, "socket" );

    bzero( &cliaddr, sizeof( cliaddr ));
    bzero( &servaddr, sizeof( servaddr ));

    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl( INADDR_ANY );
    servaddr.sin_port = htons( PORTNUM );

    if ( bind( fd, ( struct sockaddr* )&servaddr, sizeof( servaddr )) == -1 )
        err( 1, "bind" );

    printf( "bound to %s:%d\n", inet_ntoa( servaddr.sin_addr ),
        ntohs( servaddr.sin_port ));

    while (( nread = recvfrom( fd, buffer, BUFSZ, 0,
        ( struct sockaddr* )&cliaddr, &clilen )) != -1 )
    {
        printf( "received %lu bytes from %s:%d\n", nread,
            inet_ntoa( cliaddr.sin_addr ),
            ntohs( cliaddr.sin_port ));
    }

    return 1;
}

See if all the required steps are there in your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文