垂悬在读书的处理程序螺纹

时间:2016-03-24 13:26:51

标签: java sockets

我正在尝试使用客户端和处理程序设置服务器。客户端应该询问用户的字符串。然后应将其写入OutputStream,由处理程序读入,然后将字符串保存到其自己的OutputStream,然后再将其传递回客户端。我知道这个程序完全没有意义,我只是试着了解服务器,客户端和处理程序的工作方式。

到目前为止,这是我的代码:

服务器

public class Server {
  public static void main (String args[]) throws IOException {
    int port = 8080;
    ServerSocket server = new ServerSocket(port);

    while (true) {
      System.out.println("Waiting for client...");
      Socket client = server.accept();

      System.out.println("Client from "+client.getInetAddress()+" connected.");
      Handler handler = new Handler(client);
      handler.run();
    }
  }
}

处理程序

class Handler extends Thread {
private Socket client;

public Handler(Socket c) {
    client = c;
}

public void run() {

    try {
        Thread.sleep(3000);
        System.out.println("1");
        PrintWriter out = new PrintWriter(new OutputStreamWriter(client.getOutputStream()));
        BufferedReader in = 
                  new BufferedReader(new InputStreamReader(client.getInputStream(),
                                                           "UTF-8"));

        System.out.println("2");
        String message = in.readLine();
        System.out.println("4");

        System.out.println("5");
        out.println(message);
        out.flush();
        client.close();
        System.out.println("Finish");

    } catch (IOException e1) {
        e1.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }   
}

}

客户端

public class Client {
  public static void main (String args[]) throws IOException {
    Socket server = new Socket("127.0.0.1", 8080);
    System.out.println("Attempting connection...");
    Scanner scan = new Scanner (System.in);
    System.out.println("Please enter a string:");
    String message = scan.next();

    PrintWriter out = new PrintWriter(new OutputStreamWriter(server.getOutputStream()));
    BufferedReader in = 
              new BufferedReader(new InputStreamReader(server.getInputStream(),
                                                       "UTF-8"));

    out.println(message);
    String messageReturn = in.read();
    scan.close();
    System.out.println("Server said: " + messageReturn);
    in.close();
    out.close();
  }
}

问题是当处理程序尝试读取消息时,处理程序似乎挂起。此问题与此处显示的类似:Socket problem - readline won't work properly

但是这个解决方案并不适合我。我已经尝试使用objectInputStreams而不是我当前的解决方案,但这也没有用。

2 个答案:

答案 0 :(得分:1)

我怀疑你的客户端代码是否会编译。返回类型是int。

String messageReturn = in.read();

无论如何,这应该有效:

package network;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class Client {
  public static void main (String args[]) throws IOException {
    Socket server = new Socket("127.0.0.1", 9890);
    System.out.println("Attempting connection...");
    Scanner scan = new Scanner (System.in);
    System.out.println("Please enter a string:");
    String message = scan.next();

    PrintWriter out = new PrintWriter(new OutputStreamWriter(server.getOutputStream()));
    BufferedReader in = new BufferedReader(new InputStreamReader(server.getInputStream(), "UTF-8"));

    out.println(message);

    // NOTE this
    String messageReturn = in.readLine();
    scan.close();
    System.out.println("Server said: " + messageReturn);
    in.close();
    out.close();
  }
}

注意端口已更改。

服务器输出
等待客户...... 客户端从/127.0.0.1连接。 1

客户输出
尝试连接...... 请输入一个字符串: 你好

答案 1 :(得分:-1)

问题可能是您没有使用您的回复发送转义序列。 ReadLine()等待它逃脱。 尝试使用您的回复发送“\ n”。有一篇关于使用带有套接字的readLine()和println()的问题的文章。它还展示了如何解决它。 ReadLine() / Println() and sockets