简单的客户端服务器聊天应用程序

时间:2016-01-06 06:47:10

标签: java swing networking serversocket

我正在尝试使用Stream套接字和Swing的简单客户端服务器聊天应用程序。但它没有按预期工作。显然服务器只是等待连接而不继续。

我有三个java文件,Server.java,Client.java和ChatApp.java

Server.java

package com.htpj.networkingChatApp;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Client extends JFrame {

    private static final long serialVersionUID = 1L;
    private JTextField messageEnterField;
    private JTextArea displayArea;

    private String chatHost;
    private Socket clientSocket;

    private ObjectInputStream input;
    private ObjectOutputStream output;

    private String message = "";

    public Client(String host) {
        this.chatHost = host;
    }

    protected void clientGui() {
        messageEnterField = new JTextField();

        setMessageEnterFieldEditable(false);

        messageEnterField.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                sendDataToServer(e.getActionCommand());
            }
        });

        add(messageEnterField, BorderLayout.SOUTH);

        displayArea = new JTextArea();
        add(new JScrollPane(displayArea), BorderLayout.CENTER);
        displayArea.setEditable(false);

        setTitle("Chat Client");
        setSize(500, 500);
        setLocationRelativeTo(null);
        setVisible(true);

    } // END OF clientGui method

    // connect to the server and process message from server
    protected void runClient() throws UnknownHostException, IOException, ClassNotFoundException {

        // connect to server, get Streams, process connection
        try {
            connectToServer();
            getStreams();
            processConnection();

        } catch (EOFException e) {
            displayMessage("\n client closed the connection");
        } finally {
            closeConnection();
        }
    }

    private void connectToServer() throws UnknownHostException, IOException {

        displayMessage("Attempting Connection to Server: \n");

        clientSocket = new Socket(InetAddress.getByName(chatHost), 2000);
        displayMessage(
                "Connected to: " + clientSocket.getInetAddress().getHostName() + "on Port : " + clientSocket.getPort());

    } // END OF method connectToServer

    private void getStreams() throws IOException {

        output = new ObjectOutputStream(clientSocket.getOutputStream());
        output.flush();

        input = new ObjectInputStream(clientSocket.getInputStream());

        displayMessage("\n  Got I/O stream \n ");

    } // END OF getStreams method

    private void processConnection() throws ClassNotFoundException, IOException {

        displayMessage("\n connected to server\n");

        setMessageEnterFieldEditable(true);

        do {

            message = (String) input.readObject();
            displayMessage("\n" + message);

        } while (!message.equals("SERVER>> TERMINATE"));

    } // end of processConnection method

    private void closeConnection() throws IOException {
        displayMessage("\n closing connection ");
        input.close();
        output.close();
        clientSocket.close();
        setMessageEnterFieldEditable(false);

    } // end of closeConnection method

    private void displayMessage(final String messageToShow) {

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                displayArea.append(messageToShow);
            }
        });

    } // end of displayMessage method

    private void sendDataToServer(String messageToServer) {
        try {
            output.writeObject(messageToServer);
            output.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

    } // end of sendDataToServer method

    private void setMessageEnterFieldEditable(final boolean isEditable) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                messageEnterField.setEditable(isEditable);
            }
        });
    }

}

Client.java

package com.htpj.networkingChatApp;

import java.io.IOException;

public class ChatApp{
    public static void main(String[] args) {

        Server chatServer = new Server();
        Client chatClient = new Client("127.0.0.1");

        chatServer.serverGui();
        chatClient.clientGui();

         chatServer.runServer();
        try {
            chatClient.runClient();
        } catch (ClassNotFoundException | IOException e) {
            e.printStackTrace();
        }

    }
}

ChatApp.java

minvallimit = floor(minval/roudingvalue)*roundingvalue
maxvallimit = ceil(maxval/roudingvalue)*roundingvalue

0 个答案:

没有答案