簡體   English   中英

創建套接字連接到 windows 10 機器從 Raspberry pi 4 Raspbian 不工作 (Java)

[英]creating Socket to connect to windows 10 machine from Raspberry pi 4 Raspbian does not work (Java)

我有一個安裝了 Raspbian 的 Raspberry pi 4,我有一台安裝了 Windows 10 的計算機。我寫了兩個函數,一個發送文件,另一個接收文件。 當我運行這個 function 在樹莓派 4 上發送文件時:

    public static void sendFile(String fileName, String ip)
    {
        BufferedOutputStream outputStream = null;
        PrintWriter writer = null;
        BufferedReader reader = null;
        FileInputStream filein = null;
        File file = new File(fileName);
        
        if (!file.exists())
        {
            System.out.println(fileName + " does not exist");
            return;
        }
        
        try
        {
           Socket socket = new Socket(ip, port);
           outputStream = new BufferedOutputStream(socket.getOutputStream());
           writer = new PrintWriter(socket.getOutputStream());
           reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
           filein = new FileInputStream(file);
           long fileSize = file.length();
           
           writer.println(fileName);        // sending file name
           writer.println(fileSize);   // sending file size in bytes
           writer.flush();
           
           byte[] dataBuffer = new byte[1024];
           int numberOfReadBytes = 0;          // the number of read bytes for each read() function call
           System.out.println("Entering the loop");
           for(long i = 0; i < fileSize && numberOfReadBytes > -1;)
           {
               numberOfReadBytes = filein.read(dataBuffer);             // read read() function returns the number of bytes tha has been assigned to the array or -1 if EOF(end of file) is reached
               outputStream.write(dataBuffer, 0, numberOfReadBytes);    // writing the bytes in dataBuffer from index 0 to index numberOfBytes
               i += numberOfReadBytes;
           }
           
           outputStream.flush();
           System.out.println(fileName + " sent to " + ip);
           String status = reader.readLine();
           System.out.println("Status: " + status + "\t file save successfully on the other machine.");
        }
        catch(IOException ioe)
        {
            System.err.println("Status: 0\n" + ioe.getMessage());
        }
        finally     // closing streams
        {
            try
            {
                outputStream.close();
                reader.close();
                writer.close();
                filein.close();
            }
            catch (IOException ioe)
            {
                System.err.println("Error closing the connection.");
            }
        }
    }

它停在這一行Socket socket = new Socket(ip, port);

這是在 windows 10 上運行的另一個 function

    public static void receiveFile()
    {
        // 1- read the file name
        // 2- read the size of the file
        // 3- read the file and write it
        
        ServerSocket server = null;
        Socket socket = null;
        BufferedReader reader = null;
        BufferedInputStream inputStream = null;
        FileOutputStream fileout = null;
        PrintWriter writer = null;
        
        try
        {
            server = new ServerSocket(9999);
            socket = server.accept();
            reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            inputStream = new BufferedInputStream(socket.getInputStream());
            writer = new PrintWriter(socket.getOutputStream());
            
            String fileName = reader.readLine();                // reading file name
            long fileSize = Long.parseLong(reader.readLine());  // reading file size
            System.out.println(fileSize);

            // reading file data and write the data
            File file = new File(fileName);
            fileout = new FileOutputStream(file);
            
            for (long i = 0; i < fileSize; ++i)
            {
        fileout.write(inputStream.read());
        System.out.println(i);
            }

            fileout.flush();
            fileout.close();
            
            writer.println('1');

        System.out.println("Status: 1");
            System.out.println(fileName+ " is saved successfully");
        }
        catch (IOException ioe)
        {
            System.err.println("Status: 0");
            System.err.println(ioe.getMessage());
        }
        finally
        {
            try
            {
                reader.close();
                inputStream.close();
            }
            catch(IOException ioe)
            {
                System.err.println("Error closing connection\n" + ioe.getMessage());
            }
        }   
    }

我認為 windows 10 防火牆阻止連接,但我不確定。

原來是防火牆阻止了樹莓派的連接

暫無
暫無

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

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