1

我第一次尝试为 php 套接字服务器编写客户端,但遇到了一些麻烦,我有点被信息淹没了!

对于服务器,我们需要一个打开的连接,我希望我的客户端等到它接收到数据后再通知线程开始解析输入流。这是否可以在不使用循环的情况下实现?我宁愿能够调用 lock.notify()。

我也在看 NIO,这是我想要的可行选择吗?这是我到目前为止的代码,但同样,我只是想避免 for(;;) 甚至可能将接收到的消息排队,因为它们很可能只是 JSON

 Thread serverRecieve = new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            for (;;) {
                if (in != null) {
                    String line;
                    while ((line = in.readLine()) != null) {
                        sout(line);
                    }
                } else {
                    sout("inputstream is null! Waiting for a second to test again");
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(WebManager.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(WebManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
});

多谢你们!PS:我确实在这里查看了很多套接字线程,但决定问我需要什么会更容易。

4

2 回答 2

0

我认为您可以使用while循环并使用in != nullas 设置条件:

    while(in == null){
       //wait for a second before checking the in stream again
       try {
            sout("inputstream is null! Waiting for a second to test again");
            Thread.sleep(1000);
       } catch (InterruptedException ex) {
          Logger.getLogger(WebManager.class.getName()).log(Level.SEVERE, null, ex);
       }    
    }

     //now your in is available. Read the data and proceed
     String line = null;
     while ((line = in.readLine()) != null) {
          sout(line);
     }

while一旦in流可用,第一个循环将终止。

于 2012-11-27T21:07:50.827 回答
0

如何创建Runnable用于从套接字读取的专用子类型,如下所示:

class Reader implements Runnable {

    private final Socket socket;
    private volatile boolean stopped;

    Reader(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            while (true) {
                int in = socket.getInputStream().read();
                // process in here
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (!stopped) socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void stop() {
        try {
            stopped = true;
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class Client {
    private volatile Reader reader;
    void start() {
        reader = new Reader(new Socket(serverHost, serverPort));
        Thread readerThread = new Thread(reader, "Reader-thread");
        readerThread.start();
    }
    void stop() {
        Reader reader = this.reader;
        // reader.stop() will close socket making `run()` method finish because of IOException
        // reader.socket is final, thus we have proper visibility of it's values across threads
        if (reader != null) reader.stop();
    }
}
于 2012-11-27T21:31:25.377 回答