Results 1 to 8 of 8
  1. #1
    andrearro88's Avatar
    andrearro88 is offline Member
    Join Date
    Nov 2011
    Location
    Milan, Italy
    Posts
    4
    Rep Power
    0

    Default Simple client/server application with FileOutputStream issue

    Hello,
    I wrote a simple client/server application where client send to server an image file (.jpg), and server reply to client sending a string.
    I got an issue while receiving the file in the server application (using DataInputStream and FileOutputStream): the image is succesfully written on the filesystem but apparently the writing cycle (while) never ends!

    Here is the client code:
    Java Code:
    package wallace.clientplate;
    
    import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    public class ClientPlate {
    
    	/**
    	 * @param args
    	 * @throws IOException 
    	 * @throws UnknownHostException 
    	 */
    	public static void main(String[] args) throws UnknownHostException, IOException{
    		
    	//create file
    		File file = new File("img/targa1.jpg");
    		FileInputStream fin = new FileInputStream(file);
    	    
    	//connect to server
    	//open output and input streams
    	    System.out.println("Connecting to server...");
    		Socket socket = new Socket("localhost", 6789);
    		DataOutputStream out = new DataOutputStream(socket.getOutputStream());
    		BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));		
    		
    	//send image to server
    		System.out.println("Sending image...");
    		byte[] readData = new byte[1024];
    		int i;
    		while((i = fin.read(readData)) != -1)
    			out.write(readData, 0, i);
    		System.out.println("Image sent");
    		fin.close();
    		
    	//receive reply from server
    		System.out.println("Waiting for server reply...");
    		String plate = in.readLine();
    		System.out.println("FROM SERVER: " + plate);
    		in.close();
    		out.close();
    		socket.close();
    	}
    }
    and the server code:
    Java Code:
    package wallace.serverplate;
    
    import java.awt.image.BufferedImage;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    import javax.imageio.ImageIO;
    
    import com.asprise.util.ocr.OCR;
    
    
    public class ServerPlate {
    	
    	/**
    	 * @param args
    	 * @throws IOException 
    	 */	
    	public static void main(String[] args) throws IOException{
    		
    	    //create socket
    		ServerSocket welcomeSocket = new ServerSocket(6789);
    		
    		while(true){
    		//open connection with client
    		//create input and output streams
    	       	System.out.println("Waiting for client connection...");
    	        Socket socket = welcomeSocket.accept();
    	        System.out.println("Connected");
    	        System.out.println("Creating input and output streams...");
    	        DataInputStream in = new DataInputStream(socket.getInputStream());
    	        DataOutputStream out = new DataOutputStream(socket.getOutputStream());
    	        
    	    //create file
    	        System.out.println("Creating file output stream...");
    	        File file = new File("img/image.jpg");
    	        FileOutputStream fout = new FileOutputStream(file);
    	        
    	    //receive and save image from client
    	        byte[] readData = new byte[1024];
    	        int i;
    	        while((i = in.read(readData)) != -1){
    	        	System.out.println("writing..." + i);
    	        	fout.write(readData, 0, i);
    	        }
    	        System.out.println("flush");
    		    fout.flush();
    		    System.out.println("File stored on file system");
    		    fout.close();
    		    
    		//close input stream
    		    in.close();
    		    
    		//get string from image
    		    String plate = getPlate();
    		    
    		//send string to client
    			out.writeChars(plate);
    			
    		//close connection
    			out.close();
    		    socket.close();
    	    }
    	 }
    }
    Here is the client Console output:

    Connecting to server...
    Sending image...
    Image sent
    Waiting for server reply...


    and the server Console output:

    Waiting for client connection...
    Connected
    Creating input and output streams...
    Creating file output stream...
    writing...1024
    writing...1024
    writing...1024
    writing...1024
    writing...1024
    writing...1024
    writing...1024
    writing...188


    Really interesting thing: if I stop the client execution, the server get out from the cycle and (obviously) ends with an exception:
    Server Console output stopping the client:

    Waiting for client connection...
    Connected
    Creating input and output streams...
    Creating file output stream...
    writing...1024
    writing...1024
    writing...1024
    writing...1024
    writing...1024
    writing...1024
    writing...1024
    writing...188
    flush
    File stored on file system
    Exception in thread "main" java.net.SocketException: Socket closed
    at java.net.SocketOutputStream.socketWrite(SocketOutp utStream.java:116)
    at java.net.SocketOutputStream.write(SocketOutputStre am.java:132)
    at java.io.DataOutputStream.writeChars(DataOutputStre am.java:297)
    at wallace.serverplate.ServerPlate.main(ServerPlate.j ava:62)

    I was wondering why it happens and how to fix the issue.
    Thanks.

    p.s:I'm sorry for my bad English! :)

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,255
    Rep Power
    25

    Default Re: Simple client/server application with FileOutputStream issue

    You have done a very nice job of putting together your question. One of the best I've seen in a long time.
    I'll take your code and look at it. Back soon.

  3. #3
    Grkn is offline Member
    Join Date
    Oct 2011
    Posts
    65
    Rep Power
    0

    Default Re: Simple client/server application with FileOutputStream issue

    while((i = in.read(readData)) != -1){
    System.out.println("writing..." + i);
    fout.write(readData, 0, i);
    }

    I didn't read all of your question but I looked your code

    i = in.read(readData);
    if you put this in while loop then it won't quit

    reason:
    i is the length of data in inputstream buffer



    length=read(readData) // it uses default loop and derived from read() method

    ex: length=read(readData) //You used this code
    int val;
    int length;
    while(val=read()!=-1){
    //Som necessary code to store a byte data
    length++;
    }
    return length;

    So length will not be -1 and it will waits in read(readData) method

    I explained some solutions in here
    How do I recognize, when HTTP response ends?
    Last edited by Grkn; 11-23-2011 at 06:34 PM.

  4. #4
    andrearro88's Avatar
    andrearro88 is offline Member
    Join Date
    Nov 2011
    Location
    Milan, Italy
    Posts
    4
    Rep Power
    0

    Default Re: Simple client/server application with FileOutputStream issue

    Ok, I'm gonna take a look to your solution.
    But now, if I got where the issue is, I wonder why the client application get out from the cycle.

    This is the Client While code:
    Java Code:
    while((i = fin.read(readData)) != -1)
                out.write(readData, 0, i);

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,255
    Rep Power
    25

    Default Re: Simple client/server application with FileOutputStream issue

    I wonder why the client application get out from the cycle
    Can you explain what you mean here?

  6. #6
    andrearro88's Avatar
    andrearro88 is offline Member
    Join Date
    Nov 2011
    Location
    Milan, Italy
    Posts
    4
    Rep Power
    0

    Default Re: Simple client/server application with FileOutputStream issue

    Can you explain what you mean here?
    I mean...
    i = in.read(readData);
    if you put this in while loop then it won't quit
    This is what Grkn wrote about my problem, and, as I can see, i = in.read(readData) is in the client application cycle too.
    Is that cause of the difference between DataInputStream and FileInputStream?

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,255
    Rep Power
    25

    Default Re: Simple client/server application with FileOutputStream issue

    When is the value returned by read() = to -1?
    Is is at EOF?
    When is EOF on the stream that you are reading from? Can there be more than one EOF on a stream?
    If there can only be one, and you want to continue using that stream for more data, then you need to have a way to tell the receiver of the file how much data you are sending.
    This means creating a mini protocol or agreement between the sender and the receiver so the receiver knows what is happening. One way to do that is to send an int with the length of the file that will be send next. The receiver reads the int and then reads that number of bytes from the stream.

    Here is the debug print out from when I execute your program:
    Running: F:\Java\jre6\bin\java.exe -classpath D:\JavaDevelopment;.;..\. -Xmx128M ClientServer_Problem

    C Connecting to server...
    S Waiting for client connection...
    S Connected
    S Creating input and output streams...
    S Creating file output stream...
    C Sending image...
    S fSize=1929
    C sent i=1024
    S writing...1024 av=0
    C sent i=905
    C Image sent
    S writing...905 av=0
    C Waiting for server reply...
    S flush
    S File stored on file system, socket=false
    S writing to out, socket=false
    C FROM SERVER: From S to C<
    S sockect closed
    S Waiting for client connection...

    0 error(s)

  8. #8
    andrearro88's Avatar
    andrearro88 is offline Member
    Join Date
    Nov 2011
    Location
    Milan, Italy
    Posts
    4
    Rep Power
    0

    Default Re: Simple client/server application with FileOutputStream issue

    Quote Originally Posted by Norm View Post
    When is the value returned by read() = to -1?
    Is is at EOF?
    When is EOF on the stream that you are reading from? Can there be more than one EOF on a stream?
    If there can only be one, and you want to continue using that stream for more data, then you need to have a way to tell the receiver of the file how much data you are sending.
    This means creating a mini protocol or agreement between the sender and the receiver so the receiver knows what is happening. One way to do that is to send an int with the length of the file that will be send next. The receiver reads the int and then reads that number of bytes from the stream.
    Thank you very much Norm, I followed your advice and now it works perfectly.
    I don't know if my code is the smartest one, but it works!!!

    Client code:
    Java Code:
    package wallace.clientplate;
    
    import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    public class ClientPlate {
    
    	/**
    	 * @param args
    	 * @throws IOException 
    	 * @throws UnknownHostException 
    	 */
    	public static void main(String[] args) throws UnknownHostException, IOException{
    		
    	//create file
    		File file = new File("img/targa3.jpg");
    		FileInputStream fin = new FileInputStream(file);
    	    
    	//connect to server
    	//open output and input streams
    	    System.out.println("Connecting to server...");
    		Socket socket = new Socket("localhost", 6789);
    		DataOutputStream out = new DataOutputStream(socket.getOutputStream());
    		BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));		
    		
    	//send image to server
    		byte[] readData = new byte[1024];
    		int l = (int)file.length();
    		System.out.println("Sending image (" + l + " B)...");
    		out.writeInt(l); //send file length to server
    		int i;
    		while((i = fin.read(readData)) != -1){
    			out.write(readData, 0, i);
    			System.out.println(i + "B sent");
    		}
    		System.out.println("Image sent");
    		fin.close();
    		
    	//receive reply from server
    		System.out.println("Waiting for server reply...");
    		String plate = in.readLine();
    		System.out.println("FROM SERVER: " + plate);
    		
    	//close connection to server
    		in.close();
    		out.close();
    		socket.close();
    	}
    }
    Output:

    Connecting to server...
    Sending image (8542 B)...
    1024B sent
    1024B sent
    1024B sent
    1024B sent
    1024B sent
    1024B sent
    1024B sent
    1024B sent
    350B sent
    Image sent
    Waiting for server reply...
    FROM SERVER: CE544SX


    Server Code:
    Java Code:
    package wallace.serverplate;
    
    import java.awt.image.BufferedImage;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    import javax.imageio.ImageIO;
    
    import com.asprise.util.ocr.OCR;
    
    
    public class ServerPlate {
    	
    	/**
    	 * @param args
    	 * @throws IOException 
    	 */	
    	public static void main(String[] args) throws IOException{
    		
    	    //create socket
    		ServerSocket welcomeSocket = new ServerSocket(6789);
    		
    		while(true){
    		//open connection with client
    		//create input and output streams
    	       	System.out.println("Waiting for client connection...");
    	        Socket socket = welcomeSocket.accept();
    	        System.out.println("Connected");
    	        System.out.println("Creating input and output streams...");
    	        DataInputStream in = new DataInputStream(socket.getInputStream());
    	        DataOutputStream out = new DataOutputStream(socket.getOutputStream());
    	        
    	    //create file
    
    	        System.out.println("Creating file output stream...");
    	        File file = new File("img/image.jpg");
    	        FileOutputStream fout = new FileOutputStream(file);
    	        
    	    //receive and save image from client
    	        int l = in.readInt(); //read length of file from server
    	        System.out.println("Length: " + l + " B");
    	        byte[] readData = new byte[1024];
    	        int i = 0;
    	        while(l != 0){
    	        	i = in.read(readData); //receive file from server
    	        	l = l - i;
                    System.out.println("writing..." + i + ", to EOF: " + l);
                    fout.write(readData, 0, i); //write the file on the file system
                }
    		    fout.flush();
    		    System.out.println("File stored on file system");
    		    fout.close();
    		    
    		//get string from image
    		    String p = getPlate();
    		    System.out.println("PLATE: " + p);
    		    
    		//send string to client
    			out.writeBytes(p);
    			System.out.println("Plate number sent to the client\n");
    			
    		//close connection
    			in.close();
    			out.close();
    		    socket.close();
    	    }
    	 }
    }
    Output:

    Waiting for client connection...
    Connected
    Creating input and output streams...
    Creating file output stream...
    Length: 8542 B
    writing...1024, to EOF: 7518
    writing...1024, to EOF: 6494
    writing...1024, to EOF: 5470
    writing...1024, to EOF: 4446
    writing...1024, to EOF: 3422
    writing...1024, to EOF: 2398
    writing...1024, to EOF: 1374
    writing...1024, to EOF: 350
    writing...350, to EOF: 0
    File stored on file system
    PLATE: CE544SX
    Plate number sent to the client

    Waiting for client connection...

Similar Threads

  1. problem with a simple client server app
    By Moorag in forum New To Java
    Replies: 0
    Last Post: 04-11-2011, 05:29 PM
  2. simple FTP server and FTP client
    By simontkk2005 in forum Networking
    Replies: 2
    Last Post: 11-17-2010, 10:38 AM
  3. Simple server-client
    By DC200 in forum New To Java
    Replies: 6
    Last Post: 12-09-2009, 05:13 PM
  4. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 09:18 AM
  5. Simple example Client Server Application
    By ferosh in forum Networking
    Replies: 1
    Last Post: 04-01-2007, 10:36 AM

Tags for this Thread

Posting Permissions

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