我试图通过java中的套接字发送一个custome对象。我知道我需要把具有对象的类放在同一个包中,具有相同的serialVersionUID并实现Serializable。我已经做到了,但我仍然无法通过套接字发送对象。我哪里错了?
这是客户代码:
客户端中的类用户
package data;
import java.io.Serializable;
public class User implements Serializable {
/**
*
*/
public static final long serialVersionUID = 1L;
private String username = null;
private String hashedPassword = null;
private Integer code = null;
private String from = null;
private String to = null;
private Object data = null;
public User() {
}
public User(Integer code) {
this.code = code;
}
public User(Integer code, Object data) {
this.code = code;
this.data = data;
}
public User(String username, String hashedPassword, Integer code, String from, String to, Object data) {
this.username = username;
this.hashedPassword = hashedPassword;
this.code = code;
this.from = from;
this.to = to;
this.data = data;
}
public Integer getCode() {
return code;
}
// other getters and setters
}
发送用户类对象的代码:
package tankclient;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import data.*;
public class Main {
public static void main(String args[]) {
Socket socket = null;
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
boolean isConnected = false;
String ipServer = "localhost";
int portServer = 4444;
try {
// Print("connecting to server...");
socket = new Socket(ipServer, portServer);
isConnected = true;
oos = new ObjectOutputStream(socket.getOutputStream());
ois = new ObjectInputStream(socket.getInputStream());
oos.writeObject(new User());
// Print("connect server success");
} catch (Exception ex) {
isConnected = false;
ex.printStackTrace();
}
// DataTransfer transfer = new DataTransfer();
// //transfer.SendDataRequest(new DataRequest(3));
// transfer.TestSend();
}
}
这是服务器的代码:
用户类代码:与客户端用户完全相同(我从客户端复制并粘贴到服务器中)
接收用户对象的服务器代码:
package tankserver;
import data.*;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Main {
public static void main(String args[]) {
ServerSocket serverSocket = null;
Socket socket = null;
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
boolean isConnected = false;
int portServer = 4444;
try {
System.out.println("waiting for client to connect ...");
serverSocket = new ServerSocket(portServer);
socket = serverSocket.accept();
oos = new ObjectOutputStream(socket.getOutputStream());
ois = new ObjectInputStream(socket.getInputStream());
isConnected = true;
System.out.println("connected");
User obj = (User) ois.readObject();
System.out.println(obj.toString());
} catch (Exception ex) {
isConnected = false;
ex.printStackTrace();
}
//DataTransfer transfer = new DataTransfer();
//transfer.GetDataRequest();
//transfer.TestRecieve();
}
}
这里是我收到的stacktrace(在服务器端):
java.net.SocketException:连接重置为 java.net.SocketInputStream.read(SocketInputStream.java:209)at java.net.SocketInputStream.read(SocketInputStream.java:141)at java.net.SocketInputStream.read(SocketInputStream.java:223)at java.io.ObjectInputStream中的$ PeekInputStream.peek(ObjectInputStream.java:2296) 在 java.io.ObjectInputStream中的$ BlockDataInputStream.readBlockHeader(ObjectInputStream.java:2476) 在 java.io.ObjectInputStream中的$ BlockDataInputStream.refill(ObjectInputStream.java:2546) 在 java.io.ObjectInputStream中的$ BlockDataInputStream.skipBlockData(ObjectInputStream.java:2448) 在 java.io.ObjectInputStream.skipCustomData(ObjectInputStream.java:1944) 在 java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1621) 在 java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1518) 在 java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1774) 在java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371) at tankserver.Main.main(Main.java:31)BUILD SUCCESSFUL(总时间:8 秒)
当我尝试调试然后跟随情况发生:当我在客户端(服务器运行normaly)中放置断点并逐步运行然后它工作但是当我在服务器中放置断点(客户端运行normaly)并运行步骤然后它在第28行抛出异常ois = new ObjectInputStream(socket.getInputStream());
答案 0 :(得分:1)
我知道我需要把具有对象的类放在同一个包
中
不,除非你的意思是“与自己相同的包装”,这是微不足道的。
具有相同的serialVersionUID
是
并实现Serializable。
是
我已经做过但我仍然无法通过套接字发送对象。我在哪里错了?
您没有在客户端类中正确关闭套接字,因此连接已重置。关闭输出流而不是仅刷新它。