• Post Reply Bookmark Topic Watch Topic
  • New Topic

Image transfer TCP client/server  RSS feed

 
Mark pet
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a TCP client server, where client sends image to the server. Server receives it and manipulates the image. Then the server sends back that manipulated image to the client. Once the client receives that image, it will send next image to the server to be processed. This process continues till 100 images to be processed. How to implement it in Java thread any suggestions?
 
Tim Moores
Saloon Keeper
Posts: 3893
91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What, exactly, does threading have to do with it? What prevents you from implementing it using a Future or a Callable? Tell us what you have and where you are stuck making progress.
 
Mark pet
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is client side



Server side




Issues: Client sends a file, server could not receive exactly same size of image file. Another issue is that server could not send back the modified file to server.
Flow should be like this a) client sends image b) server receives that image 3) then server starts to modify that image 4) server then send back that modified file to the client 5) client receives that modified file 5) client sends another image fine and procedure continue....Please help.




 
Paul Clapham
Sheriff
Posts: 22509
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a bit confused about why your server and client are both acting like servers and clients. Here's what I think your communications protocol should look like:

SERVER

1. Accept a connection.

2. Receive an image from the client via that connection.

3. Modify the image.

4. Send the modified image to the client via that connection.

5. Disconnect.

CLIENT

1. Connect to the server.

2. Send an image to the server via that connection.

3. Receive the modified image from the server via that connection.

4. Disconnect.

It looks like you have a lot of the relevant file-transfer code already written. What you don't have is a design which manages it, and that's what the protocol I just described is.
 
Paul Clapham
Sheriff
Posts: 22509
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now that I look at your code a bit more, there are several other problems.

First you haven't addressed the problem that the server doesn't know how big the file sent from the client is going to be. Looking at the size of a file on the server isn't going to work -- unless you're guaranteed that these files going back and forth are all going to be exactly the same size. It would be better to have the client send the file size first, before sending the file.

And even if you did know the size of the file, your code doesn't read the data sent from the client correctly. You're assuming that the "read(byte[] data, int offset, int size)" method reads the entire file. That isn't the case -- it might only read part of it. The method returns the actual number of bytes it read, but you're ignoring that. You should write a loop which keeps reading until the whole file is received. Or you might want to look into the java.nio.file.Files methods "copy(InputStream, Path, CopyOption...)" and "copy(Path, OutputStream)" to take care of that bookkeeping for you.
 
Mark pet
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks. File size is always same in this case. I fixed the issues of read(byte[] data) using loop. After sending image to the server (port 1234), client should wait till it starts to receive the modified image from the server. How to manage it. Should client use sleep, e.g., Thread.sleep(1000) or use different socket for sending image and receiving modified image. I am stuck in this step. Please suggest.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!