在通过套接字传输后,为什么我的文件中的数据丢失了?

时间:2015-08-18 18:46:28

标签: java sockets tcp ftp

在尝试将文件上传到服务器或从服务器下载时,我为服务器和客户端编写了相同的代码。

从服务器下载工作正常,我的文件中没有数据丢失,但由于某些原因上传文件,并非所有文件都被传输。

例如,我的客户端上的文件大小小于服务器上的文件大小。然后当它在服务器上打开时,并不是所有的都在那里(因为并非所有的都被收到)

服务器:

算法:

  • 从客户端收到消息
  • 客户端告诉服务器它想要发送文件(推送)

服务器读取放置文件的位置,然后从客户端

接收文件
func ger(ctxt string) func(string, error) error {
    return func(msg string, err error) error {
        return fmt.Errorf("%s : %s : %v", ctxt, msg, err)
    }
}

// And using it:
er := ger("Handling cmd")
er("Add", err)

客户代码:

客户端告诉服务器它想要“推送”一个文件,然后它传递将它放在服务器上的位置,然后传输文件

  public static void GetClientMessage() {

            while (true) {
                try {

                    try {

                        try {
                            serverSocket = new ServerSocket(PORT_NUMBER);
                        } catch (IOException ex) {
                            System.out.println("GetClientMessage():serverSocket:IOException:ex " + ex);
                            SendBackException(ex.toString());  // Inform client
                        }
                        try {
                            System.out.println("Waiting for client");
                            socket = serverSocket.accept();
                        } catch (IOException ex) {
                            System.out.println("GetClientMessage():socket = serverSocket.accept():IOException:ex " + ex);
                            SendBackException(ex.toString());  // Inform client
                        }

                        bufOut = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                        brffReadIn = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));

                        // 1 - Read Line (it is the flag)
                        flag = brffReadIn.readLine();
                        // 2 - Handle Flag
                        HandleClientMessage(flag);

                        // Make decisions based upon that message
                    } catch (IOException ex) {
                        System.out.println("GetClientMessage():IOException:ex: " + ex);
                        SendBackException(ex.toString());  // Inform client
                    }

                    socket.close();
                    serverSocket.close();

                } // Close while loop
                catch (IOException ex) {
                    System.out.println("GetClientMessage:serverSocket.close():IOException:ex " + ex);
                }

            }

        }

           public static void HandleClientMessage(String message) {
           System.out.println("HandleClientMessage:message: '" + message + "'");
            switch (message) {
                case "push":
                    GetClientFile();
                    break;
                case "open_cla":
                    OpenCla();
                    break;
                case "kill_cla":
                    KillCla();
                    break;
                case "get":                
                    SendFile();
                    break;
                default:
                    break;
            }
        }

        // Gets path to where to place file on local
        public static String GetPath() {

            String filePath = " ";

            try {

                bufOut = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                brffReadIn = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));

                filePath = brffReadIn.readLine();

                System.out.println("Path to place file on local: " + filePath);

            } catch (IOException ex) {
                System.out.println(("GetPath():IOException:ex: " + ex));
            }
            return filePath;
        }

        public static void GetClientFile() {

            // Get the location where to place the file on local
            fileOnLocal = GetPath();

            int count;
            try {

                File file = new File(fileOnLocal);
                // Get the size of the file
                long length = file.length();
                byte[] bytes = new byte[16* 1024];
                InputStream in = socket.getInputStream();
                OutputStream out = new FileOutputStream(fileOnLocal);

                while ((count = in.read(bytes)) > 0) {
                    System.out.println("strByteArray: " + strByteArray);
                    out.write(bytes, 0, count);
                }
                out.flush();

                System.out.println("File Size in bytes: " + file.length());
                if (file.length() < 5) {
                    System.out.println("FileClient:Error:File:" + fileOnLocal + " not found on server");
                    out.close();
                    in.close();
                    socket.close();
                    file.delete();
                    System.out.println("File:" + file.getAbsolutePath() + " deleted");
                } else {
                    out.close();
                    in.close();
                    socket.close();
                }

            } catch (IOException ex) {
                System.out.println(":FileClient:GetServerFile():IOException:ex:" + ex);
            }
        }

执行此操作后,在客户端上接收字节,但不是全部。有没有我编码错误的东西?我的byte []数组应该是不同的大小吗?这将用于Win7&amp; Win8,将来可能还有Mac。

编辑:我明白了。我试图发送一条消息后跟一串字节太快了。

这解决了我的问题:

public void SendFlagToServer(String flag){

        try {

            bufOut = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            brffReadIn = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));

            bufOut.write(flag);
            bufOut.newLine();
            bufOut.flush();

            System.out.println(host + ":SendFlagToServer: " + flag);

        } catch (IOException ex) {
            Logger.Log((host + ":FileClient:SendFileToGetToServer():IOException:ex: " + ex));
        }
    }

1 个答案:

答案 0 :(得分:1)

您正在关闭第一个客户端连接后的套接字

socket.close();
serverSocket.close();

解决方案:

  
      
  • 接受客户端套接字后,使用套接字连接创建一个新线程并处理该线程中的所有IO操作

  •   
  • 不要关闭serverSocket。关闭serverSocket后,将不再接受客户端套接字连接。

  •   

你能提供你得到的例外吗?