尝鲜我在行 2021-06-02 20:29 采纳率: 50%
浏览 43
已采纳

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

服务端

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条回答 默认 最新

  • 关注

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

    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");
    
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 hfss 组天线阵列报错:The A and B vectors must be parallel to the XY plane,如何解决?
  • ¥30 ansible连接设备报错
  • ¥60 pmsampsize包取值问题
  • ¥15 波形合成电路的设计学习
  • ¥15 unity脚本挂不上
  • ¥15 数学建模数学建模需要
  • ¥20 Java Sound Api 调用Mixer.getLine偶现Line unsupported错误。
  • ¥15 使用cuda加速opencv运算但是报错AttributeError: module 'cv2.cuda' has no attribute 'getCudaEnabledDeviceCount'
  • ¥15 java输入输出异常
  • ¥15 三子连珠对弈小游戏制作