简体   繁体   English

创建一个简单的聊天客户端/服务器

[英]Creating a simple Chat Client/Server

i was trying creating Client/server Chat in console "not gui", in gui case i can add action listener to the JTextField and every time that user hit enter the data will be send it to the client and vise versa, in console java application how can i do that??我正在尝试在控制台“not gui”中创建客户端/服务器聊天,在 gui 情况下,我可以将动作侦听器添加到 JTextField,并且每次用户点击输入数据时都会将其发送到客户端,反之亦然,在控制台 java 应用程序中我怎样才能做到这一点?? i mean how could i listen to the console (or) should i listen to the BufferReader so if it full then read the Buffer???我的意思是我怎么能听控制台(或)我应该听 BufferReader 所以如果它满了然后读取缓冲区???

You can do smth like:你可以这样做:

String StrRead() {
    byte[] enter = new byte[256];
    int num = 0;
    String str = new String();
    try {    
        num = System.in.read(enter);
    }
    catch (Exception e)
    {
        System.out.println(e.toString());
    }
    str = new String(enter,0,num);
    return str.trim();
}

and then just call String s = StrRead();然后只需调用 String s = StrRead();

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

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