Results 1 to 7 of 7
  1. #1
    Beattie282 is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Unhappy Count bytes recived over a socket?

    Hello peeps,

    i want to count the amount of bytes recived by the client so i can compare it againt the amount sent from the server to check the file integerity.

    i can count the bytes going out but not coming in :s

    any ideas? this is the server code all working fine

    Java Code:
    import java.net.*;
    import java.io.*;
    
    public class FileServer {
    	  public static void main (String [] args ) throws IOException {
    		    // create socket
    		    ServerSocket servsock = new ServerSocket(13267);
    		    while (true) {
    		      System.out.println("Waiting...");
    
    		      Socket sock = servsock.accept();
    		      System.out.println("Accepted connection : " + sock);
    
    		      // sendfile
    		      FileInputStream fileInputStream = new FileInputStream("C:/testvideo.avi");
    		      long time = System.currentTimeMillis();
    		      OutputStream out = sock.getOutputStream();
    		      byte [] buffer = new byte[128*1024]; 
    		        int bytesRead = 0;
    		        long totalSent = 0;
    
    		        while ( (bytesRead = fileInputStream.read(buffer)) != -1)
    		        {
    		        	System.out.println("buffer length is: " + buffer.length);
    		            if (bytesRead > 0)
    		            {   
    		                out.write(buffer, 0, bytesRead);
    		                totalSent += bytesRead;
    		                System.out.println("sent " + totalSent);  <---------------------------------- this bit prints out the amount of bytes fine i just need this on the client
    		            }   
    		        }
    
    		        sock.close();
    
    		        System.out.println("Sent " + totalSent + " bytes in "
    		                + (System.currentTimeMillis() - time) + "ms.");
    		      }
    		    }
    }
    And this is the client code

    Java Code:
    import java.net.*;
    import java.io.*;
    
    public class FileClient {
    	public static void main (String [] args ) throws IOException {
    	    int filesize=6022386; // filesize temporary hardcoded
    
    	    long start = System.currentTimeMillis();
    	    int bytesRead;
    	    int current = 0;
    	    // localhost for testing
    	    Socket sock = new Socket("127.0.0.1",13267);
    	    System.out.println("Connecting...");
    
    	    // receive file
    	    byte [] mybytearray  = new byte [filesize];
    	    InputStream is = sock.getInputStream();
    	    
    	    FileOutputStream fos = new FileOutputStream("C:/testexe.avi");
    	    BufferedOutputStream bos = new BufferedOutputStream(fos);
    	    bytesRead = is.read(mybytearray,0,mybytearray.length);
    	    int test = 0;
    	    
    	    // thanks to A. Cádiz for the bug fix
    	    do {
    	       bytesRead = is.read(mybytearray, current, (mybytearray.length-current));
    	       if(bytesRead >= 0) 
    	    	   current += bytesRead;
    	           System.out.println(is.read()); <-------------------------------------- i have been trying all sorts here to make the counter work!
    	    } while(bytesRead > -1);
    
    	    bos.write(mybytearray, 0 , current);
    	    bos.flush();
    	    long end = System.currentTimeMillis();
    	    System.out.println("This operation took : ");
    	    System.out.println(end-start);
    	    System.out.println(" milli seconds");
    	    bos.close();
    	    sock.close();
    	  }
    }
    any help would be great thanks because this is the only way i can think of verifiying the integerity of the file sent.

    Thanks
    Beattie282

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Count bytes recived over a socket?

    Why don't you just print current?
    Also, something doesn't look correct about the reading there.
    You seem to throw away the first buffer.
    In fact you seem to throw away all the buffers before writing out the last one read in.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Beattie282 is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: Count bytes recived over a socket?

    eh? the reading on the client end or server? where do i throw this away?

    thanks
    Last edited by Beattie282; 04-18-2012 at 05:31 PM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Count bytes recived over a socket?

    Client.

    Java Code:
    	    InputStream is = sock.getInputStream();
    ... READ ONCE
    	    bytesRead = is.read(mybytearray,0,mybytearray.length);
    ...
    	    do {
    ... READ AGAIN - which overwrites the contents of mybytearray
    	       bytesRead = is.read(mybytearray, current, (mybytearray.length-current));
    ... this is reread wach time round the loop, though for the life of me I can't figure out why you are subtracting the total read in bytes.
    ... that will clear the array each time (well, the bytes it has read in anyway).
    	       if(bytesRead >= 0) 
    	    	   current += bytesRead;
    	           System.out.println(is.read()); <-------------------------------------- i have been trying all sorts here to make the counter work!
    	    } while(bytesRead > -1);
    .. Now you write out whatever happens to be in the array.
    	    bos.write(mybytearray, 0 , current);
    A normal read/write is -
    create a bytebuffer.
    read into the buffer, noting the number of bytes read.
    write the buffer.
    repeat until bytesRead == -1.
    close streams.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Count bytes recived over a socket?

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Beattie282 is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: Count bytes recived over a socket?

    Thankyou! the reason i dident notice this is because the file was actually sending, i think this was because the file i was using could fit in one buffer.

    thanks mate appreciate the help

    i have the while like this now

    Java Code:
    while ( (bytesRead = in.read(buffer)) != -1){
            	fileOutputStream.write(buffer, 0, bytesRead);
            	totareceived += bytesRead;
            	System.out.println("received " + totalreceived);
            }
    Last edited by Beattie282; 04-18-2012 at 06:22 PM. Reason: typo

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Count bytes recived over a socket?

    You'll need to do your writing in that loop as well, if you plan on keeping the received bytes.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Socket Programming ERROR : Socket not connected
    By vishrut_n_shah in forum Networking
    Replies: 0
    Last Post: 11-04-2011, 10:32 AM
  2. Replies: 1
    Last Post: 07-02-2010, 02:18 PM
  3. Problem reading from socket using read(bytes[])
    By sm123 in forum New To Java
    Replies: 1
    Last Post: 04-21-2010, 07:49 PM
  4. append response to the request from Socket and write to another socket
    By vaibhav_singh_vs@yahoo.co in forum Networking
    Replies: 3
    Last Post: 04-17-2009, 08:02 PM
  5. Losing Packets/Bytes using DataInputStream and socket
    By ajordanneve in forum Networking
    Replies: 0
    Last Post: 03-24-2009, 06:22 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
  •