我可以关闭并重新打开套接字吗?
我学习了一个使用套接字的例子。在此示例中,客户端向服务器发送请求以打开套接字,然后服务器(侦听特定端口)打开套接字,一切都很好,套接字从双方(客户端和服务器)“打开”。
但我仍然不清楚这个东西有多灵活。例如,客户端是否可以关闭一个打开的(从两端)套接字并再次重新打开它(在服务器保持套接字打开的情况下)。
服务器是否有可能“知道”客户端的套接字已关闭?客户端是否有可能知道服务器端的套接字已关闭?
添加:
对我来说还有一件重要的事情。如果应用程序(无论是服务器还是客户端)崩溃、异常终止、终止,会发生什么?它会关闭应用程序一侧打开的所有套接字吗?
添加2:
如果套接字一侧的应用程序被关闭(杀死、关闭、终止)然后再次打开(在相同的IP地址和相同的端口上)会怎么样。我们应该在两个应用程序之间创建一个新套接字,还是可以使用旧套接字(在崩溃之前创建)。
I learned an example of usage of sockets. In this example a client sends a request to a server to open a socket and then the server (listening to a specific port) opens a socket and everything is fine, socket is "opened" from both sides (client and server).
But it is still not clear to me how flexible is this stuff. For example, is it possible for the client to close an opened (from both ends) socket and to reopen it again (under condition that the server keeps the socket opened).
Is it possible for the server to "know" that a socket was closed on the client side? Is it possible for the client to know that a socket was closed on the server side?
ADDED:
One more important thing to me. What happens if a application (no mater server or client) crashes, abnormally terminated, killed? Will it close all sockets opened on the side of the application?
ADDED 2:
What if an application on one side of the socket is switched off (killed, closed, terminated) and then it is switched on again (on the same IP address and the same port). Should we create a new socket between the two applications or we can use the old socket (created before the crash).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
套接字可用于很多事情,这些问题的答案会发生变化,但我假设您正在谈论 TCP。
不可以,因为 TCP 将执行告别操作,并且您无法再次从那里建立连接。您必须再次进行三次握手,从而开始一个全新的连接。
是。 TCP 可以发送告别数据包,或者一侧可能超时,并且完全有可能在这两种情况下检测到这些情况。
A socket can be used for a lot of things for which the answers to these questions would change, but I'll assume you're talking about TCP.
No, because TCP will perform a goodbye and you can't pick up the connection from there again. You'd have to do the three-way handshake again, and that starts a brand new connection.
Yes. TCP can send out a goodbye packet or one side can time out and it's entirely possible to detect these scenarios in both cases.
当服务器尝试向该客户端发送一些数据时,将抛出相应的异常。
创建异常是为了处理这些异常情况。如果发生停电并且客户端(或服务器)被关闭,那么另一方一旦尝试与关闭的一方交互就会收到异常。
更新:
创建新套接字。
When server tries to send some data to that client a correspondent exception will be thrown.
Exceptions are created for handling these abnormal cases. If there is a black out and a client (or server) is turned off then other side will get an exception as soon as it try to interact with turned off side.
UPD:
Create new socket.
Q1->服务器是否有可能“知道”客户端的套接字已关闭?客户端是否有可能知道服务器端的套接字已关闭?
A1->服务器会知道客户端是否关闭套接字,反之亦然。实际上FIN会从一端发送,发起关闭连接。
Q2->如果套接字一侧的应用程序被关闭(杀死、关闭、终止),然后再次打开(在相同的 IP 地址和相同的端口上),会怎么样?我们应该在两个应用程序之间创建一个新套接字,还是可以使用旧套接字(在崩溃之前创建)。
A2->如果创建了socket,则fd号与ip-addr &绑定; port,所以服务器端的socket有server-ip &客户端的某些端口和套接字具有 client-ip &一些港口。如果崩溃发生在客户端,则所有 fd 都被释放,并且不能重复使用。如果我们使用相同的 fd,系统会将其视为普通文件 fd。
Q1->Is it possible for the server to "know" that a socket was closed on the client side? Is it possible for the client to know that a socket was closed on the server side?
A1 -> The sever will know, if client closes socket and vice versa. Actually FIN will be sent from the end, which is initiating to close connection.
Q2-> What if an application on one side of the socket is switched off (killed, closed, terminated) and then it is switched on again (on the same IP address and the same port). Should we create a new socket between the two applications or we can use the old socket (created before the crash).
A2->If socket is created, fd number is bind with ip-addr & port, so socket on server side has server-ip & some port and socket on client side has client-ip & some port. if crash happened at client the all fd are freed and it can not be reused.if we use the same fd, system will treat it as normal file fd.