自动从服务器/客户端读取jframe上的聊天程序?

xesrikrc  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(263)

我正在使用jframe创建一个简单的服务器/客户端聊天程序,其中包含一个gui。我在从服务器/客户机读取时遇到问题,因此我可以将字符串附加到jframe窗口。将readmessage()方法放在main中会导致空指针错误,将readmessage()方法放在constructor/actionperformed()中会导致jframes暂停。有人有办法吗?这是我的服务器程序:

public class ChatBoxServer implements ActionListener {

    private ServerSocket ss;
    private Socket s;
    private ObjectInputStream oin;
    private ObjectOutputStream oout;
    private ChatFrameServer chat = new ChatFrameServer();
    private JButton connect = chat.getConnectButton();;
    private JButton send = chat.getSendButton();
    private String user;
    private int port;

    public ChatBoxServer() throws IOException{
        send.addActionListener(this);
        connect.addActionListener(this);

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if (e.getActionCommand().equals("Send")) {

        String outgoingMsg = "";

        try {
            outgoingMsg= "<" + user +">:\t" + chat.getSendField().getText() +"\n"; 
            chat.getChatArea().append(outgoingMsg);

            oout.writeObject(outgoingMsg);  
            oout.flush();  
        } catch (IOException e1) {
            e1.printStackTrace();
        }  

        if (chat.getSendField().getText().equals("quit")) {
            try {
                s.close();
                oin.close();   
            } catch (Exception error) {
                System.out.println("Server side");
            }
        }
    } 
        if (e.getActionCommand().equals("Connect")) {
            try {
            port = Integer.parseInt(chat.getPortNum().getText());
            } catch (Exception error) {
                chat.getChatArea().append("That port is not valid");
            }

            user = chat.getUser().getText();
            try {
                createServer(port);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

    private void createServer(int port) throws IOException {
        ss=new ServerSocket(port); 
        s=ss.accept(); 
        oin=new ObjectInputStream(s.getInputStream());  
        oout=new ObjectOutputStream(s.getOutputStream());  

        if (!s.isConnected())
        JOptionPane.showMessageDialog(null, "Cannot Connect", "Connection Status", JOptionPane.WARNING_MESSAGE);

        chat.getChatArea().append("You are now connected!\tType quit to end chat.\n");

    }

    private void readMessage() {

        String incomingMsg = "";

        if (!incomingMsg.equalsIgnoreCase("quit")) {
            try {
                incomingMsg = oin.readUTF();
            } catch (IOException e) {
                JOptionPane.showMessageDialog(null, "Server Error", "Connection Status", JOptionPane.WARNING_MESSAGE);
            }
            chat.getChatArea().append(incomingMsg + "\n");
        }

    }

    public static void main(String[] args) throws IOException {
        ChatBoxServer cbs = new ChatBoxServer();

        cbs.readMessage();
    }

}

这是我的客户程序:

public class ChatBoxClient implements ActionListener {

    Socket s;
    ObjectInputStream oin;
    ObjectOutputStream oout;
    private ChatFrameClient chat = new ChatFrameClient(); 
    JButton send;
    JButton connect;
    private String user;
    private String hostName;
    private int port;

    public ChatBoxClient() throws IOException {
        send  = chat.getSendButton();
        send.addActionListener(this);
        connect = chat.getConnect();
        connect.addActionListener(this);

    }

    public void connection() throws IOException {

        s=new Socket(hostName, port);  

        oout=new ObjectOutputStream(s.getOutputStream());  
        oin = new ObjectInputStream(s.getInputStream());

        if (s.isConnected())
            chat.getChatArea().append("You are now connected!\tType quit to end chat.\n");

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if (e.getActionCommand().equals("Send")) {

            String outgoingMsg=""; 

            try {
                outgoingMsg = "<" + user + ">:\t" + chat.getSendField().getText() + "\n";  
                chat.getChatArea().append(outgoingMsg);
                oout.writeObject(outgoingMsg);
                oout.flush(); 

            } catch (IOException  e1) {
                e1.printStackTrace();
            }  

            if (chat.getSendField().getText().equalsIgnoreCase("quit")) {

                try {
                    oin.close();
                    oout.close();
                    s.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }

            }
        }
        if (e.getActionCommand().equals("Connect")) {
            user = chat.getUserName().getText();
            port = Integer.parseInt(chat.getPortNum().getText());
            hostName = chat.getHostName().getText();

            try {
                connection();
            } catch (IOException e1) {
                e1.printStackTrace();
            }

        }
    }

    private void readMessage() {

        String incomingMsg = "";

        if (!incomingMsg.equalsIgnoreCase("quit")) {
            try {
                incomingMsg = oin.readUTF();
            } catch ( IOException e) {
                JOptionPane.showMessageDialog(null, "Server Error", "Connection Status", JOptionPane.WARNING_MESSAGE);
            }
            chat.getChatArea().append("<" + user + ">:\t" + incomingMsg + "\n");

        }

    }

    public static void main(String[] args) throws IOException {
        ChatBoxClient cbc = new ChatBoxClient();
        cbc.readMessage();
    }

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题