【问题标题】:Sockets not working插座不工作
【发布时间】:2014-08-19 15:51:54
【问题描述】:

我正在尝试制作一个简单的客户端和服务器来发送和接收文本。客户端等待命令行输入,然后将其发送到服务器,服务器在System.out 上显示它。但是,尽管客户端允许我输入下一行来发送,但服务器仍然无法从套接字读取。

客户:

public class Main implements Runnable{
    String [] args;
    Socket socket;
     PrintWriter printer;

    public Main(String [] args){
        this.args = args;
    }
    @Override
    public void run() {
        try {
            socket = new Socket(args[0], Integer.parseInt(args[1]));
            printer = new PrintWriter(socket.getOutputStream());
        } catch (UnknownHostException e){
            System.out.println("Unknown host.");
            System.exit(2);
        } catch (IOException e) {
            e.printStackTrace();
        }
        while(true){
            String textToSend = System.console().readLine();
            printer.print(textToSend);
        }
    }
}

printer.print(textToSend); 行不通。

服务器:

public class Main implements Runnable{
    String [] args;

    public Main(String [] args){
        this.args = args;
    }
    @Override
    public void run() {
        if (args == null){
            System.out.println("Argument for port missing");
            System.exit(2);
        }
        try {
            ServerSocket listener = new ServerSocket(Integer.parseInt(args[0]));

            while (true){
                Socket client = listener.accept();
                System.out.println("Client connected");
                BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));

                try{
                    while(true){
                        String text = in.readLine();
                        System.out.println(text);
                        if (in.read() == -1){
                            return;
                        }
                    }
                }catch (IOException e){
                    System.out.println("Client lost");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

欢迎任何帮助。

【问题讨论】:

  • 要具体。什么“不起作用”?你有错误吗?你的堆栈跟踪实际上告诉你什么?
  • 我什至没有得到任何堆栈跟踪。只是从未收到过文本。
  • 你的服务器上的run 还能运行吗?
  • @ScaryWombat 1. 它确实(从 main(String [] args) 摆脱 static 修饰符)。 2. 它打印我在System.in 中写的东西
  • 套接字停止工作 - Java 现在会发生什么? :)

标签: java sockets


【解决方案1】:

当你在做的时候

 if (in.read() == -1){
    return;
 }

你正在扔掉一个字符

还有,当你写作时

printer.print(textToSend); 

textToSend 没有CR,因此接收readLine 将阻塞。 尝试在textToSend 末尾添加CR 或使用println 方法

【讨论】:

  • 即使我发送多于一个字符也没有收到任何内容。
  • 啊,你和我一样发现了错误,很好(-:
  • 如果有要读取的字符,那么它会读取它,并且由于它没有被保存,所以我称之为throwing it away
  • 当然可以。他需要做的是检查该行是否为空。
【解决方案2】:

我发现错误:你必须添加换行符并调用flush:

      printer.print(textToSend + "\n");
      printer.flush();

这对我有用。

Flush 强制发送所有数据并清空所有缓冲区,以便发送数据。需要换行,以便接收端知道该行是完整的。

您检查是否没有发送数据应该是line == null,而不是read() == -1

这是我的整个演示代码:

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while(true){
    String textToSend = null;
    try {
       textToSend = reader.readLine();
       System.out.println("read text: " + textToSend);
    } catch (IOException e) {
        e.printStackTrace();
    }
    printer.print(textToSend + "\n");
    printer.flush();
    System.out.println("text send");
 }

服务器端的错误检查:

    String text = in.readLine();
    if (text == null){
          return;
    }
    System.out.println(text);

【讨论】:

  • 我启动了服务器,然后启动了客户端。在客户端控制台中输入“你好”,然后按 Enter。文本出现在服务器控制台上。
  • 我也改变的是控制台的使用
  • 是的,现在对我有用(忘记重建)。但它也确实会丢掉一个字符。
猜你喜欢
  • 2021-03-13
  • 1970-01-01
  • 2017-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-20
  • 1970-01-01
相关资源
最近更新 更多