簡體   English   中英

通過套接字接收文件,TCP連接凍結

[英]Receive file via socket, TCP connection freezes

我已經使用套接字4小時了,我使用的方式是只有一個應用程序作為客戶端和服務器,一旦客戶端連接它就是用新客戶端打開theard 並等待消息

一旦消息發送到服務器,客戶端將收到響應,該部分正常工作沒有任何問題。

客戶Theard的一部分:

while (true)
        {
            InputStreamReader IR = new InputStreamReader(clientSocket.getInputStream());
            BufferedReader BR = new BufferedReader(IR);
            PrintStream PS = new PrintStream(clientSocket.getOutputStream());
            String message = BR.readLine();
            if (message != null)
            {
                System.out.println(clientSocket.getInetAddress() + ":" + clientSocket.getPort() + " has connected."+message);
                if (message.equals("exit"))
                {
                    PS.println("Exiting...");
                    exit();
                }
                else if (message.equals("list"))
                {
                    getList(PS);
                }
                else if ((message.contains("get") && (message.contains(",") && (message.contains(" ")))))
                {
                    String[] spliter = message.split(" ");
                    String[] file = spliter[1].split(",");
                    String file_name = file[0];
                    String file_md5 = file[1];
                    getFile(file_name, file_md5, clientSocket);
                }
            }
            else
            {
                break;
            }

        }

服務器支持2條消息,第一條是“list”,send one命令是“get with values”。

如果客戶端將請求命令“列表”它將運行此:有一個“服務器/客戶端”,它正在發送請求並接收一行字符串 ,它正在工作沒有任何問題,我從服務器接收文件列表。

PrintStream PS = new PrintStream(clientSocket.getOutputStream());
        PS.println("list");
        InputStreamReader IR = new InputStreamReader(clientSocket.getInputStream());
        BufferedReader BR = new BufferedReader(IR);
        String lista_plikow = BR.readLine();
        if ( lista_plikow != null)
        {
            return lista_plikow;
        }

但我有問題使用stackoverflow上的代碼通過套接字發送文件 ,但“ 接收 ”不起作用,有我的接收函數,循環始終為0(即使第一個字節長度正確),但是字節長度是正確的,它使用新創建的文件但沒有發生任何事情,文件總是在使用,並且有0個字節而不是PS.println的內容。

PrintStream PS = new PrintStream(clientSocket.getOutputStream());
    PS.println("get "+filename+","+file_md5);
    int bytesRead;
    int current = 0;
    FileOutputStream fos = null;
    BufferedOutputStream bos = null;
    try
    {
        byte [] mybytearray  = new byte [Integer.parseInt(size)];
        InputStream is = clientSocket.getInputStream();
        fos = new FileOutputStream(filename + ".recived");
        bos = new BufferedOutputStream(fos);
        bytesRead = is.read(mybytearray,0,mybytearray.length);
        current = bytesRead;
        System.out.println("X" + bytesRead);
        do {
               bytesRead =
                  is.read(mybytearray, current, (mybytearray.length-current));
            System.out.println(bytesRead + " = " + current + " " + (mybytearray.length-current));

               if(bytesRead >= 0) current += bytesRead;
               System.out.println(bytesRead);
        } while(bytesRead > -1);
        bos.write(mybytearray, 0 , current);
        bos.flush();
        System.out.println("File " + "recived." +filename.replace(":", " ")
            + " downloaded (" + current + " bytes read)");
    }catch (Exception e)
    {
        System.out.println(e.getMessage());
    }

最后一部分是“PS.println(”get“+ filename +”,“+ file_md5);” 正是這樣做,發送工作正常:

FileInputStream fis = null;
            BufferedInputStream bis = null;
            OutputStream os = null;

            String the_file = TorrentAppGui.folder+"\\"+file_name.replace(":", " ");
             File myFile = new File (the_file);
              byte [] mybytearray  = new byte [(int)myFile.length()];
              fis = new FileInputStream(myFile);
              bis = new BufferedInputStream(fis);
              bis.read(mybytearray,0,mybytearray.length);
              os = clientSocket.getOutputStream();
              System.out.println("Sending " + the_file + "(" + mybytearray.length + " bytes)");
              os.write(mybytearray, 0, mybytearray.length);
              os.flush();
              System.out.println("Done.");

我不知道為什么我不能保存“ get ”命令收到的字節,你有什么想法嗎? 我知道只有“receve”功能不起作用,因為如果我通過telnet登陸應用程序,我可以在控制台中獲取該文件,但它沒有達到我的目標。 從cli看屏幕。

通過telnet連接是工作文件

您不能在同一個套接字上混合使用緩沖和非緩沖的流/讀取器/寫入器。 您將丟失緩沖區中的數據。 在套接字的生命周期中使用相同的流對。 在這種情況下,我將使用DataInputStreamDataOutputStream ,以及消息和文件名的readUTF()/writeUTF()方法。 您還需要在文件之前發送文件長度,除非該文件是通過連接發送的最后一件事:否則對等體將不知道何時停止讀取文件並返回並再次開始閱讀消息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM