0

I have a python server that my java client will connect to and upon connection, python server will send an image to the Java client. I am getting a file on the Java side but the image is corrupted and I can not open it. Its of same size as the original image, but corrupted. If someone can point out what I am doing wrong, that will be very helpful. Thanks ins advance.

EDIT: The python server works with python client, so I think its problem with the Java code. Link to python client and server : http://www.bogotobogo.com/python/python_network_programming_server_client_file_transfer.php

Following is my code :

Python server:

import socket                   # Import socket module
port = 2200                   # Reserve a port for your service.
s = socket.socket()             # Create a socket object
host = socket.gethostname()     # Get local machine name
s.bind((host, port))            # Bind to the port
s.listen(5)                     # Now wait for client connection.
print 'Server listening....'
while True:
    conn, addr = s.accept()     # Establish connection with client.
    print 'Got connection from', addr
    data = conn.recv(1024)
    print('Server received', repr(data))
    filename='imagefile.jpg'
    f = open(filename,'rb')
    l = f.read(1024)
    while (l):
        conn.send(l)
        print('Sent ',repr(l))
        l = f.read(1024)
    f.close()
    print('Done sending')
    conn.send('Thank you for connecting')
    conn.close()

Java client:

    String usr2ConnectDefault = "127.0.0.1";
    InetAddress  ip = InetAddress.getLocalHost();
    int port2ConnectDefault = 2200;
    Socket socket;
    BufferedReader in;
    PrintWriter out;
    System.out.print(ip.getHostAddress());
    socket = new Socket(ip.getHostAddress(), port2ConnectDefault);

    System.out.println("Connected to server...sending echo string");        
    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    out = new PrintWriter(socket.getOutputStream(), true);  

    out.println("Begin transfer:");
    int sizeOfFile =  32336;          

    byte[] fileData = new byte[sizeOfFile];
    for (int i = 0; i < sizeOfFile; i++) 
    {
       byte bite = (byte) in.read();
       fileData[i] = bite;
       System.out.println(fileData[i]);
    }
    // save file to disk
    System.out.println(fileData);
    FileOutputStream fos = null;
    fos = new FileOutputStream("fileImage.png");    
    fos.write(fileData); 
    fos.close();

    out.close();
    // verify if request is OK
    String readString = in.readLine();
    in.close();

    socket.close();
5
  • On the Python server, the f.close() call should be outside the while (l) loop. Is this a typo in posting here or an error in the code? Commented Oct 28, 2017 at 8:02
  • 1
    Made the changes. Sorry for incorrect indentation. Commented Oct 28, 2017 at 16:09
  • 1
    on the server don't read only 1024 read os.path.getsize(filename) then receive the same size on the client Commented Oct 28, 2017 at 17:00
  • @SPSP How do I read everything on java side ? I have to read byte one by one right ? Sorry I am not very familiar with reading through sockets in java. Commented Oct 29, 2017 at 1:15
  • @SPSP THank you sooo much!! It worked. I used BufferedImage to read the entire file and save it. Thanks you! Commented Oct 29, 2017 at 1:40

2 Answers 2

1

In your code:

l = f.read(1024)
while (l):
    conn.send(l)
    print('Sent ',repr(l))
    l = f.read(1024)
    f.close()  # here

The file has been closed too early. The first 1024 bytes of image will be sent. You should reduce one indent of that line:

l = f.read(1024)
while (l):
    conn.send(l)
    print('Sent ',repr(l))
    l = f.read(1024)
f.close()

And, I think your last three lines should add one indentation, to accept coming clients. So, the while statment should be this:

while True:
    conn, addr = s.accept()     # Establish connection with client.
    print 'Got connection from', addr
    data = conn.recv(1024)
    print('Server received', repr(data))
    filename='imagefile.jpg'
    f = open(filename,'rb')
    l = f.read(1024)
    while (l):
        conn.send(l)
        print('Sent ',repr(l))
        l = f.read(1024)
    f.close()
    print('Done sending')
    conn.send('Thank you for connecting')
    conn.close()

In your Java client, you have hard coded the image size:

int sizeOfFile =  32336;          

byte[] fileData = new byte[sizeOfFile];

and write the whole array to file:

fos = new FileOutputStream("fileImage.png");    
fos.write(fileData); 

That's why server send 1024B but client creates the same size image as the original.

Sign up to request clarification or add additional context in comments.

4 Comments

I have made the change in the that you suggested in the python code. It still creates a corrupted image.
Also, I am printing bytes as I received them in java code. Its printing numbers but at the end its printing bunch of -1 at the end.
@Student I'm sorry to fail to solve your problem. I think you should first determine where the problem is - server or client? So try to send/receive the file by another program, like nc.
Its ok. Thanks for trying. I think the problem is in Java client since I tried the python server with a python client. and it transferred the file right. The client and server code both are here: bogotobogo.com/python/…
0

The solution was to read the entire file from python server and then send it to java client and to read it as BufferedImage on Java side and save it with ImageIO.write.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.