2011-11-15 95 views
-1

我有一个任务是为蒙特卡洛执行一个分布式解决方案,其中主服务器必须执行套接字连接通信职责并向一个或多个工作人员发送一个数字,能够接收它可以获得的点数总结其收到的任何其他计数。我stuck.it只是一直等待连接这是我的代码java中的蒙特卡罗模拟

import java.io.IOException; 
    import java.io.ObjectInputStream; 
    import java.io.ObjectOutputStream; 
    import java.io.PrintWriter; 
    import java.net.ServerSocket; 
    import java.net.Socket; 
    import java.util.Random; 
    import java.util.Scanner; 

public class PICalcDistributedMaster { 

    PrintWriter pWriter; 

    // x coordinate of circle centre also equal to x coordinate of square centre 

    private double xPoint; 

    // y coordinate of circle centre also equal to y coordinate of square centre 

    private double yPoint; 

    // Half the width of the square, also equal to circle radius. 

    private double radius; 

public PICalcDistributedMaster(double radius) { 
      this.xPoint = radius; 
      this.yPoint = radius; 
      this.radius = radius; 
    } 

public void setRadius(double radius) { 

     this.radius = radius; 

    } 

public double getRadius() { 

     return radius; 
    } 

    /** 
    * 
    * @param x The x coordinate of a point,p 
    * @param y The y coordinate of a point,p 
    * @return true if p lies within a circle inscribed within the square, 
    * such that circle diameter = square diameter. 
    * To do this, this method finds the distance between the point 
    * and the centre of the circle and if the distance is greater than 
    * 
    */ 

    public boolean isInside(double x , double y){ 

    double dx = this.xPoint-x; 
    double dy = this.yPoint-y; 
    double dist = Math.sqrt(dx*dx+dy*dy); 

    return dist <= this.radius; 

    }//end method. 

    /** 
    * Assumes a square whose centre is at point r,r 
    * where r = square's radius. and its edges are at 0,0, 2r,0, 0,2r and 2r,2r. 
    */ 
    public void go(){ 

     calculate(); 
     send(); 
     connect(); 
    } 
    public void send(){ 

      try{         
       Socket s = new Socket(); 
       ServerSocket ss = new ServerSocket(1234, 50); 
       System.out.println("Waiting for connection"); 

       s = ss.accept(); // accepts network connection 
       PrintWriter pWriter = new PrintWriter(s.getOutputStream()); 
       pWriter.println("Sending number to Slave Machine"); 
       System.out.println("Waiting for connection"); 
       System.out.println("Connection received from "+s.getInetAddress());  

       ObjectOutputStream ostream = new ObjectOutputStream(s.getOutputStream()); 
       ostream.flush(); 

       ObjectInputStream istream = new ObjectInputStream(s.getInputStream()); 
       System.out.println("IO streams found"); 
       istream.read(); //reads the input stream 

       String message = "connection successful"; 
       ostream.writeObject(message); 
       ostream.flush(); 
       System.out.println(message);     
       } 

      catch (IOException ioe){ 

      ioe.printStackTrace(); 
      }   
      } 

     public void connect(){ 

     String message = "connection successful"; 
     } 

     void calculate(){ 

     PICalcDistributedMaster pim = new PICalcDistributedMaster(10); 
     Scanner input = new Scanner(System.in); 
     System.out.print("Please enter number of throws: "); 

     double numThrows = input.nextDouble(); 
     double PI = pim.computePI(numThrows); 
     double difference = PI - Math.PI; 

      System.out.println(" THE CALCULATED VALUE OF PI FOR "+numThrows+"THROWS="+PI); 
      System.out.println(" THE ACTUAL VALUE OF PI = "+Math.PI); 
      System.out.println(" THE DIFFERENCE IN DEVIATION FROM CALCULATED VALUE TO ACTUAL VALUE IS = "+difference); 

    } 

    public double computePI (double numThrows){ 

      Random randomGen = new Random(); 
      double hits = 0; 
      double PI = 0;    

      double width = radius * 2; 
      //radius * 2 is the width of the square so we are stretching the random number generated 
      //to apply across width, since by default it only generates between 0 and 1. 
     for (int i = 0; i <= numThrows; i++){ 

        double xPos = (randomGen.nextDouble()) * width; 
     double yPos = (randomGen.nextDouble()) * width; 
      if (isInside(xPos, yPos)){ 
      hits++;   
         } 

         PI = (4 * (hits/numThrows));      
        } 

        System.out.println("Throws = "+numThrows); 
        System.out.println("Hits = "+hits); 


      return PI; 
     } 


     public static void main(String [] args){ 

      PICalcDistributedMaster pim = new PICalcDistributedMaster(10); 
      pim.go(); 

     } 

}

回答

0

s = ss.accept();阻止。你正在等待连接,直到有人会连接到你并发送一些东西。据我了解这是经理。因此,实现使用套接字连接到Manager并请求任务的Worker。

相关问题