Solved

hoe to calculate delay

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.

896 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

14 Experts available now in Live!

Get 1:1 Help Now