Java 通过套接字流发送对象
尝试编写一个程序,允许客户端相互发送对象。我目前正在使用ObjectOutStream跨套接字发送它,每当我尝试从对象流读取或写入对象时,它都会给出异常:java.io.NotSerializableException。我在网上搜索了这个异常,我得到的解决方案主要是在您从流中发送或读取的对象上实现可序列化接口。我做了,但仍然收到这个例外 以下是对象类:Java 通过套接字流发送对象,java,sockets,serializable,objectinputstream,objectoutputstream,Java,Sockets,Serializable,Objectinputstream,Objectoutputstream,尝试编写一个程序,允许客户端相互发送对象。我目前正在使用ObjectOutStream跨套接字发送它,每当我尝试从对象流读取或写入对象时,它都会给出异常:java.io.NotSerializableException。我在网上搜索了这个异常,我得到的解决方案主要是在您从流中发送或读取的对象上实现可序列化接口。我做了,但仍然收到这个例外 以下是对象类: public class Event implements Serializable { private static final l
public class Event implements Serializable {
private static final long serialVersionUID = 1L;
Integer from;
Vector<Integer> timestamp;
public Event(int identifier, Vector<Integer> timestamp) {
this.from = identifier;
this.timestamp = timestamp;
}
int getFromID() {
return from;
}
Vector<Integer> getTimestamp() {
return timestamp;
}
}
公共类事件实现可序列化{
私有静态最终长serialVersionUID=1L;
来自的整数;
向量时间戳;
公共事件(整数标识符、向量时间戳){
this.from=标识符;
this.timestamp=时间戳;
}
int getFromID(){
返乡;
}
向量getTimestamp(){
返回时间戳;
}
}
下面是客户端类中正在写入其他套接字的部分
Random rand = new Random();
int temp;
while (eventCount < 100) {
System.out.println("Generating Event");
int choice = rand.nextInt(5);
if (choice == 0) {
temp = timestamp.get(identifier);
++temp;
timestamp.set(identifier, temp);
} else {
int randC = rand.nextInt(outputClients.size());
ClientSocket cc = outputClients.get(randC);
cc.out.writeObject(new Event(identifier, timestamp));
}
System.out.println("Done Generating Event");
}
Random rand=new Random();
内部温度;
而(事件计数<100){
System.out.println(“生成事件”);
int choice=rand.nextInt(5);
如果(选项==0){
temp=时间戳.get(标识符);
++温度;
时间戳设置(标识符,临时);
}否则{
int randC=rand.nextInt(outputClients.size());
ClientSocket cc=outputClients.get(randC);
cc.out.writeObject(新事件(标识符、时间戳));
}
System.out.println(“生成事件完成”);
}
这是读取对象的线程
public class ClientConnection extends Thread {
Socket socket;
ObjectOutputStream out;
ObjectInputStream in;
Random rand = new Random();
public ClientConnection(Socket s) {
this.socket = s;
try {
out = new ObjectOutputStream (socket.getOutputStream());
in = new ObjectInputStream (socket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
// execute the event
private void executeEvent(int from, Vector<Integer> x) {
int temp;
synchronized (timestamp) {
for (int i = 0; i < timestamp.size(); ++i) {
if (x.get(i) > timestamp.get(i)) {
timestamp.set(i, x.get(i));
}
}
temp = timestamp.get(from);
++temp;
timestamp.set(from, temp);
}
}
@Override
public void run () {
while (true) {
System.out.println("Reading events");
if (!isAlive) {
break;
}
try {
Event event = (Event) in.readObject();
executeEvent(event.getFromID(), event.getTimestamp());
} catch (ClassNotFoundException e) {
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(timestamp);
}
}
}
public类ClientConnection扩展线程{
插座;
对象输出流输出;
目标输入流;
Random rand=新的Random();
公共客户端连接(套接字){
this.socket=s;
试一试{
out=newObjectOutputStream(socket.getOutputStream());
in=newObjectInputStream(socket.getInputStream());
}捕获(IOE异常){
e、 printStackTrace();
}
}
//执行事件
私有void executeEvent(int-from,向量x){
内部温度;
已同步(时间戳){
对于(int i=0;itimestamp.get(i)){
timestamp.set(i,x.get(i));
}
}
temp=时间戳。获取(从);
++温度;
时间戳设置(从,温度);
}
}
@凌驾
公开作废运行(){
while(true){
System.out.println(“读取事件”);
如果(!isAlive){
打破
}
试一试{
.readObject()中的事件=(事件);
executeEvent(event.getFromID(),event.getTimestamp());
}catch(classnotfounde异常){
}捕获(IOE异常){
e、 printStackTrace();
}
System.out.println(时间戳);
}
}
}
下面是完整上下文的客户机类(假设导入了所有适当的包)
公共类计算机{
静态最终int MAX_SYSTEMS=2;//MAX SYSTEMS
静态向量时间戳=新向量();
静态int[]timestamp1=新int[MAX_SYSTEMS];//时间戳
静态int标识符;//计算机ID
静态int-eventCount=0;//事件计数
静态布尔值isAlive=true;//检查计算机是否处于活动状态
Socket-sockToServer;
PrintWriter输出服务器;
从服务器输入的BufferedReader;
字符串textFromServer;
服务器插座;
静态ArrayList OutputClient=新ArrayList();
静态ArrayList InputClient=新ArrayList();
日志;
公共静态void main(字符串[]args)引发IOException{
新电脑(“127.0.0.1”,8000);
}
公用计算机(字符串主机名,int端口)引发IOException{
//实例化服务器套接字
int socketPort=port+identifier+1;
系统输出打印项次(socketPort);
ss=新服务器套接字(socketPort);
System.out.println(“服务器套接字实例化”);
//创建套接字(使用流)以写入流
对于(int i=0;ipublic class Computer {
static final int MAX_SYSTEMS = 2; // MAX SYSTEMS
static Vector<Integer> timestamp = new Vector<Integer>();
static int[] timestamp1 = new int[MAX_SYSTEMS]; // Time-stamp
static int identifier; // Computer ID
static int eventCount = 0; // Event Counts
static boolean isAlive = true; // Check if the computer is alive
Socket sockToServer;
PrintWriter outputToServer;
BufferedReader inputFromServer;
String textFromServer;
ServerSocket ss;
static ArrayList<ClientSocket> outputClients = new ArrayList<ClientSocket>();
static ArrayList<ClientConnection> inputClients = new ArrayList<ClientConnection>();
Log log;
public static void main(String[] args) throws IOException {
new Computer("127.0.0.1", 8000);
}
public Computer(String hostname, int port) throws IOException {
// Instantiate server socket
int socketPort = port + identifier + 1;
System.out.println(socketPort);
ss = new ServerSocket(socketPort);
System.out.println("Server Socket Instantiated");
// Creating sockets (with streams) to write to stream
for (int i = 0; i < MAX_SYSTEMS; ++i) {
if (i != identifier) {
Socket thing1 = new Socket(hostname, port + i + 1);
ClientSocket cs = new ClientSocket (thing1);
outputClients.add(cs);
}
}
log.write("Client Sockets Instantiated\n");
// Create threads for reading objects and updating timestamp
for (int i = 0; i < MAX_SYSTEMS - 1; ++i) {
ClientConnection clientConn = new ClientConnection(ss.accept());
clientConn.start();
inputClients.add(clientConn);
}
log.write("Server connected to clients");
Random rand = new Random();
// Writing Events
int temp;
while (eventCount < 100) {
System.out.println("Generating Event");
int choice = rand.nextInt(5);
if (choice == 0) {
temp = timestamp.get(identifier);
++temp;
timestamp.set(identifier, temp);
} else {
int randC = rand.nextInt(outputClients.size());
ClientSocket cc = outputClients.get(randC);
cc.out.writeObject(new Event(identifier, timestamp));
}
System.out.println("Done Generating Event");
}
log.write("Computer finished generating events. Continue listening...\n");
outputToServer.println("Finish");
// Wait for Tear Down Message
while (true) {
try {
textFromServer = inputFromServer.readLine();
if (textFromServer.equals("Tear Down")) {
isAlive = false;
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
log.write("Computer shutting off....");
for (int i = 0; i < outputClients.size(); ++i) {
ClientSocket sc = outputClients.get(i);
sc.socket.close();
}
sockToServer.close();
}
// client socket class (organizing)
public class ClientSocket {
Socket socket;
ObjectOutputStream out;
ObjectInputStream in;
public ClientSocket(Socket s) {
try {
this.socket = s;
this.out = new ObjectOutputStream(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Client Socket Created");
}
}
public class Event implements Serializable {
private static final long serialVersionUID = 1L;
Integer from;
Vector<Integer> timestamp;
public Event(int identifier, Vector<Integer> timestamp) {
this.from = identifier;
this.timestamp = timestamp;
}
int getFromID() {
return from;
}
Vector<Integer> getTimestamp() {
return timestamp;
}
}
// send event thread
public class ClientConnection extends Thread {
Socket socket;
ObjectOutputStream out;
ObjectInputStream in;
Random rand = new Random();
public ClientConnection(Socket s) {
this.socket = s;
try {
out = new ObjectOutputStream (socket.getOutputStream());
in = new ObjectInputStream (socket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
// execute the event
private void executeEvent(int from, Vector<Integer> x) {
int temp;
synchronized (timestamp) {
for (int i = 0; i < timestamp.size(); ++i) {
if (x.get(i) > timestamp.get(i)) {
timestamp.set(i, x.get(i));
}
}
temp = timestamp.get(from);
++temp;
timestamp.set(from, temp);
}
}
@Override
public void run () {
while (true) {
System.out.println("Reading events");
if (!isAlive) {
break;
}
try {
Event event = (Event) in.readObject();
executeEvent(event.getFromID(), event.getTimestamp());
} catch (ClassNotFoundException e) {
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(timestamp);
}
}
}
}
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: timetableexchange.Computer
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at timetableexchange.Computer$ClientConnection.run(Computer.java:239)
Caused by: java.io.NotSerializableException: timetableexchange.Computer
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at timetableexchange.Computer.<init>(Computer.java:128)
at timetableexchange.Computer.main(Computer.java:39)
java.io.NotSerializableException: timetableexchange.Computer
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at timetableexchange.Computer.<init>(Computer.java:128)
at timetableexchange.Computer.main(Computer.java:39)