简体   繁体   中英

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. when I run this function that sends a file on the raspberry pi 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);

and this is the other function that runs on windows 10

    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.

It turns out it was the firewall blocking connection from the raspberry pi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM