Solved

how to send data in chunks from server and client in java

Posted on 2011-03-07
9
1,305 Views
Last Modified: 2013-11-23
if the file i request is too large, the client need to wait very long. what does to be modify from my code in order to receive the data in chunks.
e.g. transmit 100MB into 10MB each chunk in order for me to process
int len=0;
        byte [] mybytearray  = new byte [buffer];
        InputStream is = connection.getInputStream();
        FileOutputStream fos = new FileOutputStream("default.amc");
        BufferedOutputStream bos = new BufferedOutputStream(fos);

 while((len=is.read(mybytearray)) > 0)
        {
        	bos.write(mybytearray,0,len);
        	current+=len;
        	
        }
        
         bos.flush();
         long end = System.currentTimeMillis();
         System.out.println(end-start);
         System.out.println(current);
         bos.close();

Open in new window

0
Comment
Question by:jackson_duffy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 92

Expert Comment

by:objects
ID: 35054550
To support sending data in chunks would also require modifying the client to support receive the data in chunks
Changes to the server would be to support specifying the start byte and the number of bytes to send.


>  while((len=is.read(mybytearray)) > 0)

btw, that should be:

 while((len=is.read(mybytearray)) >= 0)
0
 

Author Comment

by:jackson_duffy
ID: 35054693
my server side codes are shown below.
with my current codes. i only able to see the received file at the client folder when the file is received fully
Please advise any changes needed. thanks
byte[] bytes = new byte[65536];
                File file = new File("test.txt");
                FileInputStream bis = new FileInputStream(file);
                BufferedInputStream in = new BufferedInputStream(bis);
                OutputStream out = connection.getOutputStream();
                
                DataOutputStream dout = new DataOutputStream(out);
                dout.writeUTF(message);
                //now send the length (as a simple long)
                long numToSend = file.length();
                dout.writeLong(numToSend);
                //now send the file contents
                long numSent = 0;
                while(numSent < numToSend) {
                   long numThisTime = numToSend - numSent;
                   numThisTime = numThisTime < bytes.length ? numThisTime : bytes.length;
                   int numRead = in.read(bytes, 0, (int) numThisTime);
                   if(numRead ==-1 ) break;
                   dout.write(bytes,0,numRead);
                   numSent += numRead;
                }
                out.flush();
                out.close();
                connection.close();

Open in new window

0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:jackson_duffy
ID: 35065313
any amends to my codes is needed??
0
 
LVL 92

Expert Comment

by:objects
ID: 35065693
not sure what you need.
0
 

Author Comment

by:jackson_duffy
ID: 35065726
i am sorry.
what i am trying to ask is how to i send and receive file in chunks using a server and client architecture. and what do i have to take note of especially for the server side which is send data and the client side which is to receive data.
as you mentioned, for the send data portion, do the server have to send data in chunks too in order for the client to receive the data in chunks? or what should be done?
0
 
LVL 92

Expert Comment

by:objects
ID: 35065874
first step would be to design the protocol used by the client and server to communicate. Not much point writing any code until you decide how the two are going to communicate.
0
 

Author Comment

by:jackson_duffy
ID: 35066122
the protocol is udp
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 35066377
I meant the application protocol that the client and server will use when communicating.

heres an example od a simple udp client server setup.
http://systembash.com/content/a-simple-java-udp-server-and-udp-client/
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question