Linked Questions

Popular Questions

I can transfer String data from Python client to JAVA server and I can transfer image file from Python client to JAVA server but when I combine these two codes to transfer image file after transferring String data I can't transfer image file successfully. It create image file but there is no content. Belows are my codes. I hope you help me. Thank you for reading.

JAVA server code

package manager;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Manager {

    public static void main(String[] args) {

        String AllParkingLotInfo[] = new String[4];
        String line;
        int i = 0;

        try {
            ServerSocket ParkingLotManager = new ServerSocket(6000);
            System.out.println("Server On\n");

            Socket ParkingLot = ParkingLotManager.accept();
            System.out.println("Client Connected\n");

            BufferedReader ParkingLotInfoInput = new BufferedReader(
                    new InputStreamReader(
                            ParkingLot.getInputStream(), "UTF-8"
                    )
            );

            while((line = ParkingLotInfoInput.readLine()) != null && i <= 3)                   {
                AllParkingLotInfo[i] = line;
                i++;
            }

            Date today = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String time = dateFormat.format(today);
            File dir = new File(time);
            dir.mkdir();

            DataInputStream dis = new         DataInputStream(ParkingLot.getInputStream());
            FileOutputStream fos = new     FileOutputStream("C:\\Users\\USER\\Documents\\eclipse-    workspace\\ParkingLotManagementExample\\"+""+time+"\\"+AllParkingLotInfo[1]+"_"+    AllParkingLotInfo[2]+".jpg");
            byte[] buffer = new byte[300000];

            int FileSize = 300000;
            int read = 0;
            int totalRead = 0;
            int remaining = FileSize;

            while((read = dis.read(buffer, 0, Math.min(buffer.length,     remaining))) > 0) {
                totalRead += read;
                remaining -= read;
                System.out.println("read " + totalRead + " bytes.");
                fos.write(buffer, 0, read);
            }

            fos.close();
            dis.close();

            ParkingLot.close();
            ParkingLotManager.close();

        } catch(IOException e) {
            e.printStackTrace();
        }

    }

}

Python client code

from socket import *
from select import select
from datetime import datetime
import sys

HOST = '' # sorry it is my ip
PORT = 6000
BUFSIZE = 1024

ParkingLot_1 = socket(AF_INET, SOCK_STREAM)

ParkingLotNumber = '1'
PlateNumber = '11A 1111'
ParkingInDate = datetime.today().strftime("%Y %m %d %H %M")
ParkingOutDate = '2019 02 15 00 19'

image = '/home/pi/Downloads/test.jpg'

try:
    ParkingLot_1.connect((HOST,PORT))

    ParkingLot_1.sendall(bytes(ParkingLotNumber, encoding = 'utf8'))
    ParkingLot_1.sendall(bytes("\n", encoding = 'utf8'))

    ParkingLot_1.sendall(bytes(PlateNumber, encoding = 'utf8'))
    ParkingLot_1.sendall(bytes("\n", encoding = 'utf8'))

    ParkingLot_1.sendall(bytes(ParkingInDate, encoding = 'utf8'))
    ParkingLot_1.sendall(bytes("\n", encoding = 'utf8'))

    ParkingLot_1.sendall(bytes(ParkingOutDate, encoding = 'utf8'))
    ParkingLot_1.sendall(bytes("\n", encoding = 'utf8'))

    ParkedCar = open(image, 'rb')
    ConvertedParkedCar = ParkedCar.read()

    ParkingLot_1.sendall(ConvertedParkedCar)

    ParkedCar.close()

    ParkingLot_1.close()
except Exception as e:
    print(e)

Related Questions