Results 1 to 2 of 2
  1. #1
    Baloch is offline GuRu
    Join Date
    Feb 2012
    Location
    Quetta
    Posts
    3
    Rep Power
    0

    Default Can't get acknowledgement message from server side when image is received from client

    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 :
    Java 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();
           }
        }
        
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Can't get acknowledgement message from server side when image is received from cl

    You never close the output stream on the client so the server is, probably, still trying to read from its input stream.

Similar Threads

  1. Client-side or server-side XML parsing?
    By noodle_variation_187-X in forum JavaServer Pages (JSP) and JSTL
    Replies: 11
    Last Post: 05-31-2012, 09:06 AM
  2. Calculations in Server side with client`s data
    By thitami in forum Threads and Synchronization
    Replies: 8
    Last Post: 11-19-2011, 03:49 PM
  3. NIO Server: listening on client's side socket
    By carek in forum Networking
    Replies: 1
    Last Post: 12-24-2009, 07:30 AM
  4. How to open a file located in server from client side
    By Malathi in forum Web Frameworks
    Replies: 4
    Last Post: 04-20-2009, 09:10 PM
  5. Replies: 2
    Last Post: 10-05-2008, 06:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •