我可以在服务器程序中调用客户机程序吗?

hvvq6cgz  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(212)

我正在创建一个程序,我的目标是在一个名为clientchat的单独类中单击jbutton,创建一个服务器(在我的chatboxserver类中),在该类中,我调用我的客户机程序(chatboxclient类)来建立连接。但是,当我尝试从chatboxserver.java调用chatboxclient.java时,我的程序会暂停。是可以在服务器类中调用我的客户机类,还是需要分别运行这两个程序?
聊天室服务器:

public class ChatBoxServer implements ActionListener {

    private ServerSocket ss;
    private Socket s;
    private ObjectInputStream oin;
    private ObjectOutputStream oout;
    private ChatBox chat;
    private JButton hello;
    private String user;
    private String user2;
    private String host;

    public ChatBoxServer(String user, String user2, String host, int port) throws IOException{
        setUsers(user, user2);
        setHost(host);

        chat = new ChatBox();
        hello = chat.getSendButton();
        hello.addActionListener(this);
        createServer(port);

        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");

        readMessage();

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        String outgoingMsg = "";

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

            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");
            }
        }
    }

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

    }

    private void readMessage() {
        String incomingMsg = "";

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

    }

    private void setUsers(String user, String user2) {
        this.user = user;
        this.user2 = user2;
    }

    private void setHost(String host) {
        this.host = host;
    }

}

聊天室客户端:

public class ChatBoxClient implements ActionListener {

    Socket s;
    ObjectInputStream oin;
    ObjectOutputStream oout;
    private ChatBox chat;
    JButton hello;
    private String user;
    private String user2;

    public ChatBoxClient(String user, String user2, String hostname, int port) throws IOException {

        setUsers(user, user2);
        connection(hostname, port);

        chat = new ChatBox();
        hello = chat.getSendButton();
        hello.addActionListener(this);

        readMessage();

    }

    public void connection(String hostname, int port) 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) {

        String outgoingMsg=""; 

        try {
            outgoingMsg = chat.getSendField().getText();  
            chat.getChatArea().append("<" + user2 + ">:\t" + outgoingMsg + "\n");
            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();
            }

        }
    }

    private void readMessage() {
        String incomingMsg = "";

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

    }

    private void setUsers(String user, String user2) {
        this.user = user;
        this.user2 = user2;
    }

}

当我分别调用这两个类时,程序工作,但当我调用服务器类中的客户机类时,程序不工作。

暂无答案!

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

相关问题