简体   繁体   English

Java套接字聊天不显示所有消息

[英]Java Socket Chat Not Displaying All Messages

I am trying to work through a socket chat with just one client and the server. 我正在尝试仅与一个客户端和服务器进行套接字聊天。 I have it successfully running, as in the server is capable of passing messages back and forth, but when I attempt to make the server side implementation a bit more complex, adding commands and such, that the client can use, the communication fails. 我已经成功运行了它,因为在服务器中可以来回传递消息,但是当我尝试使服务器端实现变得更加复杂,添加命令以及客户端可以使用的命令时,通信将失败。 It appears it might go out of sync even as using the same commands over and over again can produce different results, even though I flush everything after every command. 似乎即使一次又一次地使用相同的命令会产生不同的结果,即使我在每个命令之后都刷新了所有内容,它也可能不同步。

Example of simplistic output, this works as expected, every time: 简化输出的示例,每次都能按预期工作:

Client: 客户:

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

public class Test1Client
{
public static void main(String args[])
{


    InputStreamReader convert = new InputStreamReader(System.in);
    BufferedReader stdin = new BufferedReader(convert);

    try
    {
        Socket echoClient = new Socket("localhost", 17);
        PrintStream outs = new PrintStream(echoClient.getOutputStream());
        BufferedReader ins = new BufferedReader(new InputStreamReader(echoClient.getInputStream()));
        while(true){
        System.out.print("Type whatever you want: ");
        String line = stdin.readLine();
        outs.println(line);
        System.out.println("Server says: " + ins.readLine());
        }

    }
    catch (IOException e)
    {
        System.out.println(e);
    }
}
}

Server: 服务器:

import java.net.*;
import java.util.ArrayList;
import java.io.*;


public class Test1Server
{
    public static void main(String args[])
    {
        try
        {
             ServerSocket socket= new ServerSocket(12167);
             //Try not to use port number < 2000. 
             System.out.println("Waiting for a client to connect..."); 
             Socket s = socket.accept();
            System.out.println("Client Connected.");
            BufferedReader ins = new BufferedReader(new InputStreamReader(s.getInputStream()));
            PrintStream outs = new PrintStream(s.getOutputStream());                 

      while (true)
             {

            String line = ins.readLine();
            outs.println(line); 

        }

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

I get output like this, it works every time just spitting it back out: 我得到这样的输出,每次将其吐出来时它都起作用:

Type whatever you want: login
Server says: login
Type whatever you want: login
Server says: login
Type whatever you want: login
Server says: login
Type whatever you want: login
Server says: login

But when I make the server side a bit more complex by replacing its while(true) block with the following, I get a much more messy result: 但是,当我通过使用以下内容替换其while(true)块使服务器端变得更复杂while(true) ,我得到的结果会更加混乱:

                String line = ins.readLine();
                String response = manager.process(line);
                outs.println(response); 
                outs.flush();

process: 处理:

msg= "User logged in successfully \n";
return msg;

You'll also notice some commented lines in the process command code. 您还会在process命令代码中注意到一些注释行。 When I give back a simple message the server seems to keep up, but when I use the login function as well it gives the terrible output like this: 当我给出一个简单的消息时,服务器似乎保持正常运行,但是当我使用登录功能时,它也会给出可怕的输出,如下所示:

Type whatever you want: login ryanne 
Server says: ryanne logged in successfully 
Type whatever you want: login ryanne 
Server says: 
Type whatever you want: login ryanne 
Server says: You may already be logged in or did not use correct username or password 
Type whatever you want: login ryanne 
Server says: 
Type whatever you want: newuser jeff 
Server says: You may already be logged in or did not use correct username or password 
Type whatever you want: newuser jeff 12345
Server says: 
Type whatever you want: new user jeff 12345
Server says: You may already be logged in or did not use correct username or password 
Type whatever you want: 

Again, notice the blanks where nothing comes back from the server, and then even the change in the commands does not prompt different responses. 再次注意空白处,服务器什么也没回来,然后即使命令中的更改也不会提示不同的响应。 Its as if it went out of sync, just by using one additional function? 仅使用一项附加功能就好像不同步了吗?

You have some "\\n" at the end of some strings. 在某些字符串的末尾有一些"\\n" If you both put "\\n" and use println , you will have double carriage returns, which will mess up your protocol. 如果同时输入"\\n"并使用println ,则将有双回车符,这会弄乱您的协议。 Remove the "\\n" 's, and it should work better. 删除"\\n" ,它应该可以更好地工作。

Maybe, data you sent was not flushed. 也许您发送的数据没有刷新。 Use outs.flush(); 使用outs.flush(); after outs.println(line); outs.println(line); or change it's constructor call to PrintStream(echoClient.getOutputStream(),true); 或将其构造函数调用更改为PrintStream(echoClient.getOutputStream(),true); (enable auto-flush on printing new line) (在打印新行时启用自动刷新)

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

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