gpt4 book ai didi

java - 通过网络发送对象 vector

转载 作者:行者123 更新时间:2023-12-02 08:29:18 25 4
gpt4 key购买 nike

好的,所以我正在编写一个测试程序来将对象 vector 从服务器发送到客户端。

-服务器-

import java.net.*;
import java.util.Vector;
import java.io.*;

public class TestServer{
public static void main(String[] args) throws IOException {

Vector<Obj> obj = new Vector<Obj>();
obj.add(new Obj(5));
obj.add(new Obj(4));
obj.add(new Obj(7));
obj.add(new Obj(8));
obj.add(new Obj(2));

ServerSocket serverSocket = new ServerSocket(4444);
boolean listening = true;

while(listening){
new ClientThread(serverSocket.accept(), obj).start();
}

serverSocket.close();
System.exit(0);
}
}

class ClientThread extends Thread implements Runnable{
Socket acceptedSocket;
Vector<Obj> obj;

public ClientThread(Socket acceptedSocket, Vector<Obj> obj){
super("ClientThread");
this.acceptedSocket = acceptedSocket;
this.obj = obj;
}

public void run(){
try{
Socket clientSocket = acceptedSocket;
clientSocket.setTcpNoDelay(true);
System.out.println("Accepted. Now creating I/O.\n");
DataInputStream in = new DataInputStream(clientSocket.getInputStream());
ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream());
System.out.println("player connected. \n");
out.writeObject(obj);
out.flush();

while(true){
out.writeObject(obj);
out.flush();

sleep(15);
}

}

catch(Exception e){
e.printStackTrace();
System.exit(1);
}


}
}

class Obj implements Serializable{
int i;

public Obj(int i){
this.i = i;
}
}

-客户端-

import java.net.*;
import java.util.Vector;
import java.io.*;

import javax.swing.JOptionPane;


public class TestClient {

static Vector<Obj> obj;
static ClientThread ct;

public static void main(String[] args){
obj = new Vector<Obj>();
connect();

for(int i = 0; i < obj.size(); i++){
System.out.println("Obj: " + obj.get(i).i);
}

System.exit(0);
}

public static void connect(){
String ip = JOptionPane.showInputDialog("Input server IP.");
ct = new ClientThread(ip, obj);
ct.start();
ct.setPriority(Thread.MAX_PRIORITY);
}
}

class Obj implements Serializable{
int i;

public Obj(int i){
this.i = i;
}
}

class ClientThread extends Thread implements Runnable{
Socket socket;
Vector<Obj> obj;
DataOutputStream out;
ObjectInputStream in;
boolean loop = true;

@SuppressWarnings("unchecked")
public ClientThread(String ip, Vector<Obj> obj){
super("ClientThread");

try{
socket = new Socket(ip, 4444);
socket.setTcpNoDelay(true);
out = new DataOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream());
this.obj = obj;
this.obj = (Vector<Obj>) in.readObject();
}

catch(Exception e){
e.printStackTrace();
}
}

public void run(){
try{
while(loop){
try{
if(!socket.isClosed() && socket.isConnected()){
obj = (Vector<Obj>) in.readObject();
this.sleep(15);
}

else
loop = false;

}
catch(Exception e){
e.printStackTrace();
socket.close();
}
}



}

catch(Exception e){
e.printStackTrace();
}
}
}

端口4444已开放。

收到此错误:

Accepted. Now creating I/O.

player connected.

java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at java.io.ObjectOutputStream$BlockDataOutputStream.drain(Unknown Source)
at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(Unknown Source)
at java.io.ObjectOutputStream.writeNonProxyDesc(Unknown Source)
at java.io.ObjectOutputStream.writeClassDesc(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeFatalException(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at ClientThread.run(TestServer.java:49)

最佳答案

您收到此错误是因为客户端终止于 系统.退出(0);在第 21 行调用。

我发现这只是一个测试,但在测试中编写仔细的代码也是一种很好的形式。-使用正确的可见性修饰符- 在finally block 关闭流- 如果您在操作期间不需要线程安全,请使用 ArrayList 而不是 Vector-等等

这段代码包含许多潜在的错误,这就是为什么在第一个站点没有注意到真正的(非常简单)的问题。

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

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