Solved

hoe to calculate delay

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate jars 4 45
eclipse console opening separately 2 29
ejb entity bean example issue 2 18
Eclipse Java import and method not resolved 4 52
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 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 about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

820 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