简体   繁体   English

我如何知道套接字何时空闲?

[英]How can i know when a socket is idle?

I'm trying to write a client server application. 我正在尝试编写客户端服务器应用程序。 the clients post service requests and the server receives them and display them in a tech room. 客户发布服务请求,服务器接收请求并将其显示在技术室中。

everything was fine until I tried to send a screen capture to a special admin client, that should receive a screen capture of the list of live requests. 一切都很好,直到我尝试将屏幕截图发送给特殊的管理客户端,该客户端应该收到实时请求列表的屏幕截图。

my main server listening thread looks like this : 我的主服务器侦听线程如下所示:

while (true) {

         clientSocket = echoServer.accept();
         log.debug("Server Started Listening");
         is = new DataInputStream(clientSocket.getInputStream());
         os = new PrintStream(clientSocket.getOutputStream());

 // An option for a stop listening button. currently not available !
         if( listening==true )  {
         line = is.readUTF();
         os.println(line); 
         System.out.println(line);
         RequestReciever.pharseToRequest(line);
}

The pharseToRequest Method looks like this : pharseToRequest方法如下所示:

public static void pharseToRequest(String input) {

    List<String> list = new ArrayList<String>(Arrays.asList(input.split(";;;")));
    if (list.get(0).equalsIgnoreCase("Login") && list.get(1).equalsIgnoreCase("Login") && list.get(2).equalsIgnoreCase("5"))
    {
        _adminClients.add(list.get(4));
        updateScreenCapture();
        AdminClientUpdate tmp = new AdminClientUpdate(list.get(4));
        Thread aCU = new Thread (tmp);
        aCU.start();
    }
    else
    {
    ServerRequest newReq = new ServerRequest(list.get(0), list.get(1), Integer.parseInt(list.get(2)),list.get(3),list.get(4));
    addRequest(newReq);
    }
}

when the client sends "Login" as the data, I treat him as an ADMIN CLIENT, and try to send it a screencapture, via the AdminclientUpdate thread. 当客户端将“登录”作为数据发送时,我将其视为ADMIN CLIENT,并尝试通过AdminclientUpdate线程向其发送屏幕截图。

and when a client sent regular data, I just update it on the server screen with the "addRequest" method. 当客户端发送常规数据时,我只需使用“ addRequest”方法在服务器屏幕上对其进行更新。

the problem I have is when I try to send the screen capture, it will find its way to the admin client and will be updated there correctly, but my server gets an error and stops working. 我遇到的问题是,当我尝试发送屏幕截图时,它将找到通往管理客户端的方式,并将在那里正确更新,但是我的服务器出现错误并停止工作。

I suspect it might have something to do with the listening thread still have data in it from the old request while the AdminclientUpdate thread transferring data, or something. 我怀疑这可能与监听线程有关,而在AdminclientUpdate线程传输数据时还是有旧请求中的数据。

This is the Error I get from the server , after sending the screen capture : 发送屏幕截图后,这是我从服务器收到的错误:

java.io.EOFException
    at java.io.DataInputStream.readUnsignedShort(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at ListeningThread.run(ListeningThread.java:50)
at java.lang.Thread.run(Unknown Source)

The serversocket.accept() continutes reading as is just received a new request from a client , when it really did not... instead of pausing and waiting for a new request... serverocket.accept()继续读取,实际上是从客户端收到新请求时,实际上并没有...而不是暂停并等待新请求...

I'm stuck with this for two weeks now, and very frustrated. 我已经坚持了两个星期了,非常沮丧。

please HELP. 请帮忙。

David. 大卫。

In my case The server should start a new thread to handle every accepted connection, instead of processing each one in-line in the accept thread. 就我而言,服务器应该启动一个新线程来处理每个接受的连接,而不是在接受线程中内联处理每个线程。 But also I tried to get the first update via the serversocket instead of the login initialization. 但是我也尝试通过服务器插座而不是登录初始化来获取第一次更新。 now, after getting the 1st update while logging in, I added a Server Socket on the client side so it will keep listening for further updates from server. 现在,在登录时获得第一个更新后,我在客户端添加了一个服务器套接字,因此它将继续侦听来自服务器的进一步更新。

EJP's idea gave me the first lead. EJP的想法给了我第一个线索。

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

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