套接字和ServerSockets服务器/客户端GUI聊天程序

时间:2013-11-30 04:44:08

标签: java multithreading swing sockets serversocket

我正在尝试学习套接字和ServerSockets如何在Java中工作,所以我尝试使用聊天服务器,但同时使用线程来处理每个客户端。我想我的代码需要一些新的眼睛,因为我不知道为什么它不起作用。程序启动但客户端未连接且未创建。我很确定我的客户端类有什么问题,但我不确定需要修复什么。任何帮助,即使只是一个有用的资源的链接,将非常感激。谢谢。

服务器代码

     package chatbox.server;

        import static java.lang.System.out;

        import java.io.IOException;
        import java.io.InputStream;
        import java.io.InputStreamReader;
        import java.io.OutputStream;
        import java.io.PrintStream;
        import java.net.ServerSocket;
        import java.net.Socket;
        import java.util.ArrayList;
        import java.util.Scanner;


     public class Server {


    public final static int DEFAULT_PORT = 5000;

    private ServerSocket socket;
    private ArrayList<Socket> clients;


    public Server(int port) throws IOException {
        System.out.println("Server is now online");
        System.out.println("port: " + port);
        this.socket = new ServerSocket(port);
        System.out.println("Listening socket established");
        System.out.println("Waiting for connections...");
        this.clients = new ArrayList<Socket>();


        while (true) {
            try {
                final Socket connection = this.socket.accept();
                this.clients.add(connection);


                Runnable incomingMsg = new Runnable() {
                    private InputStream inputStream = connection.getInputStream();
                    private InputStreamReader reader = new InputStreamReader(
                            inputStream);
                    private Scanner scanner = new Scanner(reader);

                    @Override
                    public void run() {
                        while (true) {
                            if (scanner.hasNextLine()) {
                                String msg = scanner.nextLine();
                                System.out.println("Handling message: \"" + msg
                                        + "\"");
                                notifyAllConnections(msg);
                            }
                        }
                    }

                };

                Thread thread = new Thread(incomingMsg);


                thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {

                    @Override
                    public void uncaughtException(Thread thread, Throwable exc) {
                        try {
                            connection.close();
                        } catch (IOException e1) {

                            e1.printStackTrace();
                        } finally {
                            clients.remove(connection);
                            System.out.println("Removed connection");
                        }

                    }
                });
                thread.start();
                System.out.println("Added new connection");
            } catch (IOException exc) {

                System.out
                        .println("Error occurred.");
            }
        }
    }

    protected void notifyAllConnections(String msg) {
        for (Socket sock : this.clents) {
            try {
                OutputStream out = sock.getOutputStream();
                PrintStream printer = new PrintStream(out);
                printer.println(msg);
                printer.flush();
            } catch (IOException exc) {
                System.out.println("Message was not fully broadcast");
            }

        }
    }

    public static void main(String[] args) {
        try {
            Server server = new Server(
                    Server.DEFAULT_PORT);
        } catch (IOException exc) {
            System.out
                    .println("Could not create the server socket.");
            exc.printStackTrace();
            String servername = "localhost";
            try {
                new Client(servername, 5000);
            } catch (Exception ex) {
                out.println("Error" + ex.getMessage());

            }
        }
    }
    }

客户代码

     package chatbox.client

        import java.awt.event.ActionListener;

        import javax.swing.JFrame;
        import java.io.*;
        import java.util.*;
        import java.net.*;
        import javax.swing.*;
        import java.awt.*;
        import java.awt.event.*;
        import static java.lang.System.out;

        public class Client extends JFrame {

    private PrintWriter pw;
    private Scanner scanner;
    private JPanel chatAndSend;
    private JTextArea chatWindow;
    private JScrollPane mainScroll;
    private JTextArea chatText;
    private JScrollPane miniScroll;
    private JButton send;
    private Socket client;

    public Client(String servername, int port) throws Exception {

        this.client = new Socket(servername, port);
        this.scanner = new Scanner(new InputStreamReader(
                this.client.getInputStream()));
        this.pw = new PrintWriter(this.client.getOutputStream());

        makeGUI();
        new MessagesThread().start();

    }

    public void makeGUI() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(new BorderLayout());
        this.chatWindow = new JTextArea(10, 20);
        this.chatWindow.setEditable(false);
        this.chatWindow.setLineWrap(true);
        this.mainScroll = new JScrollPane(chatWindow,
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        this.add(this.mainScroll, BorderLayout.NORTH);
        this.chatAndSend = new JPanel();
        this.chatAndSend.setLayout(new FlowLayout());
        this.chatText = new JTextArea(1, 1);
        this.chatText.setLineWrap(true);
        this.miniScroll = new JScrollPane(chatText,
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        this.chatAndSend.add(this.miniScroll);
        this.send = new JButton();
        this.send.setText("SEND");
        this.send.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                pw.println(chatText.getText());
                pw.flush();
            }
        });
        this.chatAndSend.add(this.send);
        this.add(this.chatAndSend, BorderLayout.SOUTH);
        this.setVisible(true);
        this.pack();
    }

    class MessagesThread extends Thread {
        public void run() {
            String line;
            try {
                while (true) {
                    line = scanner.nextLine();
                    chatWindow.append(line + "\n");
                }
            } catch (Exception exception) {
                System.out.println(exception.getMessage());
            }
        }
    }
    }

2 个答案:

答案 0 :(得分:4)

  • 在Client MessagesThread类中,在while循环中,它应该是while (scanner.hasNextLine()) {
  • 在同一个类中,您将从后台线程附加到JTextArea,这是您不应该做的事情。
  • 在Client MessagesThread类中,实现Runnable而不是扩展Thread(一般建议)
  • 更好 - 使用SwingWorker,因此您可以使用发布/进程更新Swing事件线程上的JTextArea。
  • 为了我的钱,在尝试将程序放入GUI之前,我先将程序启动并运行在非GUI版本中。
  • 您的拼写错误表明代码甚至不应该编译,与clientss和clents一起使用,...... ???这是真正的代码吗?如果我们无法复制/粘贴您的代码并对其进行测试,那么我们将很难帮助您。

修改
我查看并运行了您的新代码。你是否看到你试图在catch块中创建一个永远不会被调用的新客户端?

最重要的是 - 在运行代码时使用调试器来查看代码正在执行或未执行的操作。添加println语句(更多)。如果你这样做了,你就会发现永远不会调用Client的构造函数,并且会知道你试着调用它的代码来查看原因。

答案 1 :(得分:0)

不确定这是否是您的问题之一,但我不知道在Server类中进行foreach循环是否安全,因为您可以从一个线程迭代并在同一时间从另一个线程添加列表,这可能是foreach循环的问题。如果我错了,请纠正我。 (或者只是从foreach中删除可能会对其造成伤害?)