2

我使用 Socket 在 java 中有一个简单的客户端/服务器聊天。问题是程序相互连接,但由于某些原因我无法从双方获取/接收数据。当我断开服务器时,客户端给我一个错误“连接重置”,表明它们已连接但它们不交换数据。

该代码与取自此处的 Java 教程中的代码相同。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication3;

/**
 *
 * @author Amr
 */
import java.net.*;
import java.io.*;

public class KnockKnockServer {
    public static void main(String[] args) throws IOException {

        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(4440);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 4444.");
            System.exit(1);
        }

        Socket clientSocket = null;
        try {
            clientSocket = serverSocket.accept();
            System.out.println(Inet4Address.getLocalHost());
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }

        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(
                new InputStreamReader(
                clientSocket.getInputStream()));
        String inputLine, outputLine;


        while ((inputLine = in.readLine()) != null) {
             outputLine = "heelo";
             out.println(outputLine);
             if (outputLine.equals("Bye."))
                break;
        }
        out.close();
        in.close();
        clientSocket.close();
        serverSocket.close();
    }
}

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

public class KnockKnockClient {
    public static void main(String[] args) throws IOException {

        Socket kkSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;

        try {
            kkSocket = new Socket(Inet4Address.getLocalHost(), 4440);
            out = new PrintWriter(kkSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: taranis.");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: taranis.");
            System.exit(1);
        }

        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
        String fromServer;
        String fromUser;

        while ((fromServer = in.readLine()) != null) {
            System.out.println("Server: " + fromServer);
            if (fromServer.equals("Bye."))
                break;

            fromUser = stdIn.readLine();
        if (fromUser != null) {
                System.out.println("Client: " + fromUser);
                out.println(fromUser);
        }
        }

        out.close();
        in.close();
        stdIn.close();
        kkSocket.close();
    }
}
4

3 回答 3

5

建立连接后,您的服务器要做的第一件事是

while ((inputLine = in.readLine()) != null)

也就是说,它等待客户说些什么。

你的客户做的第一件事是

while ((fromServer = in.readLine()) != null)

也就是说,它等待服务器说些什么。

让两者中的一个先发送一些东西,它应该可以工作。

于 2013-04-23T08:18:49.433 回答
2

该教程中缺少的部分是此块:

// initiate conversation with client
KnockKnockProtocol kkp = new KnockKnockProtocol();
outputLine = kkp.processInput(null);
out.println(outputLine);

目前,您没有初始化对话,因此其他海报指出了套接字读取阻塞。

于 2013-04-23T08:25:55.413 回答
1

您在客户端和服务器上都有一个线程,该线程BufferedReader使用readLine()阻塞的方法从各自的 .

由于客户端和服务器都希望在它们中的任何一个发送任何内容之前相互读取,因此它们都会坐在那里并阻塞。您要么需要分叉并拥有一个从 读取的线程BufferedReader和一个写入的线程PrintWriter,或者您必须拥有:

outputLine = "heelo";
out.println(outputLine);

在你的while循环之外。

于 2013-04-23T08:19:08.183 回答