简体   繁体   English

通过套接字连接销毁Java对象

[英]Java object destruction with socket connection

So i have a Client class with a socket connection. 所以我有一个带有套接字连接的Client类。 When a client connects to the server i start by doing a read on that socket to get so information about the client. 当客户端连接到服务器时,我首先对该套接字进行读取以获取有关客户端的信息。 Then i move the client to a table that has all the connected clients (those who sent their information). 然后,我将客户端移动到具有所有已连接客户端(发送其信息的客户端)的表中。 Then main socket on which i listen for new connections is where i create clients. 然后,我在其中侦听新连接的主套接字就是创建客户端的地方。 The problem is that i am not sure when and if an object with a pending read on a socket is destroyed. 问题是我不确定何时以及是否销毁了套接字上具有待决读取的对象。 My current strategy is having a second table holding all the clients that have not sent their information yet (the unconnected clients if you will). 我当前的策略是拥有另一个表,该表包含所有尚未发送信息的客户端(如果有的话,则为未连接的客户端)。 And once a clients sends his information i move him from the unconnected table to the connected table. 一旦客户发送了他的信息,我便将他从未连接表移至已连接表。 I really hate this method. 我真的很讨厌这种方法。 I want to simply create the client in the accept callback without adding it to a table. 我只想在accept回调中创建客户端,而不将其添加到表中。 And once the information is received move the client to the connected table. 一旦收到信息,就将客户端移至连接的表。

my Current implementation: 我当前的实现:

myMainChannel.accept(null, new CompletionHandler<AsynchronousSocketChannel,Void>() 
        {
                public void completed(final AsynchronousSocketChannel ch, void att) 
                {
                    // accept the next connection
                    myMainChannel.accept(null, this);
                    //add client to unconnected table
                    allUnconnectedClients.put(ch, new Client(ch));
                }
                public void failed(Throwable exc, Void att) 
                {                       
                    exc.printStackTrace();  
                }

         });

Inside the constructor of the client is the first read operation. 客户端的构造函数内部是第一个读取操作。

what id like to do is simply create the client without adding it to the table. id想要做的就是简单地创建客户端而不将其添加到表中。 So in other words repalce this: 所以换句话说:

allUnconnectedClients.put(ch, new Client(ch));

with this: 有了这个:

new Client(ch);

I know it's kinda weird just creating a local variable without using it but there is a read timeout (inside the constructor) that will close everything if nothing is received within 5 seconds. 我知道只是创建一个局部变量而不使用它有点奇怪,但是有一个读取超时(在构造函数内部),如果在5秒钟之内什么都没收到,它将关闭所有内容。

Since you probably would be waiting for either a read or a write at any given time you can use the read/write methods that accept an attachment + completion handler at any given time. 由于您可能会在任何给定时间等待读取或写入,因此可以使用在任何给定时间接受附件+完成处理程序的读/写方法 Your Client would be the attachment. 您的Client就是附件。 The handler would be responsible to register the next read or write callback. 处理程序将负责注册下一个读取或写入回调。 If it doesn't then the client would become eligible for garbage collection. 如果没有,那么客户端将有资格进行垃圾回收。

Think of it as continuation passing. 认为它是延续过去。

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

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