简体   繁体   English

尝试在按下 Enter 键时发送消息

[英]Trying to make send a message when the enter key is pressed

I am trying to send a message to the server.我正在尝试向服务器发送消息。 My problem is that the client keeps waiting for input and never sends it to the server until the last message that tells it to terminate (which is "bye").我的问题是客户端一直在等待输入并且永远不会将它发送到服务器,直到最后一条消息告诉它终止(即“再见”)。 After client terminates, the server will receive the message.客户端终止后,服务器将收到消息。 But I want the server to receive the message everytime the client hit's the enter key.但我希望服务器在每次客户端按下回车键时都能收到消息。

I am not sure, but I think that the problem is with the Bufferedwriter in the client, because if I connect to the server using the browser, the server receives all the browser information.我不确定,但我认为问题在于客户端中的Bufferedwriter ,因为如果我使用浏览器连接到服务器,服务器会收到所有浏览器信息。 An example is shown below:一个例子如下所示:

Connect to this server name: localhost and port: 121
Connection from client: Socket[addr=/0:0:0:0:0:0:0:1,port=61942,localport=121]
From client > GET / HTTP/1.1
From client > Host: localhost:121
From client > Connection: keep-alive
From client > Cache-Control: max-age=0
From client > Upgrade-Insecure-Requests: 1
From client > User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36
...

Relevant server code:相关服务器代码:

try {
    sers = new ServerSocket(port);
    client = sers.accept();
    
    reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
    writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
    
    boolean clientIsOnline = true;
    
    while (clientIsOnline) {
        print("Connection from client: " + client);
        
        while((msg = reader.readLine()) != null) {
            print("From client > " + msg);
            if(msg.toLowerCase().equals("bye"))
                print("Client left, server closed");
                clientIsOnline=false;
        }
        ss.close();
        reader.close();
        writer.close();
    }
    
} catch(Exception e) {
    print("Error starting  server: ");
    e.printStackTrace();
}

Relevant client code:相关客户端代码:

public Client(String adr, int port) {
    this.adr=adr;
    this.port=port;
    try {
        c=new Socket(adr, port);
        r = new BufferedReader(new InputStreamReader(c.getInputStream()));
        w = new PrintWriter(new OutputStreamWriter(c.getOutputStream()));

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

Client method for sending the message客户端发送消息的方法

private void msgToServer(String msg) {
    try {
        w.write(msg);
        w.flush();
    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    if(msg.toLowerCase().equals("bye")) {
        print("I am out, bye");
        try {
            c.close();
            r.close();
            w.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Client main method:客户端主要方法:

public static void main(String[] args) {

        int port=121;
        String msg =null;

        Scanner sc = new Scanner(System.in);
        Client c = new Client("localhost", port);

        System.out.println("Write something to server");
        while(true) {
            c.msgToServer(sc.next());
        }
    }

I also tried using the PrintWriter instead of BufferedWriter, and also tried adding the true keyword as parameter, like我还尝试使用PrintWriter而不是 BufferedWriter,并尝试添加true关键字作为参数,例如

new PrintWriter(new OutputStreamWriter(c.getOutputStream()), true);

Just to give an example of what it looks like using my client.只是举例说明使用我的客户端的情况。

I type hello hit enter (I expect it to send this, but it doesn't).我输入hello点击回车(我希望它发送这个,但它没有)。 Then I type my name is john hit enter again.然后我输入my name is john再次按回车键。 Finally I type bye I excpect:最后我输入bye我期望:

From client > hello
From client > my name is john 
From client > bye

What I get: From client > hellomynameisjohnbye我得到了什么: From client > hellomynameisjohnbye

I believe the newline is being consumed when the client reads the input, so it needs to be added when sent to the server.我相信当客户端读取输入时会消耗换行符,因此需要在发送到服务器时添加它。 In the client method:在客户端方法中:

private void msgToServer(String msg) {
    try {
        // re-introduce newline:
        w.println(msg);

    [SNIP]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM