Solved

hoe to calculate delay

Posted on 2010-09-03
3
231 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
  • 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now