Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: an't get acknowledgement message from server side when image is received from client

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default an'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 :
    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
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: an't get acknowledgement message from server side when image is received from client

    Quote Originally Posted by java_monster View Post
    The PROBLEM is client never reads/gets the acknowledgement message.
    So the question is, "Why not?"
    Follow the flow and see where things seem to go unexpected.
    Does the server actually connect with the client? Does the image get sent?...somewhere along the path things are not what was expected.

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: an't get acknowledgement message from server side when image is received from client

    Quote Originally Posted by jps View Post
    So the question is, "Why not?"
    Follow the flow and see where things seem to go unexpected.
    Does the server actually connect with the client? Does the image get sent?...somewhere along the path things are not what was expected.
    Firstly that you for the response .
    Yes the server connects and receives the image successfully. But the problem is the Client side never receives server's message " File Received:Message by server ". I cannot figure out where the flow actually gets stuck and the code which receives server's message on the client side never runs.

    Please help . . . . .

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: an't get acknowledgement message from server side when image is received from client

    Add some more printlns to see exactly which line of code is the last one to be executed as expected

  5. #5
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: an't get acknowledgement message from server side when image is received from client

    java_monster,
    Do you know the behavior of loops? I believe yes.
    Do you know the behavior of reading from sockets and reading from files? I believe no.
    In your client code you are having a while loop that reads from a file. As long as the file exists it will always return image and its never null.
    So you are able to sent the image to server. On the server side you again have loop. The loop will not finish until the client socket is closed.
    So you are not going beyond the loops on both the sides.
    Just remove the loops and try.

    Syed.

  6. #6
    Junior Member
    Join Date
    Aug 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: an't get acknowledgement message from server side when image is received from client

    Quote Originally Posted by syedbhai View Post
    java_monster,
    Do you know the behavior of loops? I believe yes.
    Do you know the behavior of reading from sockets and reading from files? I believe no.
    In your client code you are having a while loop that reads from a file. As long as the file exists it will always return image and its never null.
    So you are able to sent the image to server. On the server side you again have loop. The loop will not finish until the client socket is closed.
    So you are not going beyond the loops on both the sides.
    Just remove the loops and try.

    Syed.
    Shah saheb thank you for you comment. I removed the loops and the problem is still not solved.Now the client sends the image but server never receives it . Can you please edit my code and post a working code here ? And will you please suggest me good source/website/ebook to follow.

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: an't get acknowledgement message from server side when image is received from client

    Quote Originally Posted by java_monster View Post
    Can you please edit my code and post a working code here ?
    We discourage people providing working code solutions on this forum, please do not encourage people to do so. Please see this post and this post on getting help
    Quote Originally Posted by java_monster View Post
    I removed the loops and the problem is still not solved
    Have you followed the suggestions in post 2 and 4? What were the results? What questions do you have remaining?

  8. #8
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: an't get acknowledgement message from server side when image is received from client

    Hello.
    You made a mistake.
    Please remove that additional loop in the client as well.
    And remove the inner while loop on the server side.
    Again, don't keep any loop in the client code. And don't keep inner loop in the server code.
    The code shall work.
    let me know.

    Syed.

  9. #9
    Junior Member
    Join Date
    Aug 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: an't get acknowledgement message from server side when image is received from client

    JPS I am sorry I didn't know the rules of the forum.
    JPS and SyedBhai I followed your suggestions now the server never receives the image , although client gets successfully connected. Is it possible ImageIO.write() on client side sends more data than ImageIO.read() on server side can read?

    Below is my edited code according to suggestions and output of programs on both server and client side.
    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);
     
                  image=ImageIO.read(new File("clientImage.jpg"));
                  ImageIO.write(image, "jpg", sock.getOutputStream());
                  System.out.println("Image sent");
                  System.out.println("Receiving server's message");//but it never recives
              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 :
    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");
                BufferedImage image;
     
                   System.out.println("Server is waiting for connection");
                   while(true)
               {
                    Socket sock=serverSocket.accept();
                    System.out.println("Connected to client mechine");
                    image=ImageIO.read(sock.getInputStream());
                    ImageIO.write(image, "jpg",new File("newImage.jpg"));
                    System.out.println("Image saved");
                    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();
           }
        }
     
    }

    Client's output:
    image sent
    Receiving server's message


    Server's output:
    server is running
    Server is waiting for connection
    Connected to client mechine

  10. #10
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: an't get acknowledgement message from server side when image is received from client

    Quote Originally Posted by java_monster View Post
    Is it possible ImageIO.write() on client side sends more data than ImageIO.read() on server side can read?
    Interesting theory. You could send a small picture, a single pixel, and see if that works.

    According to the given output the server gets to here:
    System.out.println("Connected to client mechine");
    image=ImageIO.read(sock.getInputStream());
    ImageIO.write(image, "jpg",new File("newImage.jpg"));
    System.out.println("Image saved");
    ...you could add another println, as there are two lines of code in question, to determine if the upper line executes or not.
    ..same for the client, try to narrow down what happens and what does not.

  11. The Following User Says Thank You to jps For This Useful Post:

    java_monster (August 5th, 2013)

  12. #11
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: an't get acknowledgement message from server side when image is received from client

    Hello.
    Can you try sending some sample string first instead of an image?
    If everything works fine then the problem could be with ImageIO.

    syed.

  13. The Following 2 Users Say Thank You to syedbhai For This Useful Post:

    java_monster (August 5th, 2013), jps (August 4th, 2013)

  14. #12
    Junior Member
    Join Date
    Aug 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: an't get acknowledgement message from server side when image is received from client

    Quote Originally Posted by syedbhai View Post
    Hello.
    Can you try sending some sample string first instead of an image?
    If everything works fine then the problem could be with ImageIO.

    syed.
    Yes it can send and received text without any problem.

    --- Update ---

    Quote Originally Posted by jps View Post
    Interesting theory. You could send a small picture, a single pixel, and see if that works.

    According to the given output the server gets to here:
    System.out.println("Connected to client mechine");
    image=ImageIO.read(sock.getInputStream());
    ImageIO.write(image, "jpg",new File("newImage.jpg"));
    System.out.println("Image saved");
    ...you could add another println, as there are two lines of code in question, to determine if the upper line executes or not.
    ..same for the client, try to narrow down what happens and what does not.
    I tried sending small picture...... still the same problem. I tried your second suggestion , this line seems to block further processing by server side I don't know why
    image=ImageIO.read(sock.getInputStream());

  15. #13
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: an't get acknowledgement message from server side when image is received from client

    Hello.
    Can you just try sending and receiving of an image as a one complete byte array? You can skip ImageIO API.
    If things work okay then you can work on ImageIO API.

    Syed.

  16. #14
    Junior Member
    Join Date
    Aug 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: an't get acknowledgement message from server side when image is received from client

    Hello SyedBhai ! Yes that seems to be only option. Thank you so much for your help. Also I thank JPS for responses.
    Regards !
    Aka Peeruk

Similar Threads

  1. Creating an EOF and Acknowledgement on client side
    By Jess17 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 6th, 2013, 08:03 AM
  2. File transfer from Client to Server side
    By highlander141 in forum Java Networking
    Replies: 1
    Last Post: August 29th, 2012, 08:53 AM
  3. Replies: 15
    Last Post: July 2nd, 2012, 12:45 PM
  4. Replies: 0
    Last Post: May 31st, 2012, 05:35 PM
  5. Method :sending image java server to php client
    By ashi123 in forum Java Theory & Questions
    Replies: 0
    Last Post: July 22nd, 2011, 10:15 AM