简体   繁体   English

在Java中,是否有通过连接到其serverSocket的套接字来获取服务器对象的方法?

[英]In Java, is there a method to get hold of a server object through a socket connected to its serverSocket?

If you have a Java client Socket connected to a Java server's ServerSocket, how do you then obtain the Java server object in the client class? 如果您将Java客户端Socket连接到Java服务器的ServerSocket,那么如何在客户端类中获取Java服务器对象?

I have had a look at the Socket class and there seems to be no method for getting hold of a server object through the Socket.connect()ion. 我看过Socket类,似乎没有办法通过Socket.connect()离子获取服务器对象。

The reason I am asking, is that I would like to send an instruction from my client to the server to deregister the client from subscribing to further updates from the server. 我问的原因是,我想从我的客户端向服务器发送指令,以取消注册客户端从订阅服务器的进一步更新。 My server-client relationship is based on the Observer pattern. 我的服务器 - 客户端关系基于Observer模式。

To carry out the instruction, I believe I need to obtain the server object. 为了执行该指令,我相信我需要获取服务器对象。

I am asking this question because I have not found anything on Google or stackoverflow.com which combines the Observer pattern with server-client socket relationships. 我问这个问题是因为我没有在Google或stackoverflow.com上找到任何将Observer模式与服务器 - 客户端套接字关系相结合的内容。

Of course that may indicate my approach is terminally flawed, but if it is, let it be a warning to others :) 当然,这可能表明我的方法存在最终缺陷,但如果是,请将其作为对其他人的警告:)

Its not 100% clear what you're asking, but here goes. 它不是100%清楚你要问的是什么,但这里有。

If you want the remote client to indicate to the server side that it is done, then have it send a message that the server side understands to mean done, then simply close the socket object on the server side and on the client. 如果您希望远程客户端向服务器端指示它已完成,那么让它发送服务器端理解为已完成的消息,然后只需关闭服务器端和客户端上的套接字对象。 The server socket may continue listening for more connections if appropriate. 如果适当,服务器套接字可以继续侦听更多连接。

If the socket handling the client connection on the server side is to shut down the server socket so it will no longer listen for incoming connections, then simply pass both sockets to the code that is handling the socket which is handling the client connection. 如果处理服务器端客户端连接的套接字是关闭服务器套接字以便它不再侦听传入连接,那么只需将两个套接字传递给处理正在处理客户端连接的套接字的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM