Java多线程,多客户端服务器 - 客户端不执行run方法

时间:2014-03-20 14:49:44

标签: java multithreading swing sockets

尝试在Java上做得更好,我试图创建一个多线程客户端&服务器信使,客户端向服务器发送消息,然后服务器重定向&将消息发送给第一个客户端想要发送给它的用户。我也在研究很多关于并发和多线程等方面的问题。我目前的问题,我已经得出结论,我的客户端的BufferedReader / InputStream是不是准备好了#34;当我启动我的运行线程时,它因此返回nullPointerException。其他一些问题:

  • 我应该在SwingUtilities.invokeLater()语句中执行drawGUI()方法,还是无关紧要,因为我并没有真正与该方法中的GUI交互
  • 想知道HashMap usersIP是否适用于键值对,将客户端与其IP配对。 “服务器类”中还有其中一个
  • 会问另一个问题,但忘了它是什么。如果我记得
  • ,将在以后编辑

客户端代码(缩减为有用的东西)

public class Client extends Thread {
public static Socket socket;
public static PrintStream os;
public static BufferedReader is;
public static StringBuffer toAppend = new StringBuffer("");
public static StringBuffer toSend = new StringBuffer("");

//Some variables for use with the JMenuBarItems


public static Map<String, String> usersIP = new HashMap<String, String>();
public static String[] friends;

//Swing variables

public static void drawGUI() {

    //JMenuBar set up

    //Some JMenuItem stuff and adding their action listeners

    //Adding the JMenuBarItems to the JMenuBar

    //Main GUI Window set up

    //Initializing the JTextArea and setting some properties
    chatField = new JTextField();
    chatField.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            String s = chatField.getText();

            if(!s.equals("")) {
                appendToChatBox(s);
                //textArea.append(username + "> " + s);
                chatField.setText("");
                sendString(s);
                System.out.println("In Chat Field: To Send: " + toSend.toString());
            }
        }
    });
    //Adding some panels to the JFrame
            //Setting some stuff for the JFrame
}

    //Some of these methods below may be unneeded, but I left them there just in case
public static void appendToChatBox(String s) {
    synchronized(toAppend) {
        toAppend.append(s);
    }
}

public static void appendToChatBox(String username, String s) {
    synchronized(toAppend) {
        toAppend.append(username + "> " + s);

    }
}

public static void sendString(String s) {
    synchronized(toAppend) {
        toAppend.append(s);
    }
}

public static void cleanUp() {
    try {
        if(socket != null) {
            socket.close();
            socket = null;

        }
    } catch(IOException e) {
        socket = null;

    }

    try {
        if(is != null) {
            is.close();
            is = null;

        } 
    } catch(IOException e) {
        is = null;

    }

    if(os != null) {
        os.close();
        os = null;
    }
}

public static void connect() {
    try {
        System.out.println("In connect method"); //Debug

        socket = new Socket(host, port);
        os = new PrintStream(socket.getOutputStream());
        is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        appendToChatBox("Successfully Connected");
        System.out.println("ToAppend in connect method: " + toAppend.toString()); //Debug
        System.out.println("Successfully Connected");

    } catch(UnknownHostException e) {
        System.err.println("Don't know about host: " + host);

    } catch(IOException e) {
        System.err.println("Couldn't get I/O for the connection to the host " + host);
    }

    if(socket != null && os != null && is != null) {
        try {

            while(!closed) {
                if(toSend.length() != 0) {
                    System.out.println("In connect()> ToSend.length isn't = to 0"); //Debug
                    os.println(toSend);
                    os.flush();
                    toSend.setLength(0);
                }
            }
        } catch(Exception e) {
            System.err.println("Error in sending a message to the Server " + e);
            cleanUp();
        }
    }
}

public static void reconnect() {
    cleanUp();
    connect();

}

public static void main(String args[]) {
    SwingUtilities.invokeLater(new Thread(new Runnable() {
        @Override
        public void run() {
            drawGUI();

        }
    }));

    new Thread(new Runnable() {
        @Override
        public void run() {
            System.out.println("Attempting to connect to server..."); //Debug
            connect();
        }
    }).start();

    new Client().start();
}

@Override
public void run() {
    String servermessage;

    System.out.println("Client.run() invoked"); //Debug


    try {
        System.out.println("In try-catch statement in Client.run()"); //Debug

        while(is.ready()) {
            System.out.println("Input Stream isn't null & is ready in Client.run()"); //Debug

            servermessage = is.readLine().trim();

            if(servermessage != null && servermessage.length() != 0) {
                if(servermessage.equalsIgnoreCase("client_disconnect")) {
                    break;

                } else if(servermessage.equals(Integer.toString(USERNAME_REQUEST))) {

                } else {
                    appendToChatBox("Server" + servermessage);
                    System.out.println("Server> " + servermessage);

                }
            }

            if(toSend.length() > 0) { //Debug
                System.out.println("ToSend in Client.run's while loop: " + toSend.toString());

            } else { //Debug
                System.out.println("ToSend is empty");

            }
        }
    } catch(Exception e) {
        e.printStackTrace();

    }

    if(toAppend.length() != 0) {
        appendToChatBox("From Client.run() " + toAppend.toString());

    } else { //Debug
        System.out.println("ToAppend is empty");

    }
}

0 个答案:

没有答案
相关问题