java套接字上的读写对象
我试图在客户端和服务器之间写入和读取对象,但当我在客户端的输出中接收到对象时,我接收到server.Coordinates,然后服务器崩溃 通过调试,我发现该函数不起作用,但坐标类是相同的:java套接字上的读写对象,java,sockets,serversocket,Java,Sockets,Serversocket,我试图在客户端和服务器之间写入和读取对象,但当我在客户端的输出中接收到对象时,我接收到server.Coordinates,然后服务器崩溃 通过调试,我发现该函数不起作用,但坐标类是相同的: void readCoordinates() throws IOException, ClassNotFoundException { //creazione e ricezione oggetto coordinate Coordinates coordinate
void readCoordinates() throws IOException, ClassNotFoundException {
//creazione e ricezione oggetto coordinate
Coordinates coordinates = (Coordinates) objectInputStream.readObject();
System.out.println("" + coordinates.getX() + " - " + coordinates.getY());
}
这是服务器:
public class Server {
static Server server;
//socket server
ServerSocket serverSocket;
//stream output
OutputStream outputStream;
ObjectOutputStream objectOutputStream;
//stream input
InputStream inputStream ;
ObjectInputStream objectInputStream;
public static void main (String[] args) {
//Server start
server = new Server();
server.start();
}
public void start(){
try {
//creazione server socket
serverSocket = new ServerSocket(8080);
System.out.println("ServerSocket awaiting connections...");
//accept connection
Socket socket = serverSocket.accept();
System.out.println("Connection from " + socket + "!");
//output
outputStream = socket.getOutputStream();
objectOutputStream = new ObjectOutputStream(outputStream);
//input
inputStream = socket.getInputStream();
objectInputStream = new ObjectInputStream(inputStream);
//receive coordinates
readCoordinates();
//write coordinates
writeCoordinates(1, 1);
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println("Error");
System.exit(1);
}
}
void readCoordinates() throws IOException, ClassNotFoundException {
//creazione e ricezione oggetto coordinate
Coordinates coordinates = (Coordinates) objectInputStream.readObject();
System.out.println("" + coordinates.getX() + " - " + coordinates.getY());
}
public void writeCoordinates(int x, int y) throws IOException{
//creazione e invio oggetto coordinate
Coordinates coordinates = new Coordinates(x, y);
objectOutputStream.writeObject(coordinates);
}
}
这是客户:
public class ConnectionManager {
//server
String serverIP = "127.0.0.1";
int serverPort = 8080;
Socket socket;
// stream output
OutputStream outputStream;
ObjectOutputStream objectOutputStream;
// stream input
InputStream inputStream;
ObjectInputStream objectInputStream;
public ConnectionManager() {
try {
//creation socket
System.out.println("Socket creation...");
this.socket = new Socket(serverIP,serverPort);
//creation input e output stream
System.out.println("Creation input and output stream...");
this.inputStream = this.socket.getInputStream();
this.objectInputStream = new ObjectInputStream(this.inputStream);
this.outputStream = socket.getOutputStream();
this.objectOutputStream = new ObjectOutputStream(this.outputStream);
} catch (UnknownHostException e){
System.err.println("Unreacheable host");
} catch (IOException e) {
System.out.println(e.getMessage());
System.out.println("Error");
System.exit(1);
}
}
public void writeCoordinates(int x, int y) throws IOException{
//send coordinates
Coordinates coordinates = new Coordinates(x, y);
objectOutputStream.writeObject(coordinates);
}
void readCoordinates() throws IOException, ClassNotFoundException {
//read coordinates
Coordinates coordinates = (Coordinates) objectInputStream.readObject();
System.out.println("" + coordinates.getX() + " - " + coordinates.getY());
}
}
“坐标”类是相同的:
public class Coordinates implements Serializable{
//coordinate
int x;
int y;
Coordinates(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
我错在哪里?我也不能从服务器写入到客户端
祝您有愉快的一天我想您的客户端程序将在向服务器发送coords后结束。服务器在收到跳线后尝试向客户端发送另一个,但客户端已关闭连接 服务器代码:
Server server = new Server();
server.start();
和客户端代码:
ConnectionManager connectionManager = new ConnectionManager();
connectionManager.writeCoordinates(5,5);
connectionManager.readCoordinates();
我添加了1行connectionManager.readCoordinates()代码>将跳线发送到服务器后。此线路等待响应,在向服务器发送跳线后不会立即关闭连接。我找到了答案。
要通过套接字传递对象,类和包必须相同,然后您需要设置相同的SerialVersionId,请显示服务器的确切输出。@code PeeditorServerSocket等待连接。。。来自套接字的连接[addr=/127.0.0.1,端口=61595,localport=8080]!client.Coordinates错误Java结果:1构建成功(总时间:6秒)
我发现在服务器中,当我到达objectInputStream行时,变量为空,服务器崩溃,但在客户端objectInputStream上works@Code-学徒我现在正在调试,我发现问题出在从套接字读取对象的指令上,导致问题的坐标是什么?@NomadMaker我尝试了许多组合,但读取函数从未起作用。我认为问题出在cast或类中,因为我在套接字上写入对象时,客户端不会停止。仅当调用该函数时,客户端或服务器崩溃。