0

大家好,我正在尝试通过 java 做一个 echo 服务器,但它不工作.. 我不知道为什么.. 但似乎服务器正在等待客户端,客户端正在等待服务器......所以他们不能互相传递信息..

这是服务器的代码

    ServerSocket server = null;
        try {
             server = new ServerSocket(3333);
              System.out.println("Listening on 3333");
    } catch (IOException ex) {
        System.out.println("Error can't connect to 3333");
        System.exit(1);
    }

        Socket clientSocket = null;
    try {
        clientSocket = server.accept();
    } catch (IOException ex) {
        System.out.println("Accept fail");
        System.exit(1);
    }
    PrintWriter out = null;
    try {
         out = new PrintWriter(clientSocket.getOutputStream());
    } catch (IOException ex) {
        Logger.getLogger(JavaApplication20.class.getName()).log(Level.SEVERE, null, ex);
    }
    BufferedReader br = null;
    try {
        br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    } catch (IOException ex) {
        Logger.getLogger(JavaApplication20.class.getName()).log(Level.SEVERE, null, ex);
    }
    BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

    String inputLine, outputLine;
 while(!(inputLine=br.readLine()).equals("bye"))
            {
                out.print("echo: " + inputLine);

            }
    out.close();
    br.close();
    clientSocket.close();
    server.close();
    System.out.println("Server Exited");

这是客户端的代码

 Socket client = null;
   try {
        client = new Socket("localhost", 3333);
        System.out.println("Connected on 3333");
    } catch (UnknownHostException ex) {
        System.out.println("Couldn't connect to the server");
        System.exit(1);
    } catch (IOException ex) {
        Logger.getLogger(KnockKnockClient.class.getName()).log(Level.SEVERE, null, ex);
    }


    PrintWriter out = null;
    BufferedReader in = null;
    BufferedReader stdIn = null;

    try {
        out = new PrintWriter(client.getOutputStream(), true);
    } catch (IOException ex) {
        Logger.getLogger(KnockKnockClient.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        in = new BufferedReader(new InputStreamReader(client.getInputStream()));
    } catch (IOException ex) {
        Logger.getLogger(KnockKnockClient.class.getName()).log(Level.SEVERE, null, ex);
    }
    stdIn = new BufferedReader(new InputStreamReader(System.in));

    String fromServer, fromUser;


    while((fromUser=stdIn.readLine())!=null)
        {
            System.out.println("From user: "+ fromUser);

            out.print(fromUser);
            fromServer=in.readLine();
            System.out.println(fromServer);
        }



    out.close();
    stdIn.close();
    in.close();
    client.close();

    System.out.println("client Exited");

有什么帮助吗?

4

2 回答 2

3

您正在从客户端发送一些字符串("Hello"例如),并且您正在尝试readLine()在服务器上读取它(反之亦然)。readLine()只会在找到 EOL 字符或输入流关闭后返回。

由于客户端不发送任何 EOL 字符,服务器无限期地等待,客户端也因为它等待来自服务器的回答。

发送"Hello\n",它会更好地工作。

于 2012-12-07T17:21:43.247 回答
1

在您的客户端和服务器中out.print(fromUser);使用后。out.flush()flush 将确保它正确地连接到套接字。

while((fromUser=stdIn.readLine())!=null)
    {
        System.out.println("From user: "+ fromUser);

        out.print(fromUser);
        out.flush();
        fromServer=in.readLine();
        System.out.println(fromServer);
    }



out.close();
stdIn.close();
in.close();
client.close();

关于flush,摘自java doc。

Flushes the stream. If the stream has saved any characters from the various write()      methods in a buffer, write them immediately to their intended destination. Then, if that destination is another character or byte stream, flush it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams. 

  If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive.
于 2012-12-07T17:17:29.880 回答