Reputation: 79
I would like to send 2 Images from Socket server (Java) to client socket (python).
--> My Problem I received ONLY ONE Image (dataImage1) in client.
What Have I Done Wrong?
I send (java) the images successively:
...
BufferedImage image = ImageIO.read (file);
imgstr = encodeToString(image, "png");
outToClient.writeBytes(imgstr);
...
BufferedImage chart = ImageIO.read (fileChart);
imgstrChart = encodeToString(chart, "png");
outToClient.writeBytes(imgstrChart);
...
I receive (python) the images successively:
...
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_addr = (tcpclient.host , tcpclient.port)
client_socket.connect( server_addr )
dataImage1 = client_socket.recv( 1024 )
fp = open ( pathImage1, "wb" )
while dataImage1:
#{
if not dataImage1: break
recvDataImage += dataImage1
dataImage1 = client_socket.recv( 1024 )
#}
fp.write(b64decode( recvDataImage1 ))
fp.flush()
fp.close()
dataImage2 = client_socket.recv( 1024 )
fp = open ( pathImage2, "wb" )
while dataImage2:
#{
if not dataImage2: break
recvDataImage2 += dataImage2
dataImage2 = client_socket.recv( 1024 )
#}
fp.write(b64decode( recvDataImage2 ))
fp.flush()
fp.close()
....
Upvotes: 1
Views: 105
Reputation: 12357
How does the receiver know where image1 ends? In your code, the client keeps calling client_socket.recv
until a null string is returned. This doesn't happen until the socket is closed. So what is happening is that you are transmitting both images and the client is writing the content of both to the first file.
The second loop will never receive any data because, once closed, you will never receive more data from the socket.
You need to either (a) open a new socket to transmit the second file, or (b) send the length of each file as part of your protocol so that the client knows how much to write to pathImage1
before moving on to pathImage2
.
Upvotes: 3