我可以关闭并重新打开套接字吗? [英] Can I close and reopen a socket?

查看:64
本文介绍了我可以关闭并重新打开套接字吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

添加:

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

添加了 2 个:

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

解决方案

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

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

<块引用>

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

异常是为了处理这些异常情况而创建的.如果发生停电并且客户端(或服务器)关闭,则另一端在尝试与关闭的端交互时会立即收到异常.

更新:

<块引用>

如果应用程序在插座已关闭(被杀死,关闭,终止)然后它是再次打开(在同一个 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).

解决方案

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.

这篇关于我可以关闭并重新打开套接字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆