套接字上的“连接"调用可以成功返回,而无需服务器调用“接受"吗? [英] Can 'connect' call on socket return successfully without server calling 'accept'?

查看:78
本文介绍了套接字上的“连接"调用可以成功返回,而无需服务器调用“接受"吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

服务器已创建一个套接字并绑定到端口,并启动了一个正在循环的线程以接受连接.有时由于异常导致线程退出而退出了稍后的循环,但是套接字仍然绑定到端口.现在,如果客户端与该服务器建立连接",则说明连接成功.这怎么可能?如果我理解正确,那么只有在服务器在侦听套接字上接受了接受"之后,连接"才返回.我在这里想念什么吗?

Server has created a socket and bound to a port and started a thread which is in loop to accept the connection. Sometime later loop exited due to an exception resulting in thread exit but socket is still bounded to port. Now if client does a 'connect' to this server, it is succeeding. How is it possible? If I understand correctly, 'connect' returns only after server does 'accept' on the listening socket. Am I missing something here?

推荐答案

如果我理解正确,则仅当服务器在侦听套接字上执行接受"操作后,连接"才返回.我在这里想念什么吗?

If I understand correctly, 'connect' returns only after server does 'accept' on the listening socket. Am I missing something here?

是的. TCP在幕后建立连接(三向握手),并在准备就绪时将其放入完整的连接队列中. Accept()从此队列的前面返回下一个等待的连接.

Yes. TCP establishes the connection - the 3-way handshake - under the covers and puts it in a completed connection queue when it is ready. Accept() returns the next waiting connection from the front of this queue.

从客户端的角度来看,它是连接"的,但是直到服务器接受并开始处理后,它才与任何人交谈.有点像当您致电公司并立即进入保留队列时.您已建立联系",但是除非有人真正接听并开始交谈,否则您将无法做任何事情.

From the client's perspective it is "connected" but it won't be talking to anyone until the server accepts and begins processing. Sort of like when you call a company and are immediately put in the hold queue. You are "connected" but no business is going to be done until someone actually picks up and starts talking.

您的单个​​线程可能已死亡,但是该进程仍在运行,并且文件描述符仍处于打开状态,因此TCP不知道应用程序级别正在发生什么.

Your individual thread may have died but the process is still alive and the file descriptor still open so TCP doesn't know what is going on at the application level.

这篇关于套接字上的“连接"调用可以成功返回,而无需服务器调用“接受"吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆