java - 流损坏异常

标签 java sockets

我正在使用套接字编写客户端/服务器程序。客户端首先发送文件名,服务器从硬盘读取文件并通过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/

相关文章:

java - Stringbuilder 格式化日期

java.lang.NoClassDefFoundError : com/google/gwt/core/shared/GWTBridge

c# - 使用 TcpClient 类 C# 的异步套接字客户端

python - 如何与普通用户一起使用 root 创建的套接字

Python视频流到C++服务器

javascript - 如何使用RtcPeerConnection与其他实例连接?

java - 计算方差我得到无穷大

java - 尝试在空对象引用上调用虚拟方法 'void android.widget.ImageView.setVisibility(int)'

java - 一个好的 Java 商务日历库?

java - 套接字连接的单例类不起作用