为什么不接受()阻止?(Why doesn't accept() block?)

编程入门 行业动态 更新时间:2024-10-25 16:26:01
为什么不接受()阻止?(Why doesn't accept() block?)

我是Linux(UNIX)套接字下的socket编程新手。 我在Internet上找到了以下代码,用于为每个连接生成线程的tcp服务器。 但它不起作用。 accept()函数立即返回,并且不等待连接。 我究竟做错了什么 ?

这是代码

int main(int argv, char *args[]) { struct sockaddr_in addr; int sd, port; port = htons(SERVER_PORT); /*--- create socket ---*/ sd = socket(PF_INET, SOCK_STREAM, 0); if ( sd < 0 ) panic("socket"); /*--- bind port/address to socket ---*/ memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = port; addr.sin_addr.s_addr = INADDR_ANY; /* any interface */ if ( bind(sd, (struct sockaddr*)&addr, sizeof(addr)) != 0 ) panic("bind"); /*--- make into listener with 10 slots ---*/ if ( listen(sd, 10) != 0 ) panic("listen") /*--- begin waiting for connections ---*/ else { int sd; pthread_t child; FILE *fp; while (1) /* process all incoming clients */ { sd = accept(sd, 0, 0); /* accept connection */ fp = fdopen(sd, "wr+"); /* convert into FILE* */ pthread_create(&child, 0, servlet, fp); /* start thread */ pthread_detach(child); /* don't track it */ } } }

I'm new in socket programming under Linux (UNIX) sockets. I found the following code in the Internet, for a tcp-server that spawns a thread for each connection. However it doesn't work. the accept() function returns instantly, and doesn't wait for connection. What am I doing wrong ?

this is the code

int main(int argv, char *args[]) { struct sockaddr_in addr; int sd, port; port = htons(SERVER_PORT); /*--- create socket ---*/ sd = socket(PF_INET, SOCK_STREAM, 0); if ( sd < 0 ) panic("socket"); /*--- bind port/address to socket ---*/ memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = port; addr.sin_addr.s_addr = INADDR_ANY; /* any interface */ if ( bind(sd, (struct sockaddr*)&addr, sizeof(addr)) != 0 ) panic("bind"); /*--- make into listener with 10 slots ---*/ if ( listen(sd, 10) != 0 ) panic("listen") /*--- begin waiting for connections ---*/ else { int sd; pthread_t child; FILE *fp; while (1) /* process all incoming clients */ { sd = accept(sd, 0, 0); /* accept connection */ fp = fdopen(sd, "wr+"); /* convert into FILE* */ pthread_create(&child, 0, servlet, fp); /* start thread */ pthread_detach(child); /* don't track it */ } } }

最满意答案

您正在隐藏sd变量,将无效的套接字传递给accept() ,导致它立即失败。

它可能会返回EBADF以发出错误的文件描述符信号。 如果您在代码中检查了返回值,您会注意到。

您应该启用更多编译器警告,以捕获这些内容。 使用GCC,您可以使用-Wshadow选项启用此类警告。

You are shadowing the sd variable, passing an invalid socket to accept() which causes it to fail immediately.

It will likely return EBADF to signal a bad file descriptor. You would have noticed if you checked the return value in your code.

You should enable more compiler warnings, to catch things like these. With GCC you can use the -Wshadow option to enable such a warning.

更多推荐

本文发布于:2023-07-25 22:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1267001.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不接受   accept   block

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!