简体   繁体   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)

I have a Raspberry pi 4 with Raspbian installed, and I have a computer with Windows 10 installed.I wrote two functions one send a file and the other one receive the file.我有一个安装了 Raspbian 的 Raspberry pi 4,我有一台安装了 Windows 10 的计算机。我写了两个函数,一个发送文件,另一个接收文件。 when I run this function that sends a file on the raspberry pi 4:当我运行这个 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.");
            }
        }
    }

it stops at this line Socket socket = new Socket(ip, port);它停在这一行Socket socket = new Socket(ip, port);

and this is the other function that runs on windows 10这是在 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());
            }
        }   
    }

I think windows 10 firewall blocks connection, but I am not sure.我认为 windows 10 防火墙阻止连接,但我不确定。

It turns out it was the firewall blocking connection from the raspberry pi原来是防火墙阻止了树莓派的连接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Raspberry Pi 3上的Windows 10物联网能否很好地运行Java程序? - Can Windows 10 iot on Raspberry Pi 3 run java program well? Raspbian / Java:Classpath和MySQL,可在Windows中运行,而不在Pi上运行 - Raspbian/Java: Classpath and MySQL, working in Windows and not on Pi 在启动时引导Java应用程序-Raspberry Pi-Raspbian-Shell脚本 - Booting Java Application On Startup - Raspberry Pi - Raspbian - Shell Script 通过套接字将字符串从Java(PC)发送到C(Raspberry Pi) - Sending string from Java (PC) to C (Raspberry Pi) via Socket 如何将 ACLMessage 从在 Windows 上运行的 JADE 平台发送到在 Raspberry pi (Raspbian) 上运行的另一个 JADE 平台? - How can I send an ACLMessage from a JADE Platform running on a windows to another JADE Platform running on a Raspberry pi (Raspbian)? 不同计算机上的Java套接字不起作用 - Java Socket on Different Machine Does Not Work 已解决 - Raspberry Pi 无法连接到 Windows Websocket - SOLVED - Raspberry Pi unable to connect to Windows Websocket 通过Java .jar捕获时,Gphoto2无法保存图像(Raspberry Pi-Raspbian) - Gphoto2 fails to save image when captured via Java .jar (Raspberry Pi - Raspbian) Java 8 上 Windows 10 - CLASSPATH 不起作用 - Java 8 on Windows 10 - CLASSPATH does not work 如何从Raspberry Pi我的firebase项目连接Java类? - How do I connect a Java class from Raspberry Pi my firebase project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM