Java Socket Chat不显示所有消息

时间:2013-11-20 18:31:18

标签: java sockets chat sync

我正在尝试通过仅与一个客户端和服务器进行套接字聊天。我已成功运行,因为在服务器中能够来回传递消息,但是当我尝试使服务器端实现更复杂,添加命令等客户端可以使用时,通信失败。它似乎可能会失去同步,即使在一次又一次地使用相同的命令会产生不同的结果,即使我在每个命令后刷新所有内容。

简化输出的示例,每次都按预期工作:

客户端:

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);
    }
}
}

服务器:

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();
         }
    }
}

我得到这样的输出,它每次只是吐出来的时候起作用:

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

但是当我通过用以下内容替换while(true)块来使服务器端更复杂时,我得到了更加混乱的结果:

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

过程:

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

您还会注意到流程命令代码中的一些注释行。当我回复一条简单的消息时,服务器似乎跟不上,但是当我使用登录功能时,它会产生如下可怕的输出:

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: 

再次注意从服务器返回任何内容的空白,然后即使命令的更改也不会提示不同的响应。就好像它只是通过使用一个额外的功能而失去同步?

2 个答案:

答案 0 :(得分:2)

在某些字符串的末尾有一些"\n"。 如果你们都放"\n"并使用println,你将有双回车,这将搞乱你的协议。删除"\n",它应该会更好。

答案 1 :(得分:1)

也许,您发送的数据没有刷新。在outs.flush();后使用outs.println(line);或将其构造函数调用更改为PrintStream(echoClient.getOutputStream(),true);(在打印新行时启用自动刷新)