gpt4 book ai didi

java - 流损坏异常

转载 作者:太空宇宙 更新时间:2023-11-04 15:04:00 25 4
gpt4 key购买 nike

我正在使用套接字编写客户端/服务器程序。客户端首先发送文件名,服务器从硬盘读取文件并通过socket发送回客户端。最后客户端将内容写入文件。当我运行代码时,返回 java.io.StreamCorruptedException: invalid stream header 错误。

客户端代码:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ClientSocket {

private static final String SERVER_IP = "10.8.17.218";
private static final int SERVER_PORT = 5000;

String fileName;
String ip;

Socket socket;
Message msg=null,message=null;

ObjectInputStream in = null;
ObjectOutputStream out = null;

ObjectOutputStream toFile=null;

File destFile;

public ClientSocket(String ipi,String fname){
fileName = fname;
ip=ipi;
msg=new Message(fileName);
destFile=new File("C:\\DestinationDirectory",fileName);

try {
socket = new Socket(SERVER_IP, SERVER_PORT);
System.out.println("Connected to server!");
} catch (Exception ex) {
System.out.println("Error connecting to server: " + ex.getMessage());
}

while(true){
try {
if (out == null) {
out = new ObjectOutputStream(socket.getOutputStream());
}

out.writeObject(msg);
out.flush();

//get the reply from the server
if (in == null) {
in = new ObjectInputStream(socket.getInputStream());
}
message = (Message) in.readObject();
//System.out.println("Server said: " + message.getMessage());

} catch (Exception ex) {
System.out.println("Error: " + ex);
}

try {
toFile = new ObjectOutputStream(new FileOutputStream(destFile));
toFile.writeObject(message);
System.out.println(message.getMessage());
} catch (IOException ex) {
Logger.getLogger(ClientSocket.class.getName()).log(Level.SEVERE, null, ex);
}
}


}

public static void main(String[] args) {

ClientSocket cs= new ClientSocket("10.8.17.218","build.sql");

}
}

服务器代码:

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerFile {
private static final int PORT = 5000;

public static void main(String[] args) {
ServerSocket serverSocket = null;
Message message=null,toOut=null;
try {
//Creates a new server socket with the given port number
serverSocket = new ServerSocket(PORT);
} catch (IOException ex) {
System.out.println("Error occured while creating the server socket");
return;
}

Socket socket = null;
try {
//Waits untill a connection is made, and returns that socket
socket = serverSocket.accept();
} catch (IOException ex) {
System.out.println("Error occured while accepting the socket");
return;
}
//Now we have established the a connection with the client
System.out.println("Connection created, client IP: " + socket.getInetAddress());
ObjectInputStream in = null,fromFile=null;
ObjectOutputStream out = null,tempOut=null;
File sourceFile;

FileInputStream from=null;
BufferedInputStream bis;
String name=null;
while(true){
try {
if (in == null) {
in = new ObjectInputStream(socket.getInputStream());
}
message= (Message) in.readObject();
System.out.println("Client said: " + message.getMessage());
name=message.getMessage();
sourceFile=new File("D:\\temp\\",name);

name="D:\\temp\\"+name;
System.out.println(name);

from=new FileInputStream("D:/temp/build.sql");
bis=new BufferedInputStream(from);
fromFile=new ObjectInputStream(bis);
toOut=(Message) fromFile.readObject();
//Send a reply to the client
if (out == null) {
out = new ObjectOutputStream(socket.getOutputStream());
}
out.writeObject(toOut);
out.flush();
} catch (Exception ex) {
System.out.println("Error: " + ex);
}
}
}
}

最佳答案

您在服务器中的每个事务中创建一个新的 ObjectInputStream,但在客户端中使用单个 ObjectOutputStream。在套接字的使用生命周期内,您应该在两端各使用一个。

关于java - 流损坏异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22245118/

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