多线程GUI Java套接字聊天应用程序

时间:2017-03-09 22:41:47

标签: java multithreading sockets user-interface chat

我为聊天室类型的应用程序编写了代码,用户可以与服务器通信,服务器可以与用户通信。但我希望服务器能够与多个用户进行通信。任何人都可以帮助我,因为我在过去的几个小时里尝试过这么多而无处可去。

这是我的客户端代码

    import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PrinterException;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.sql.Time;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class ClientChat extends JFrame implements ActionListener {
    static Socket socket;
    JPanel panel;
    JTextField textField;
    JTextArea textArea;
    JButton button;

    public ClientChat() throws UnknownHostException, IOException {
        panel = new JPanel();
        textField = new JTextField();
        textArea = new JTextArea();
        button = new JButton("Send");
        this.setSize(500, 500);
        this.setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        panel.setLayout(null);
        this.add(panel);
        textArea.setBounds(20, 20, 450, 360);
        panel.add(textArea);
        textField.setBounds(20, 400, 340, 30);
        panel.add(textField);
        button.setBounds(375, 400, 95, 30);
        panel.add(button);
        button.addActionListener(this);
        socket = new Socket(InetAddress.getLocalHost(), 2000);

        textArea.setText("Connected to Server");
        this.setTitle("Client");
        while (true) {
            try {
                DataInputStream dis = new DataInputStream(socket.getInputStream());
                String string = dis.readUTF();
                textArea.setText(textArea.getText() + '\n' + "Server:"
                        + string);
            } catch (Exception e1) {
                textArea.setText(textArea.getText() + '\n'
                        + "Message sending fail:Network Error");
                try {
                    Thread.sleep(3000);
                    System.exit(0);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if ((e.getSource() == button) && (textField.getText() != "")) {

            textArea.setText(textArea.getText() + '\n' + "User:"
                    + textField.getText());
            try {
                DataOutputStream dos = new DataOutputStream(
                        socket.getOutputStream());
                dos.writeUTF(textField.getText());
            } catch (Exception e1) {
                textArea.setText(textArea.getText() + '\n'
                        + "Message sending fail:Network Error");
                try {
                    Thread.sleep(3000);
                    System.exit(0);
                } catch (InterruptedException e2) {
                    // TODO Auto-generated catch block
                    e2.printStackTrace();
                }
            }
            textField.setText("");
        }
    }

    public static void main(String[] args) throws UnknownHostException,
            IOException {
        ClientChat chatForm = new ClientChat();
    }
}

这是我的服务器代码

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class ServerChat extends JFrame implements ActionListener {
    static ServerSocket server;
    static Socket socket;
    JPanel panel;
    JTextField textField;
    JTextArea textArea;
    JButton button;
    DataInputStream dis;
    DataOutputStream dos;

    public ServerChat() throws UnknownHostException, IOException {

        panel = new JPanel();
        textField = new JTextField();
        textArea = new JTextArea();
        button = new JButton("Send");
        this.setSize(500, 500);
        this.setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        panel.setLayout(null);
        this.add(panel);
        textArea.setBounds(20, 20, 450, 360);
        panel.add(textArea);
        textField.setBounds(20, 400, 340, 30);
        panel.add(textField);
        button.setBounds(375, 400, 95, 30);
        panel.add(button);
        this.setTitle("Server");
        button.addActionListener(this);
        server = new ServerSocket(2000, 1, InetAddress.getLocalHost());
        textArea.setText("Waiting for Client");
        socket = server.accept();
        textArea.setText(textArea.getText() + '\n' + " Found");
        while (true) {
            try {
                DataInputStream dis = new DataInputStream(socket.getInputStream());
                String string = dis.readUTF();
                textArea.setText(textArea.getText() + '\n' + "Client:"
                        + string);
            } catch (Exception e1) {
                textArea.setText(textArea.getText() + '\n'
                        + "Message sending fail:Network Error");
                try {
                    Thread.sleep(3000);
                    System.exit(0);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if ((e.getSource() == button) && (textField.getText() != "")) {
            textArea.setText(textArea.getText() + '\n' + "Server:"
                    + textField.getText());
            try {
                DataOutputStream dos = new DataOutputStream(
                        socket.getOutputStream());
                dos.writeUTF(textField.getText());
            } catch (Exception e1) {
                try {
                    Thread.sleep(3000);
                    System.exit(0);
                } catch (InterruptedException e2) {
                    // TODO Auto-generated catch block
                    e2.printStackTrace();
                }
            }
            textField.setText("");
        }
    }

    public static void main(String[] args) throws UnknownHostException,
    IOException {
        new ServerChat();
    }
}

非常感谢任何帮助。谢谢

0 个答案:

没有答案