socket 实现多线程聊天

public class MyServer extends JFrame {

private JPanel contentPane;
private Socket socket;
private boolean isRunning=true;
private JTextArea textArea;
public boolean isRunning() {
    return isRunning;
}

public void setRunning(boolean isRunning) {
    this.isRunning = isRunning;
}

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MyServer frame = new MyServer();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public MyServer() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 861, 742);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JButton button = new JButton("\u542F\u52A8\u670D\u52A1");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                ServerSocket server = new ServerSocket(8080);
                System.out.println("����������");
                socket = server.accept();
                Thread t=new Thread(new MyServerRead(MyServer.this));
                t.start();
                System.out.println("�пͻ�������");
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    });
    button.setBounds(22, 10, 475, 87);
    contentPane.add(button);

    textArea = new JTextArea();
    textArea.setBounds(100, 476, 518, 138);
    contentPane.add(textArea);

    JButton button_1 = new JButton("\u53D1\u7ED9\u5BA2\u6237\u7AEF");
    button_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            write();
        }
    });
    button_1.setBounds(671, 531, 93, 23);
    contentPane.add(button_1);
}

public void read() {
    try {
        InputStream is = socket.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();
        System.out.println("���������յ���Ϣ"+line);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
public void write(){
    try {
        OutputStream os=socket.getOutputStream();
        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(os));
        String words=   textArea.getText();
        System.out.println("������������Ϣ");
        bw.write(words+"\n");
        textArea.setText("");
        bw.flush();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

public class MyClient extends JFrame {

private JPanel contentPane;
private Socket socket; 
private JTextArea textArea;
private JList list;
private DefaultListModel<String> modle;
/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MyClient frame = new MyClient();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public MyClient() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 819, 598);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    textArea = new JTextArea();
    textArea.setBounds(49, 436, 500, 101);
    contentPane.add(textArea);

    JButton btnNewButton = new JButton("����");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            write();
        }
    });
    btnNewButton.setBounds(589, 436, 129, 101);
    contentPane.add(btnNewButton);

    list = new JList();
    list.setBounds(61, 10, 484, 416);
    modle=new DefaultListModel<>();
    list.setModel(modle);
    contentPane.add(list);

    JButton btnNewButton_1 = new JButton("���ӷ�����");
    btnNewButton_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                System.out.println("���ӷ�����");
                socket=new Socket("192.168.0.30", 8080);
                System.out.println("���ӷ������ɹ�");
                Thread t=new Thread(new MyClientRead(MyClient.this));
                t.start();
            } catch (UnknownHostException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    });
    btnNewButton_1.setBounds(615, 68, 103, 86);
    contentPane.add(btnNewButton_1);
}
public void read() {
    try {
        InputStream is = socket.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();
        modle.addElement("服务器说的"+line);
        System.out.println(line);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
public void write(){
    try {
        OutputStream os=socket.getOutputStream();
        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(os));
        String words=   textArea.getText();
        bw.write(words+"\n");
        modle.addElement(words);
        textArea.setText("");
        bw.flush();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

public class MyClientRead implements Runnable{
private MyClient client;
public MyClientRead(MyClient client){
this.client=client;
}
@Override
public void run() {
while(true){
client.read();
}
}

}
public class MyServerRead implements Runnable {
private MyServer server;
public MyServerRead(MyServer server) {
this.server=server;
}
@Override
public void run() {
while(server.isRunning()){
server.read();
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值