Hello Guys !
Firstly please forgive my ignorance. Below is my simple code for client and server. Client simply sends an image to server and server reads the image and saves it. Server sends an acknowledgement message to client when image is saved. The PROBLEM is client never reads/gets the acknowledgement message. Below is my code for client and server.
Any help would be appreciated. Thank you
Client code :
import java.awt.image.BufferedImage; import java.io.BufferedReader; import java.io.File; import java.io.InputStreamReader; import java.net.Socket; import javax.imageio.ImageIO; public class clientImageSender { public static void main(String[] args) { Socket sock=null; BufferedImage image=null; String serverMessage; InputStreamReader isReader; BufferedReader bReader; try{ sock=new Socket("127.0.0.1",4444); while((image=ImageIO.read(new File("clientImage.jpg")))!=null) { ImageIO.write(image, "jpg", sock.getOutputStream()); System.out.println("Image sent"); }//below code never runs isReader=new InputStreamReader(sock.getInputStream());// gets acknoledgement by server when image is received on server's side bReader = new BufferedReader(isReader); serverMessage=bReader.readLine(); System.out.println(serverMessage); System.out.println("This code is running"); }//try block catch(Exception e){ e.printStackTrace(); } } }
Server code :
Java Code:
import java.awt.image.BufferedImage; import java.io.File; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import javax.imageio.ImageIO; public class ServerImageReceiver { public static void main(String[] args) { try { ServerSocket serverSocket=new ServerSocket(4444); System.out.println("server is running"); while(true) { System.out.println("Server is waiting for connection"); try { Socket sock=serverSocket.accept(); System.out.println("Connected"); BufferedImage image; while((image=ImageIO.read(sock.getInputStream()))!=null) { ImageIO.write(image, "jpg",new File("newImage.jpg")); System.out.println("Image saved"); } //sending acknowledgement message to client PrintWriter sendMe=new PrintWriter(sock.getOutputStream()); sendMe.println("File Received:Message by server"); sendMe.flush(); System.out.println("Server side message sent"); } catch(Exception e) { e.printStackTrace(); } } } catch(Exception e) { e.printStackTrace(); } } }