Reputation: 39
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();
Upvotes: 0
Views: 467
Reputation: 39
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.
Upvotes: 0
Reputation: 147
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.
Upvotes: 1