我可以关闭并重新打开套接字吗?

发布于 2024-08-24 23:48:54 字数 465 浏览 4 评论 0原文

我学习了一个使用套接字的例子。在此示例中,客户端向服务器发送请求以打开套接字,然后服务器(侦听特定端口)打开套接字,一切都很好,套接字从双方(客户端和服务器)“打开”。

但我仍然不清楚这个东西有多灵活。例如,客户端是否可以关闭一个打开的(从两端)套接字并再次重新打开它(在服务器保持套接字打开的情况下)。

服务器是否有可能“知道”客户端的套接字已关闭?客户端是否有可能知道服务器端的套接字已关闭?

添加:

对我来说还有一件重要的事情。如果应用程序(无论是服务器还是客户端)崩溃、异常终止、终止,会发生什么?它会关闭应用程序一侧打开的所有套接字吗?

添加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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

呆萌少年 2024-08-31 23:48:54

套接字可用于很多事情,这些问题的答案会发生变化,但我假设您正在谈论 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.

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).

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.

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?

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.

蓝海似她心 2024-08-31 23:48:54

服务器是否可以
“知道”套接字已关闭
客户端?

当服务器尝试向该客户端发送一些数据时,将抛出相应的异常。

还有一件对我来说更重要的事情。什么
如果应用程序发生(无论
服务器或客户端)异常崩溃
终止、被杀?会全部关闭吗
插座在侧面打开
应用程序?

创建异常是为了处理这些异常情况。如果发生停电并且客户端(或服务器)被关闭,那么另一方一旦尝试与关闭的一方交互就会收到异常。

更新:

如果应用程序位于一侧怎么办?
套接字被关闭(杀死,
关闭、终止),然后是
再次开机(同一IP
地址和相同的端口)。我们应该
在两者之间创建一个新套接字
应用程序或者我们可以使用旧的
套接字(崩溃前创建)。

创建新套接字。

Is it possible for the server to
"know" that a socket was closed on the
client side?

When server tries to send some data to that client a correspondent exception will be thrown.

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?

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:

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).

Create new socket.

苏别ゝ 2024-08-31 23:48:54

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文