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

前端之家收集整理的这篇文章主要介绍了java – 我可以关闭并重新打开套接字吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我学习了套接字使用的一个例子.在此示例中,客户端向服务器发送请求以打开套接字然后服务器(侦听特定端口)打开套接字并且一切正常,套接字从双方(客户端和服务器)“打开”.

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

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

添加

对我来说更重要的一件事.如果应用程序(无主服务器或客户端)崩溃,异常终止,被杀,会发生什么?它会关闭应用程序侧面打开的所有套接字吗?

增加2:

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

解决方法

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

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

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?

为处理这些异常情况创建了例外.如果出现黑屏并关闭客户端(或服务器),则一旦尝试与关闭端进行交互,另一方将获得异常.

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

创建新套接字.

原文链接:https://www.f2er.com/java/127393.html

猜你在找的Java相关文章