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.

Page 2 of 2 FirstFirst 12
Results 26 to 37 of 37

Thread: Can't Get Network I/O To Write And Read Properly...

  1. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Can't Get Network I/O To Write And Read Properly...

    import java.io.*;
    import java.net.*;

    public class SimpleClient {
    Socket socket;
    String fileName;
    int fileBytes;

    public void go() {
    try {
    //Start a new socket to get the info
    socket = new Socket("127.0.0.1",7575);
    InputStreamReader isr = new InputStreamReader(socket.getInputStream());
    BufferedReader reader = new BufferedReader(isr);
    fileName = reader.readLine();
    fileBytes = Integer.parseInt(reader.readLine());
    socket.close();
    //Socket to handle file transfer
    socket = new Socket("127.0.0.1",7575);
    byte[] bytes = new byte[fileBytes];
    InputStream is = socket.getInputStream();

    for(int i=0;i<fileBytes;i++) {
    is.read(bytes);
    }

    //Stores the file in a new directory in your system's default directory
    File dir = new File("/TestApp");
    if(!dir.exists()) {
    dir.mkdir();
    }
    FileOutputStream fos = new FileOutputStream("/TestApp/"+fileName);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    bos.write(bytes,0,bytes.length);
    bos.close();
    socket.close();
    System.out.println("Transfer Complete");
    } catch (UnknownHostException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }


    In RED

  2. #27
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: Can't Get Network I/O To Write And Read Properly...

    Ah ok see I think I changed that later after posting it here. I will re-add and try again.

  3. #28
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: Can't Get Network I/O To Write And Read Properly...

    Whoa... yes ok. It writes a bunch of data and then a seemingly infinite number of "-1" returns. So the stream was writing data even though it had ended... One file actually ran my JVM out of memory. So, in short, my idea to use a FOR loop was awful. Right... well here is an updated version of the original program using a while loop. It's still not working though... it writes a file that is several times larger than the original file.

    [CODE]	protected void sendFile() throws IOException {
    		if((file.length())<63000) {
    			byte[] bytes = new byte[(int)file.length()];
    		    buffin = new BufferedInputStream(new FileInputStream(file));
    		    buffin.read(bytes,0,bytes.length);
    		    out = sock.getOutputStream();
    		    out.write(bytes,0,bytes.length);
    		    out.close();
    		} else {
    			//int read;
    			byte[] bytes = new byte[64000];
    			buffin = new BufferedInputStream(new FileInputStream(file));
    			out = sock.getOutputStream();
    			while(buffin.read()!=-1) {
    				int read = buffin.read(bytes,0,bytes.length);
    				System.out.println(read);
    				out.write(bytes,0,bytes.length);
    			}
    			out.flush();
    		}
    	}
     
    	protected void receiveFile() throws IOException {
    		if(fileBytes<63000) {
    			byte[] bytes = new byte[fileBytes];
    		    in = sock.getInputStream();
    		    System.out.println(in.available());
    		    in.read(bytes,0,fileBytes);
    		    fos = new FileOutputStream("/Datawire/"+fileName);
    		    buffout = new BufferedOutputStream(fos);
    		    buffout.write(bytes,0,bytes.length);
    		    buffout.close();
    		} else {
    			byte[] bytes = new byte[64000];
    			in = sock.getInputStream();
    			fos = new FileOutputStream("/Datawire/"+fileName);
    			buffout = new BufferedOutputStream(fos);
    			while(in.read()!=-1) {
    				in.read(bytes,0,bytes.length);
    				buffout.write(bytes,0,bytes.length);
    			}
    			buffout.close();
    		}
    	}[/CODE]

  4. #29
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Can't Get Network I/O To Write And Read Properly...

    The code you just posted looks a mess. You can fix the simple program you gave me to work. Go back and work on that.

    For the first pass, don't mix the reading and writing. Just read in all of the data.
    Look at using all of the args to the read(byte[], start, length) method.
    Adjust the start and length as you read.

    When you get that logic working, then try merging the writing into the loop with the reading.

    You need to use the number of bytes read in your logic.

    while(in.read()!=-1)
    What happens to the byte that is read here?

  5. #30
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: Can't Get Network I/O To Write And Read Properly...

    Quote Originally Posted by Norm View Post
    while(in.read()!=-1)
    What happens to the byte that is read here?
    It's meant only as a check... oh does that remove a byte? Hmmm. I see. Ok... could you give me an example of adjusting the start variable?

  6. #31
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Can't Get Network I/O To Write And Read Properly...

    Here's a way to work out the logic. Say you are going to read a total of 10 bytes.
    On the first read you use start=0 and length=10
    But you only read 3 bytes into the buffer.
    Now compute what the start and the length should be for the next read. It needs to be past the last
    If the next read reads 4 bytes
    Now where should start and length be for the next read.
    Continue until you have read all 10 bytes.
    You advance start and decrement length as you go.

  7. #32
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: Can't Get Network I/O To Write And Read Properly...

    Like this?

    [CODE]	protected void sendFile() throws IOException {
    		if((file.length())<63000) {
    			byte[] bytes = new byte[(int)file.length()];
    		    buffin = new BufferedInputStream(new FileInputStream(file));
    		    buffin.read(bytes,0,bytes.length);
    		    out = sock.getOutputStream();
    		    out.write(bytes,0,bytes.length);
    		    out.close();
    		} else {
    			byte[] bytes = new byte[(int)file.length()];
    			buffin = new BufferedInputStream(new FileInputStream(file));
    			out = sock.getOutputStream();
    			int bytesRead = buffin.read(bytes,0,bytes.length);
    			int pos = bytesRead;
    			out.write(bytes,0,bytesRead);
    			while(bytesRead !=-1) {
    				bytesRead = buffin.read(bytes,pos,bytes.length - pos);
    				if(bytesRead !=-1) {
    					out.write(bytes,pos,bytes.length-pos);
    					pos += bytesRead;
    				}
    			}
    			out.flush();
    		}
    	}
     
    	protected void receiveFile() throws IOException {
    		if(fileBytes<63000) {
    			byte[] bytes = new byte[fileBytes];
    		    in = sock.getInputStream();
    		    System.out.println(in.available());
    		    in.read(bytes,0,fileBytes);
    		    fos = new FileOutputStream("/Datawire/"+fileName);
    		    buffout = new BufferedOutputStream(fos);
    		    buffout.write(bytes,0,bytes.length);
    		    buffout.close();
    		} else {
    			byte[] bytes = new byte[64000];
    			in = sock.getInputStream();
    			fos = new FileOutputStream("/Datawire/"+fileName);
    			buffout = new BufferedOutputStream(fos);
    			int bytesRead = in.read(bytes,0,bytes.length);
    			int pos = bytesRead;
    			buffout.write(bytes,0,bytesRead);
    			while(bytesRead !=-1) {
    				bytesRead = in.read(bytes,pos,bytes.length - pos);
    				if(bytesRead !=-1) {
    					out.write(bytes,pos,bytes.length-pos);
    					pos += bytesRead;
    				}
    			}
    			buffout.flush();
    		}
    	}[/CODE]

    I know this isn't the TestApp but they run on virtually the same concept. And this one was easier to edit since it was already moving in that direction. I will make the changes to the test app as well though.

  8. #33
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Can't Get Network I/O To Write And Read Properly...

    JFYI, the 64k limit is the default ServerSocket receive buffer size (see ServerSocket.setReceiveBufferSize(..)).

    It pays to read the API docs of the classes involved...

  9. #34
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Can't Get Network I/O To Write And Read Properly...

    No, that's way too complicated. The loop I was talking about is about 12 statements total.
    It doesn't have any max record size test. It adjusts the start pointer and the length as the data is read into the one buffer that the code had set to hold the incoming file.

  10. #35
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: Can't Get Network I/O To Write And Read Properly...

    Quote Originally Posted by dlorde View Post
    JFYI, the 64k limit is the default ServerSocket receive buffer size (see ServerSocket.setReceiveBufferSize(..)).

    It pays to read the API docs of the classes involved...
    Oh trust me I spend A LOT of time on the API. I must've missed that though... thanks for your help!

  11. #36
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: Can't Get Network I/O To Write And Read Properly...

    Quote Originally Posted by Norm View Post
    No, that's way too complicated. The loop I was talking about is about 12 statements total.
    It doesn't have any max record size test. It adjusts the start pointer and the length as the data is read into the one buffer that the code had set to hold the incoming file.
    I got it to work! Here is the final code:
    [CODE]	
               protected void sendFile() throws IOException {
    		if((file.length())<63000) {
    			byte[] bytes = new byte[(int)file.length()];
    		    buffin = new BufferedInputStream(new FileInputStream(file));
    		    buffin.read(bytes,0,bytes.length);
    		    out = sock.getOutputStream();
    		    out.write(bytes,0,bytes.length);
    		    out.close();
    		} else {
    			byte[] bytes = new byte[32000];
    			buffin = new BufferedInputStream(new FileInputStream(file));
    			out = sock.getOutputStream();
    			int bytesRead;
    			while((bytesRead = buffin.read(bytes))>0) {
    				out.write(bytes,0,bytesRead);
    			}
    			out.close();
    		}
    	}
     
    	protected void receiveFile() throws IOException {
    		if(fileBytes<63000) {
    			byte[] bytes = new byte[fileBytes];
    		    in = sock.getInputStream();
    		    System.out.println(in.available());
    		    in.read(bytes,0,fileBytes);
    		    fos = new FileOutputStream("/Datawire/"+fileName);
    		    buffout = new BufferedOutputStream(fos);
    		    buffout.write(bytes,0,bytes.length);
    		    buffout.close();
    		} else {
    			byte[] bytes = new byte[32000];
    			in = sock.getInputStream();
    			fos = new FileOutputStream("/Datawire/"+fileName);
    			buffout = new BufferedOutputStream(fos);
    			int bytesRead;
    			while((bytesRead = in.read(bytes))>0) {
    				buffout.write(bytes,0,bytesRead);
    			}
    			buffout.close();
    		}
    	}
    [/CODE]

    NOTE: I only use the size test for performance reasons. If the file is below the server's limit, there is no point in chunking it. It makes more sense to send it straight through. I'm sure it would work by simply using chunked data all the time, but I figure handling smaller amounts of data directly means I can have larger chunk sizes for larger file sizes.

    It was really only a performance thing I suppose.... correct me if I'm wrong.
    Last edited by bgroenks96; July 7th, 2011 at 10:32 AM.

  12. #37
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Can't Get Network I/O To Write And Read Properly...

    Looks like it should work.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Images (read/write, drawing)
    By helloworld922 in forum File Input/Output Tutorials
    Replies: 1
    Last Post: September 5th, 2011, 09:11 AM
  2. Program to read & write the Employee Records
    By arvindk.vij in forum Java Theory & Questions
    Replies: 1
    Last Post: February 11th, 2011, 01:19 AM
  3. Read (using Odbc)and write using (POI api)
    By amruta in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 9th, 2010, 10:12 AM
  4. How to Write and Read Binary Data
    By neo_2010 in forum File Input/Output Tutorials
    Replies: 3
    Last Post: January 4th, 2010, 02:38 PM