C UDP 套接字不工作
我在 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 中进行,所以我更改了STREAM
到 DGRAM
。
我这样调用服务器: ./receiver 1234
并且收到错误: 接受时出错:套接字上不支持操作
但是,如果我将其从 改回来DGRAM
到 STREAM
它编译并且 ./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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
UDP 是无连接的。为什么要调用
accept
?UDP is connectionless. Why would you be calling
accept
?accept(2)
对于无连接数据报协议(例如 UDP)没有意义。accept(2)
仅适用于SOCK_STREAM
和SOCK_SEQPACKET
协议。您可以
connect(2)
一个UDP协议来要求内核提供更多的过滤,但这并不总是可取的。accept(2)
does not make sense for a connectionless datagram protocol such as UDP.accept(2)
is only forSOCK_STREAM
andSOCK_SEQPACKET
protocols.You can
connect(2)
a UDP protocol to ask the kernel to provide more filtering but this is not always desirable.