Java のネットワーク プログラミングを学ぼうとしていますが、単純なチャット プログラムをサーバーとホスト間で接続するのに問題があります。これが私が経験していることです:
クライアント プログラムをサーバーに接続しようとすると、ヌル ポインター例外が発生し、接続できません。
デバッグ中に次の行を見つけました
connection.equals(server.accept());
サーバークラスのwaitForCommuinication()メソッドでは実行されません。
これは、行が
client = new Socket(InetAddress.getByName(chatServer), 50499);
クライアント クラスのメソッド connectToServer() で実行されます。
localhost ip add を使用してこのコードを実行しています
サーバークラス:
public class Server extends JFrame {
private JTextField enterField;
private JTextArea displayArea;
private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket server;
private Socket connection;
private int counter = 1;
public Server() {
super ("Server");
enterField = new JTextField();
enterField.setEditable(true);
enterField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
sendData(event.getActionCommand());
enterField.setText("");
}
});
add(enterField, BorderLayout.NORTH);
displayArea = new JTextArea();
add (new JScrollPane(displayArea));
setSize(300,150);
setVisible(true);
}
public void runServer() {
try {
server = new ServerSocket(50499, 100);
//displayMessage("\n Listening on Port: " + server.getLocalPort() + "\n");
while (true) {
try {
waitForCommunication();
getStreams();
processConnection();
} catch (EOFException eofException) {
displayMessage("\n Server terminated connection ");
} finally {
closeConnection();
++counter;
}
}
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
private void closeConnection() {
displayMessage("\nTerminating connection\n");
setTextFieldEditable(false);
try {
output.close();
input.close();
connection.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
private void displayMessage(final String string) {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
displayArea.append(string);
}
});
}
private void processConnection() throws IOException {
String message = "Connection Sucessful";
sendData(message);
setTextFieldEditable(true);
do {
try {
message = (String) input.readObject();
displayMessage("\n" + message);
} catch (ClassNotFoundException classNotFoundException) {
displayMessage("\nnUnknown object type recieved");
}
} while (!message.equals("Client>>> TERMINATE"));
}
private void setTextFieldEditable(final boolean editable) {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
enterField.setEditable(editable);
}
});
}
private void getStreams() throws IOException {
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
displayMessage("\nGOt I/O stream \n");
}
private void waitForCommunication() throws IOException {
displayMessage("Waiting for cennection \n");
connection.equals(server.accept());
displayMessage("Connection" + counter + " received from: "
+ connection.getInetAddress().getHostName());
}
private void sendData(String message) {
try {
output.writeObject("SERVER>>> " + message);
output.flush();
displayMessage("\nServer>>> " + message);
} catch (IOException ioException){
displayArea.append("\nError Writing Object");
}
}
}
クライアントクラス:
public class Client extends JFrame {
private JTextField enterField;
private JTextArea displayArea;
private ObjectOutputStream output;
private ObjectInputStream input;
private String message = "";
private String chatServer;
private Socket client;
public Client(String host){
super ("Host");
chatServer = host;
enterField = new JTextField();
enterField.setEditable(false);
enterField.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent event) {
sendData(event.getActionCommand());
enterField.setText("");
}
});
add(enterField, BorderLayout.NORTH);
displayArea = new JTextArea();
add(new JScrollPane(displayArea));
setSize(300,150);
setVisible(true);
}
public void runClient(){
try{
connectToServer();
getStreams();
processConnection();
} catch (EOFException eofException){
displayMessage("\nClient terminated connection");
} catch (IOException ioException){
ioException.printStackTrace();
} finally {
closeConnection();
}
}
private void closeConnection() {
displayMessage("\nClosing connection");
setTextFieldEditable(false);
try{
output.close();
input.close();
client.close();
} catch (IOException ioException){
ioException.printStackTrace();
}
}
private void displayMessage(final String messageToDisplay) {
SwingUtilities.invokeLater( new Runnable() {
@Override
public void run() {
displayArea.append(messageToDisplay);
}
});
}
private void processConnection() throws IOException {
setTextFieldEditable(true);
do {
try{
message = (String) input.readObject();
} catch (ClassNotFoundException classNotFoundException){
displayMessage("\nUnknown object type recieved");
}
} while (!message.equals("SERVER>>> TERMINATE"));
}
private void setTextFieldEditable(final boolean b) {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
enterField.setEditable(b);
}
});
}
private void getStreams() throws IOException {
output = new ObjectOutputStream(client.getOutputStream());
output.flush();
input = new ObjectInputStream(client.getInputStream());
displayMessage("\nGot I/O streams!\n");
}
private void connectToServer() throws IOException {
displayMessage("Attempting connection\n");
client = new Socket(InetAddress.getByName(chatServer), 50499);
displayMessage("Connected to: " + client.getInetAddress().getHostName());
}
protected void sendData(String actionCommand) {
try{
output.writeObject("CLIENT>>> " + actionCommand);
output.flush();
displayMessage("\nCLIENT>>> " + actionCommand);
} catch (IOException ioException){
displayArea.append("\nError sending Message");
}
}
}