流损坏的例外

时间:2014-03-07 08:32:00

标签: java sockets

我正在使用套接字编写客户端/服务器程序。客户端首先发送文件名,服务器从硬盘读取文件,然后通过套接字发送回客户端。最后,客户端将内容写入文件。当我运行代码时,会返回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);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您在服务器中为每个事务创建了一个新的ObjectInputStream,但您在客户端中使用了一个ObjectOutputStream。你应该在插座的整个生命周期中使用两端中的一个。