Solved

hoe to calculate delay

Posted on 2010-09-03
3
237 Views
Last Modified: 2012-05-10
sir,
    i have done a server to client program.it will trasfer the file length from server to client successfully.now i want to make a delay between server to client.so give me a idea to me and to give me a small delay program to add this.
server program:

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

public class server1 {



  public static void main (String [] args ) throws IOException {
   
    ServerSocket servsock = new ServerSocket(13267);
    while (true) {
      System.out.println("Waiting...");

      Socket sock = servsock.accept();
      System.out.println("Accepted connection : " + sock);

     
      File myFile = new File ("D:man.txt");
      byte [] mybytearray  = new byte [(int)myFile.length()];
     
      FileInputStream fis = new FileInputStream(myFile);
      BufferedInputStream bis = new BufferedInputStream(fis);
       bis.read(mybytearray,0,mybytearray.length);
     
      OutputStream os = sock.getOutputStream();
      System.out.println("Sending...");
       os.write(mybytearray,0,mybytearray.length);
     
      os.flush();
      sock.close();
      }
    }
}
client program:

 

   import java.net.*;
import java.io.*;
 
public class client {



  public static void main (String [] args ) throws IOException {
    int filesize=6022386; // filesize temporary hardcoded

    long start = System.currentTimeMillis();
    int bytesRead;
    int current = 0;
   
    Socket sock = new Socket("127.0.0.1",13267);
    System.out.println("Connecting...");

   
    byte [] mybytearray  = new byte [filesize];
    InputStream is = sock.getInputStream();
    FileOutputStream fos = new FileOutputStream("E:man.txt");
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    bytesRead = is.read(mybytearray,0,mybytearray.length);
    current = bytesRead;

   
    do {
       bytesRead =
          is.read(mybytearray, current, (mybytearray.length-current));
       if(bytesRead >= 0) current += bytesRead;
    } while(bytesRead > -1);

    bos.write(mybytearray, 0 , current);
    bos.flush();
    long end = System.currentTimeMillis();
    System.out.println(end-start);
    bos.close();
    sock.close();
  }
}








0
Comment
Question by:sunda2010
[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
  • 2
3 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 33601946
so you want to add a delay in your processing? how about Thread.sleep(time interval)
0
 

Author Comment

by:sunda2010
ID: 33601953
yes. I m want to add small delay between to server to client and to know the process of thread . sleep(time interval)
0
 
LVL 65

Accepted Solution

by:
rockiroads earned 125 total points
ID: 33601971
What if you added the delay after you write to the output stream on the server?

  os.write(mybytearray,0,mybytearray.length);
  Thread.sleep(2000)  // sleep for 2 seconds  
      os.flush();


time interval is ms so 1000 is 1 second
0

Featured Post

Independent Software Vendors: 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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

617 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