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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2456607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:14:45  来源:igfitidea点击:

Can I close and reopen a socket?

javasocketsnetworking

提问by Roman

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:

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

如果套接字一侧的应用程序关闭(终止、关闭、终止)然后再次打开(在相同的 IP 地址和相同的端口上)会怎样?我们应该在两个应用程序之间创建一个新的套接字还是我们可以使用旧的套接字(在崩溃之前创建)。

采纳答案by Roman

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

如果套接字一侧的应用程序关闭(终止、关闭、终止)然后再次打开(在相同的 IP 地址和相同的端口上)会怎样?我们应该在两个应用程序之间创建一个新的套接字还是我们可以使用旧的套接字(在崩溃之前创建)。

Create new socket.

创建新的套接字。

回答by hao

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.

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

不,因为 TCP 将执行再见,您无法再次从那里获取连接。你必须再次进行三向握手,这将启动一个全新的连接。

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.

是的。TCP 可以发送再见数据包,或者一方可以超时,在这两种情况下完全有可能检测到这些情况。

回答by chandra kumar

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?

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

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.

A1 -> 服务器会知道,如果客户端关闭套接字,反之亦然。实际上FIN会从末端发送,即发起关闭连接。

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

Q2-> 如果套接字一侧的应用程序被关闭(终止、关闭、终止)然后再次打开(在相同的 IP 地址和相同的端口上)会怎样?我们应该在两个应用程序之间创建一个新的套接字还是我们可以使用旧的套接字(在崩溃之前创建)。

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.

A2->如果创建了socket,fd号绑定了ip-addr和端口,所以服务器端的socket有server-ip和一些端口,客户端的socket有client-ip和一些端口。如果客户端发生崩溃,所有的文件都被释放并且不能被重用。如果我们使用相同的文件,系统会将其视为普通文件文件。