C UDP 套接字不工作

发布于 2024-12-28 10:05:24 字数 1706 浏览 0 评论 0原文

我在 C 中的套接字编程方面的作业之一遇到了问题。我们正在使用 UDP 做一个简单的客户端/服务器事情,而我在服务器方面遇到了麻烦。这是一个片段:

int main(int argc, char *argv[])
{
    int sockfd, newsockfd, portno;
    socklen_t clilen;
    char buffer[PACKET_DATA_LENGTH];
    struct sockaddr_in serv_addr, cli_addr;
    int n;
    if (argc < 2) {
        fprintf(stderr,"ERROR, no port provided\n");
        exit(1);
    }
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0)
        error("ERROR opening socket");

最初是 sockfd = socket(AF_NET, SOCK_DGRAM, 0) 是一个 SOCK_STREAM,但由于项目规范是在 UDP 而不是 TCP 中进行,所以我更改了STREAMDGRAM

我这样调用服务器: ./receiver 1234

并且收到错误: 接受时出错:套接字上不支持操作

但是,如果我将其从 改回来DGRAMSTREAM 它编译并且 ./receiver 1234 工作正常。

帮助?

谢谢! -kstruct

编辑 这就是 error() 的样子(这是预先给定的代码,我没有编写它,它是项目的一部分)

void error(const char *msg)
{
    perror(msg);
    exit(1);
}

EDIT2 这是我的更多代码(紧随我发布的第一块代码之后的代码)..我猜样板代码确实调用了accept(),但这不应该触发不同的输出消息吗?也就是说,它不应该说ERROR opening socket而不是ERROR on Accept吗?

bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) 
      error("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0) 
    error("ERROR on accept");

I'm having a problem with one of my homework assignments on socket programming in C.. we're doing a simple client/server thing with UDP and I'm having trouble with the server.. here's a snippet:

int main(int argc, char *argv[])
{
    int sockfd, newsockfd, portno;
    socklen_t clilen;
    char buffer[PACKET_DATA_LENGTH];
    struct sockaddr_in serv_addr, cli_addr;
    int n;
    if (argc < 2) {
        fprintf(stderr,"ERROR, no port provided\n");
        exit(1);
    }
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0)
        error("ERROR opening socket");

Originally the sockfd = socket(AF_NET, SOCK_DGRAM, 0) was a SOCK_STREAM, but since the project specification was to do it in UDP and not TCP I changed STREAM to DGRAM.

I call the server like: ./receiver 1234

And I get the error: ERROR on accept: Operation not supported on socket

However, if I change it back from DGRAM to STREAM it compiles and ./receiver 1234 works fine.

Help?

Thanks!
-kstruct

EDIT
This is what error() looks like (this is pre-given code, I did not write it, it came as part of the project)

void error(const char *msg)
{
    perror(msg);
    exit(1);
}

EDIT2
Here's more of my code (what follows immediately after the first block of code I posted).. I guess the boilerplate code does call accept(), but shouldn't that trigger a different output message? Namely, shouldn't it say ERROR opening socket rather than ERROR on accept?

bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) 
      error("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0) 
    error("ERROR on accept");

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

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

发布评论

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

评论(2

叶落知秋 2025-01-04 10:05:24

UDP 是无连接的。为什么要调用accept

UDP is connectionless. Why would you be calling accept?

初与友歌 2025-01-04 10:05:24

accept(2) 对于无连接数据报协议(例如 UDP)没有意义。 accept(2) 仅适用于 SOCK_STREAMSOCK_SEQPACKET 协议。

可以connect(2)一个UDP协议来要求内核提供更多的过滤,但这并不总是可取的。

accept(2) does not make sense for a connectionless datagram protocol such as UDP. accept(2) is only for SOCK_STREAM and SOCK_SEQPACKET protocols.

You can connect(2) a UDP protocol to ask the kernel to provide more filtering but this is not always desirable.

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