【问题标题】:Socket programming: Input Stream seems to block Output Stream套接字编程:输入流似乎阻塞了输出​​流
【发布时间】:2011-07-17 18:57:18
【问题描述】:

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

此代码示例对此进行了说明:

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());
        }
    }

}

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

【问题讨论】:

  • 我对这种行为感到惊讶,但在两个单独的线程中处理单个客户端的输入和输出对我来说似乎很奇怪。大多数基于线程的系统每个客户端使用一个线程。 (而且,我认为nio 的一大调用功能是它可以让您在不使用线程的情况下编写服务器——它是线程安全的吗?)
  • 我还注意到,如果我将 DataOutputStream 的行切换为仅使用直接 OutputStream 的行,事情就会开始工作。 DataOutputStream 是否发送了某种将这些事情搞砸的标头?怎么回事?
  • 我相信 DataOutputStream 不会发送任何标头数据,ObjectOutputStream 肯定会发送,但仍然会影响您的问题。

标签: java sockets inputstream blocking outputstream


【解决方案1】:

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

如果您将此代码替换为

ServerSocket ss = new ServerSocket(4114);
Socket connection = ss.accept();

它会按照你的意愿工作。

【讨论】:

    猜你喜欢
    • 2010-11-23
    • 2012-08-16
    • 1970-01-01
    • 2011-12-23
    • 2013-07-28
    • 2012-09-16
    • 1970-01-01
    • 2015-01-01
    • 2016-07-25
    相关资源
    最近更新 更多