Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

hoe to calculate delay

Posted on 2010-09-03
3
Medium Priority
?
238 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 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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:
This video teaches viewers about errors in exception handling.
Suggested Courses

721 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