自己写的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个回答

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) {
        }



    }
} 

这样可以满足你的需求

你这都使用了网络编程了,不会用多线程?我以前写过一个类似的,不过现在在工作的地方,有兴趣的话可以留个邮箱,我回去发给你一份!

dadi723
dadi723 多线程懂一点,就是这代码太长了,脑子转不过来,邮箱 haluan.chow@foxmail.com,非常感谢!!
大约 3 年之前 回复

我也在做简易的聊天项目,希望一块参考一下,这是我的邮箱,非常感谢! wangdy@radacat.com

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问