可以在没有服务器调用“接受"的情况下成功返回套接字上的“连接"调用吗? [英] Can 'connect' call on socket return successfully without server calling 'accept'?

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

问题描述

服务器创建了一个套接字并绑定到一个端口,并启动了一个正在循环中的线程来接受连接.稍后由于异常导致线程退出而退出循环,但套接字仍绑定到端口.现在,如果客户端连接"到该服务器,它就成功了.这怎么可能?如果我理解正确,则仅在服务器在侦听套接字上接受"后才返回连接".我在这里遗漏了什么吗?

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 在幕后建立连接 - 3 次握手 - 并在准备好时将其放入已完成的连接队列中.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天全站免登陆