dadi723 2016-12-08 09:33 采纳率: 0%
浏览 1550
已采纳

自己写的Java的简易聊天程序,测试中,怎么搞成能连两个客户端的(最好用多线程)?

希望只在服务端改动,相关代码如下:
服务端:

 package cheat;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.Button;
import java.awt.event.*;
public class CheatServer {
    public static void main(String[] args) {
        new MyFrame("服务端");
    }
}
class MyFrame extends Frame{
    static boolean judge=true;
    DataInputStream dis=null;
    MyFrame(String s){
        super(s);
        this.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                try {
                    dis.close();
                } catch (IOException err) {
                    err.printStackTrace();
                }
                setVisible(false);
                System.exit(0);
            }
        });
        setLayout(new BorderLayout());
        setBounds(200,200,250,300);
        TextArea tr=new TextArea();
        tr.setEditable(false);
        Button b=new Button("终止");
        Panel p=new Panel();
        p.setLayout(new FlowLayout(FlowLayout.CENTER));
        b.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ee){
                judge=false;
                tr.setText("服务终止\n");
                try {
                    dis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        p.add(b);
        add("North",tr);
        add("South",p);
        setVisible(true);
        try {
            ServerSocket ss=new ServerSocket(6666);
            while(judge){
                Socket s1=ss.accept();
                tr.append("用户接入成功!\n");
                dis=new DataInputStream(s1.getInputStream());
                while(judge){
                String str=dis.readUTF();
                System.out.println(str);
                }
            }
        } catch (IOException er) {
            tr.setText("服务出错!"+" ");
            System.out.println(er.getMessage());
        }
    }
}

客户端(测试参考,不希望改动):

 package cheat;
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Button;
public class CheatClent{
    public static void main(String[] args){
        new MyFreame("客户端");
    }
}
class MyFreame extends Frame{
    Socket client=null;
    DataOutputStream dos=null;
    MyFreame(String s){
        super(s);
        setBounds(200,200,400,500);
        setLayout(new BorderLayout());
        TextArea tr=new TextArea();
        tr.setEditable(false);
        TextField tf=new TextField();
        tf.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {
                String str=tf.getText().trim();
                tr.append(str+"\n");
                tf.setText("");
                try {
                    dos=new DataOutputStream(client.getOutputStream());
                    dos.writeUTF(str);
                    dos.flush();
                } catch (IOException e) {
                    tr.setText("输出异常!\n");
                }

            }
        });
        add("North",tr);
        add("South",tf);
        this.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                try {
                    dos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                setVisible(false);
                System.exit(0);
            }
        });
        setVisible(true);
        try {
            client=new Socket("127.0.0.1",6666);
            tr.setText("连接成功!\n");
        } catch (IOException er) {
            tr.setText("连接出错!\n");
        }
    }
}
  • 写回答

3条回答 默认 最新

  • taxueq 2016-12-08 12:57
    关注
    import java.io.*;
    import java.net.*;
    import java.awt.*;
    import java.awt.event.*;
    public class CheatServer {
        public static void main(String[] args) {
            new MyFrame("服务端");
        }
    }
    class MyFrame extends Frame implements Runnable{
        static boolean judge=true;
        ServerSocket ss=null;
        TextArea tr=null;
        Socket s1;
    
        MyFrame(String s){
            super(s);
            this.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e){
                    try {
                        ss.close();
    
                    } catch (IOException err) {
                        err.printStackTrace();
                    }
    
                    setVisible(false);
                    System.exit(0);
                }
            });
            setLayout(new BorderLayout());
            setBounds(200,200,250,300);
            tr=new TextArea();
            tr.setEditable(false);
            Button b=new Button("终止");
            Panel p=new Panel();
            p.setLayout(new FlowLayout(FlowLayout.CENTER));
            b.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent ee){
                    judge=false;
                    tr.setText("服务终止\n");
    
                    try {
                       // dis.close();
                        ss.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
            p.add(b);
            add("North",tr);
            add("South",p);
            setVisible(true);
            try {
                ss=new ServerSocket(6666);
                while(judge){
                     s1 = ss.accept();
                     tr.append("用户接入成功!\n");
                     Thread thread=new Thread(this);
                     thread.start();
                }
    
    
            } catch (IOException er) {
                tr.setText("服务出错!"+" ");
                System.out.println(er.getMessage());
            }
        }
        @Override
        public void run() {
    
            try {
                DataInputStream dis1=new DataInputStream(s1.getInputStream());
                 while(judge){
                 String str=dis1.readUTF();
                 System.out.println(str);
                 }
            } catch (IOException e) {
            }
    
    
    
        }
    } 
    

    这样可以满足你的需求

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建