gpt4 book ai didi

java - 通过套接字发送对象

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:53:45 25 4
gpt4 key购买 nike

我正在尝试通过 java 中的套接字发送客户对象。我知道我需要将具有我需要发送的对象的类放在同一个包中,具有相同的 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();
}

}

这是我收到的堆栈跟踪(在服务器端):

java.net.SocketException: Connection reset at 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) at java.io.ObjectInputStream$BlockDataInputStream.readBlockHeader(ObjectInputStream.java:2476) at java.io.ObjectInputStream$BlockDataInputStream.refill(ObjectInputStream.java:2546) at java.io.ObjectInputStream$BlockDataInputStream.skipBlockData(ObjectInputStream.java:2448) at java.io.ObjectInputStream.skipCustomData(ObjectInputStream.java:1944) at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1621) at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1518) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1774) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371) at tankserver.Main.main(Main.java:31) BUILD SUCCESSFUL (total time: 8 seconds)

当我尝试调试时发生以下情况:当我在客户端中放置断点(服务器正常运行)并逐步运行时它工作但是当我在服务器中放置断点(客户端正常运行)并逐步运行时然后它在第 28 行抛出异常 ois = new ObjectInputStream(socket.getInputStream());

最佳答案

I know that i need to put class that has object i need to send in same package

不,除非你的意思是“与它本身相同的包”,这是微不足道的。

have the same serialVersionUID

是的。

and implement Serializable.

是的。

I have already did it but i still can't send object over socket. Where did I [go] wrong?

您没有在客户端类中正确关闭套接字,因此连接被重置。关闭输出流而不是仅仅刷新它。

关于java - 通过套接字发送对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37370860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com