我有一个使用 GUI 的 Java TCP Client-Server 程序。这个想法是从服务器程序上的文本字段中获取值,将其存储在一个对象中并将其发送到客户端。客户端反序列化对象并使用反序列化对象的值更新文本字段。
聊天服务器.java
public class ChattingServer extends JFrame {
private JTextField senddata;
private JTextField packettype;
private JTextField packetsize;
private JTextField offset;
private JButton sendButton;
private JButton startServer;
private TCPServer mServer;
private TCP_DATA_PACKET TDP;
sendButton = new JButton("Update");
sendButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
TDP.bSendRequestedData = senddata.getText().toString();
TDP.uPacketType = packettype.getText().toString();
TDP.uPacketSize = packetsize.getText().toString();
TDP.uOffset = offset.getText().toString();
mServer.SendObject(TDP);
}
});
startServer = new JButton("Start");
startServer.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
startServer.setEnabled(false);
try{
IP.setText(InetAddress.getLocalHost().getHostAddress());
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
mServer = new TCPServer();
mServer.start();
}
});
public class TCPServer extends Thread{
public TCP_DATA_PACKET packet;
public final int SERVERPORT = Integer.parseInt(port.getText().toString());
private ObjectOutputStream mOut;
public void SendObject(Object obj){
try{
mOut.writeObject(this);
System.out.println("Object sent successfully!");
mOut.flush();
mOut.close();
}catch(IOException ioe){
System.out.println(ioe);
}
}
@SuppressWarnings("resource")
public void run() {
super.run();
try {
System.out.println("S: Connecting...");
ServerSocket serverSocket = new ServerSocket(SERVERPORT);
System.out.println("Server ip:" + InetAddress.getLocalHost().getHostAddress());
Socket client = serverSocket.accept();
System.out.println(client.getRemoteSocketAddress()+" connected\n");
ClientIP.setText(client.getRemoteSocketAddress().toString());
System.out.println("S: Receiving...");
try {
mOut = new ObjectOutputStream(client.getOutputStream());
} catch (Exception e) {
System.out.println("S: Error");
e.printStackTrace();
} finally {
client.close();
System.out.println("S: Done.");
}
} catch (Exception e) {
System.out.println("S: Error");
e.printStackTrace();
}
}
ChattingClient.java 也一样,只是接收对象
public class TCPServer extends Thread{
public TCP_DATA_PACKET packet = null;
public final int SERVERPORT = Integer.parseInt(port.getText().toString());
public final String SERVERADD = IP.getText().toString();
public boolean running = false;
public void run(){
super.run();
running = true;
try {
System.out.println("S: Connecting...");
InetAddress serverAddr = InetAddress.getByName(SERVERADD);
Socket client = new Socket(serverAddr,SERVERPORT);
try {
ObjectInputStream in = new ObjectInputStream(client.getInputStream());
while (running) {
packet = (TCP_DATA_PACKET)in.readObject();
if (packet!= null) {
senddata.setText(packet.bSendRequestedData);
packettype.setText(packet.uPacketType);
packetsize.setText(packet.uPacketSize);
offset.setText(packet.uOffset);
}
}
} catch (Exception e) {
System.out.println("S: Error");
e.printStackTrace();
} finally {
client.close();
System.out.println("S: Done.");
}
} catch (Exception e) {
System.out.println("S: Error");
e.printStackTrace();
}
}
但是,我在服务器端收到 NullPointer 异常。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ChattingServer$1.actionPerformed(ChattingServer.java:81)
在
TDP.bSendRequestedData = senddata.getText().toString();
在
sendButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
TDP.bSendRequestedData = senddata.getText().toString();
TDP.uPacketType = packettype.getText().toString();
TDP.uPacketSize = packetsize.getText().toString();
TDP.uOffset = offset.getText().toString();
mServer.SendObject(TDP);
}
});
和客户端的EOF异常
ObjectInputStream in = new ObjectInputStream(client.getInputStream());
我被困在这里......任何帮助将不胜感激。提前致谢!!
编辑:通过初始化对象 TDP 修复了 NullPointerException:
TCP_DATA_PACKET TDP = new TCP_DATA_PACKET();
在客户端和服务器中。现在,我在服务器端遇到了这个异常
java.net.SocketException: Socket closed
和客户端上的相同 EOF 异常
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at ChattingClient$TCPServer.run(ChattingClient.java:132)
谁能告诉我这里出了什么问题?任何帮助,将不胜感激。:)