套接字编程:输入流似乎阻止了输出流

时间:2011-03-17 22:23:05

标签: java sockets inputstream blocking outputstream

我正在尝试编写一个服务器 - 客户端通信应用程序,它使用两个独立的线程,一个用于输入,另一个用于输出。我有一个奇怪的“死锁”问题,但是:当一个线程读取输入,但客户端没有发送任何东西时,线程停止(因为它正在等待输入)。但是,由于某种原因,当输入线程被阻塞时,输出线程无法写入任何内容。

此代码示例中说明了这一点:

import java.io.*;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;

public class TestServer {
    public static void main(String... args) throws IOException {

        /* Creates a server socket that lurks about our port waiting for connections */
        ServerSocketChannel serverChannel = ServerSocketChannel.open();
        serverChannel.configureBlocking(false);
        serverChannel.socket().bind(new InetSocketAddress(4114));


        while(true){
            SocketChannel connectionChannel = serverChannel.accept();
            if(connectionChannel != null){
                final Socket connection = connectionChannel.socket();


                new Thread(){
                    public void run(){
                        try {
                            System.out.println("READING");
                            System.out.flush();
                            // If the next line is commented out, nothing blocks
                            connection.getInputStream().read();
                            System.out.println("DONE READING");
                            System.out.flush();
                        } catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }.start();

                new Thread(){
                    public void run(){
                        try {
                            System.out.println("WRITING");
                            System.out.flush();
                            new DataOutputStream(connection.getOutputStream()).writeBytes("AUGH!!!");
                            //connection.getOutputStream().write(5);
                            System.out.println("DONE WRITING");
                            System.out.flush();
                        } catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }.start();

                break;
            }
        }
    }
}

客户端代码:

import java.net.*;
import java.io.*;

public class TestClient {

    public static void main(String... args) throws IOException {
        Socket connection = new Socket("127.0.0.1", 4114); 
        while(true){
            System.out.println(connection.getInputStream().read());
        }
    }

}

上面的代码示例阻止,但如果服务器中的行被注释掉,则不会。这是为什么?套接字是否仅限于同时等待输入/输出?发生了什么事?

1 个答案:

答案 0 :(得分:2)

我不确定你为什么会看到这个,但它与使用频道有关。

如果用

替换此代码
ServerSocket ss = new ServerSocket(4114);
Socket connection = ss.accept();

它会按你的意愿工作。

相关问题