m0_56722864
m0_56722864
2021-06-02 20:29
采纳率: 100%
浏览 33

求教大佬们看一下为什么只能发送一次消息?

服务端

public class xxsocket {//服务端

    static int count=0;
    public static void main(String[] args){

        JFrame jf=new JFrame("xx聊天室");
        jf.setBounds(200,200,500,500);
        JPanel jl=new JPanel();
        final JTextArea jt=new JTextArea();
        jt.setRows(25);
        jt.setColumns(30);
        jt.setBackground(Color.GRAY);
        jt.setForeground(Color.CYAN);
        jt.setEditable(false);
        final JTextField jd=new JTextField();
        jd.setColumns(25);
        jd.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode()==e.VK_ENTER){
                    String aa=jd.getText();
                    jt.append(aa+"\n");
                    jd.setText("");
                    count++;
                    if (count==25){
                        jt.setText("消息过多,已清屏,请在后台聊天记录查询"+"\n");
                        count=0;
                    }
                }
            }
        });


        jf.setVisible(true);
        jl.setBackground(Color.DARK_GRAY);
        jl.add(jt,BorderLayout.NORTH);
        jl.add(jd,BorderLayout.SOUTH);
        jf.add(jl);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        try {
            ServerSocket s = new ServerSocket(913);
            Socket ss = s.accept();
            InputStream in = ss.getInputStream();
            byte[] buffer = new byte[1024];
            int len = in.read(buffer);
            final String a = new String(buffer, 0, len);
            jt.setText("");
            jt.append(a+"\n");

        }catch(IOException e){
            jt.append("发送失败"+"\n"+e);
        }

    }

}

客户端

public class socket{

public static void main(String[] args){

try {
        Socket client = new Socket("localhost", 913);
        OutputStream out = client.getOutputStream();
        BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
        DataOutputStream dos = new DataOutputStream(client.getOutputStream());
        DataInputStream dis = new DataInputStream(client.getInputStream());
        boolean isRunning = true;
        while(isRunning) {
            String msg = console.readLine();
            dos.writeUTF(msg);
            dos.flush();
            // 3、获取消息
            msg = dis.readUTF();
            System.out.println(msg);


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

 

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • software7503
    已采纳

    这段代码要放到一个线程里面

    while(true){ 
        Socket ss = s.accept();
                InputStream in = ss.getInputStream();
                byte[] buffer = new byte[1024];
                int len = in.read(buffer);
                final String a = new String(buffer, 0, len);
                jt.setText("");
                jt.append(a+"\n");
    
    }
    点赞 1 评论
  • weixin_43681859
    答题大神 2021-06-02 20:32

    是你接受消息的时候,只接收了一次,应该在循环里面去监听端口,不停的去接收消息。

    点赞 评论
  • QA_Assistant
    有问必答小助手 2021-06-03 15:02

    您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

    如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

    ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

    点赞 评论

相关推荐