简单的套接字通信程序根本不起作用

时间:2014-01-06 18:38:28

标签: java sockets ipc

服务器使用线程接受多个连接。

服务器:

@Override
public void run () {
    try {
        System.out.println(client);
        System.out.println("Client Connected");
        //So far, so good. Client is connected

        BufferedReader in = new BufferedReader(
                new InputStreamReader(this.client.getInputStream()));
        System.out.println(in.readLine());
        // Nothing happens
    } catch (Exception e) {
        e.printStackTrace();
    }

}

客户端:

try {
        PrintWriter out = new PrintWriter(Client.socket.getOutputStream());
        BufferedReader in = new BufferedReader (
                new InputStreamReader(Client.socket.getInputStream()));

        out.write("Information sent from client");
        // Does not work
        in.read();
        // Before this .read it would give a "Connection reset" probably
        // Because the client closed before the server could even read
    }

没有错误,但它只是挂起,没有发送任何内容。防火墙已经关闭,所以不可能。它曾经在昨天工作。我不知道我可能搞砸了什么。

1 个答案:

答案 0 :(得分:2)

write仅将内容写入InputStream。使用println发送一个额外的换行符,以对应服务器上的readLine语句。

out.println("Information sent from client");