是否可以无限期保持套接字打开

2022-01-19 00:00:00 sockets c network-programming java

我是网络编程新手.我很感兴趣是否可以通过 Java 服务器和 C 客户端之间的套接字建立网络连接并保持连接无限打开?我想保持连接打开以在客户端之间交换 XML 数据.

I'm new to network programming. I'm interested is it possible to establish network connection via sockets between Java server and C client and keep the connection open infinitely? I want to keep the connection open to exchange XML data between the clients.

推荐答案

理论上可以无限期地保持服务器套接字打开;但是,客户端套接字不能这样做.主要原因是客户端socket依赖于服务端socket处理数据,而服务端socket可能会关闭连接.

Is is theoretically possible to keep server sockets open an indefinitely long amount of time; however, it is not possible to do so with the client-side socket. The main reason why is because the client side socket is dependent on the server side socket handling the data, and the server socket may close the connection.

虽然可以无限期地保持连接打开,但实际方面通常会确保此类套接字不会永远保持打开状态.网络中断、中间路由器配置错误、带宽耗尽、计算能力不足等,所有这些都共同确保点对点连接充其量只是暂时的.

While it might be possible to keep a connection open indefinitely, practical aspects typically ensure that such sockets don't stay open forever. Network outages, misconfiguration in intermediate routers, exhaustion of bandwidth, lack of computing power, etc. all collude to ensure that point to point connections are at best temporary.

请注意,服务器端套接字是一个接收连接请求的套接字(产生具有自己的返回套接字的连接),因此即使没有连接也可以打开它.如果您将套接字视为与另一台计算机的开放连接,则需要重新调整思路,让服务器套接字有意义.

Note that a server side socket is a socket that receives requests to connect (spawning off connections which have their own return sockets), so it can be open even when there are no connections. If you think of a socket as an open connection to another computer, you will need to reorient your thinking to have server sockets make any sense.

相关文章