简体   繁体   English

客户端的Java网络输入流

[英]Java networking input stream for client

I'm very new to networking in java and wanted to create a network chat program, I have found a few tutorials online and kind of drifted from that. 我对使用Java进行联网非常陌生,并且想创建一个网络聊天程序,我在网上找到了一些教程,并且有些偏离了。 I have the server of my program working and the only thing that is interfering is when I try to read the chat messages that the server sends over. 我的程序服务器正在运行,唯一的干扰是当我尝试读取服务器发送过来的聊天消息时。 The server does send the bytes of data over since the print message does work. 由于打印消息确实起作用,所以服务器确实发送了数据字节。 So the problem is that the while loop never ends, what can this be a problem of? 所以问题是while循环永远不会结束,这可能是什么问题?

public String[] updateChatDialog() throws IOException{
    String returnString = "";
    int accessed = -1;

    while((accessed = in.read()) > 0){
        System.out.println((char)accessed);
        returnString = returnString + (char)accessed;
    }
    System.out.println(returnString);
    return stringToTable(returnString);
}

Any tips on java networking would be helpful! 关于Java网络的任何提示都将有所帮助!

I do reset the BufferedInputStream every time the chats are rendered into a string, AKA the method above with the return in it. 每次将聊天呈现为字符串时,我都会重置BufferedInputStream,也就是上面带有返回值的方法。

The 'problem' is that your loop reads until end of stream, and the peer isn't closing the connection so EOS never arrives. “问题”是您的循环读取直到流结束,并且对等方没有关闭连接,因此EOS永不到达。

If you want to read messages you have to define them yourself. 如果您想阅读消息 ,则必须自己定义它们。 The easiest thing for you to do in this application is write and read lines, with eg PrintWriter.println() and BufferedReader.readLine(). 在此应用程序中,最简单的操作是写和读行,例如使用PrintWriter.println()BufferedReader.readLine().

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

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