Java:同步套接字输入

时间:2012-11-27 20:49:09

标签: java php multithreading sockets synchronize

我第一次尝试为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:我确实在这里查看了很多套接字线程,但我认为只要问我需要什么就更容易了。

2 个答案:

答案 0 :(得分:0)

我认为您可以使用while循环并使用in != null作为条件:

    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流可用时终止。

答案 1 :(得分: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();
    }
}