Java中的PrintWriter或任何其他输出流不知道“ \”。 [英] PrintWriter or any other output stream in Java do not know "\r\n"

查看:118
本文介绍了Java中的PrintWriter或任何其他输出流不知道“ \”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用PrintWriter或任何其他输出流在服务器和客户端程序之间发送消息时遇到麻烦。如果我使用println( abc)进行通讯,则可以正常使用,但是如果我使用print( abc\r\n),print( abc\n)或print( abc \r)。我的意思是它不起作用是因为readLine()不会结束,因为它似乎看不到换行符,并且它仍在等待 \r或 \n

I have trouble using PrintWriter or any other output stream to send message between server and client program. It works properly if I use println("abc") to communicate, but it does not work if I use print("abc\r\n"), print("abc\n") or print("abc\r"). What I mean by "it does not work" is that the readLine() will not end because it seems not to see "newline" character and it is still waiting for "\r" or "\n"

为了更加清楚,我将在下面简单地输入一些代码:
客户端:

To make it more clear, I will simply put some codes in the following: The client:

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

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

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

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


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

    String textFromClient;
    textFromClient = in.readLine(); // read the text from client
    System.out.println(textFromClient); 


    String textToClient = "Recieved";
    out.print(textToClient + "\r\n");  // send the response to client

    out.close();
    in.close();
    clientSocket.close();
    serverSocket.close();
  }
}

服务器:

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

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

    Socket socket = null;
    PrintWriter out = null;
    BufferedReader in = null;
    BufferedReader read = new BufferedReader(new InputStreamReader(System.in));

    try {
        socket = new Socket("localhost", 1234);
        out = new PrintWriter(socket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    } catch (UnknownHostException e) {
        System.err.println("Don't know about host");
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for the connection");
    }
    System.out.println("Connected");

    String textToServer;

    textToServer=read.readLine();
    out.print(textToServer + "\r\n" );  // send to server

    System.out.println(in.readLine()); // read from server

    out.close();
    in.close();
    read.close();
    socket.close();
  }
}

我要使用print()+ \ r\n而不是println(),因为我正在编写的服务器程序要求从服务器发送到客户端的消息最后必须包含 containr\n,例如。 这是从服务器到客户端的消息。我认为println()与print( \r\n)相同,所以我不认为我应该使用println()+ \r\n,否则它将有2个换行符。 (尽管我不知道客户端将如何从服务器读取信息,因为只要求我编写服务器程序。他们将编写客户端以测试我的服务器程序。)但是,我应该发送包含以下内容的消息: rrn结尾,但readLine()似乎无法识别它。那么我该怎么办?

I want to use print() + "\r\n" instead of println() because the server program I am writing has the requirement that the message sent from server to client needs to contain "\r\n" at the end, eg. "this is the message from server to client\r\n". I think println() is the same as print("\r\n"), So I do not think I should use println() + "\r\n", otherwise it will have 2 newlines. (Although I do not know how the client side will read from server because I am asked to just write server program only. They will write the client side to test my server program.) However, I am supposed to send the messages contain "\r\n" at the end but the readLine() seems not recognize it. So what could I do with it?

如果您不理解我的问题,可以尝试上面的代码,您就会明白我的意思了。

If you do not understand my question, you can try the codes above and you will know what I mean.

推荐答案

flush()从客户端写入服务器后:

flush() after you write from client to server :

out.print(textToServer + "\r\n" );  // send to server
out.flush(); // here, it should get you going.

有效。

这篇关于Java中的PrintWriter或任何其他输出流不知道“ \”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆