Avatar of sunda2010
sunda2010
 asked on

how to assign nodes between server to client

sir,
    i have already run server to client program.now i want to make nodes between server to client.
server->node1->node2->client.so i need how to assign nodes i need a program for assigning nodes.I
have attach my server to client program below.please give me a node program.
server:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author new1
 */import java.net.*;
import java.io.*;

public class serv {




  public static void main (String [] args ) throws IOException, InterruptedException {
    // create socket
    ServerSocket servsock = new ServerSocket(13264);
    while (true) {
      System.out.println("Waiting...");

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

      // sendfile
      File myFile = new File ("D:/raj.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);
      Thread.sleep(2000);  // sleep for 2 seconds
      os.flush();
      sock.close();
      }
    }
}


client:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author new1
 */   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;
    // localhost for testing
    Socket sock = new Socket("127.0.0.1",13264);
    System.out.println("Connecting...");
    System.out.println("Delay 2 seconds:");

    // receive file
    byte [] mybytearray  = new byte [filesize];
    InputStream is = sock.getInputStream();
    FileOutputStream fos = new FileOutputStream("source-copy.pdf");
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    bytesRead = is.read(mybytearray,0,mybytearray.length);
    current = bytesRead;

    // thanks to A. Cádiz for the bug fix
    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();
  }

}
}
Java

Avatar of undefined
Last Comment
gremwell

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
gremwell

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck