package montecarlo;
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.Scanner;
/**
*
* @author hafiz
*/
public class PICalcDistributedMaster {
ObjectOutputStream ostream;
ObjectInputStream istream;
Socket s;
String numThrows;
public void go(){
Scanner input = new Scanner(System.in);
System.out.println("Please enter number of throws: ");
numThrows = input.next();
int num = Integer.parseInt(numThrows);
try{
ServerSocket sock = new ServerSocket(100);
s = new Socket("127.0.0.1",100);
System.out.println("Waiting for connection");
System.out.println("Connection received from " + s.getInetAddress());
PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
pw.println("Sending Number");
pw.println(num);
ostream = new ObjectOutputStream(s.getOutputStream());
ostream.flush();
istream = new ObjectInputStream(s.getInputStream());
System.out.println("IO streams found");
istream.read(); //reads the input stream
}
catch (IOException ie){
ie.printStackTrace();
}
}
public static void main(String [] args){
PICalcDistributedMaster pim = new PICalcDistributedMaster();
pim.go();
}
}
i have adjusted the code to what you told me.I am still getting an error after running it more than once and i think it has to do with the garbage collector problem.My error is
java.net.SocketException: Unrecognized Windows Sockets error: 0: JVM_Bind
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:365)
at java.net.ServerSocket.bind(ServerSocket.java:319)
at java.net.ServerSocket.<init>(ServerSocket.java:185)
at java.net.ServerSocket.<init>(ServerSocket.java:97)
at montecarlo.PICalcDistributedMaster.go(PICalcDistributedMaster.java:31)
at montecarlo.PICalcDistributedMaster.main(PICalcDistributedMaster.java:56)
I assume the problem is with the socket it is binding to.I have tried different kinds but i cant still proceed
I’d like to suggest programming in smaller chunks. You’ve got a lot of code here and I don’t think most of it ever runs:
This code creates a server socket, binds it to a port.
Then you create a new socket
sto connect to the server socket. (Which isn’t yet listening.)You destroy your new socket
swith thesock.accept()result — when you lose the last reference, the socket is free for garbage collection, and you only ever had one reference to it —s.The
sock.accept()call probably ought to block until a new connection arrives. If it doesn’t block, that means you triggered an exception even before all this code.Incidentally, there’s another instance of overwriting content nearly immediately after creating it:
You’ll never see
connection successfulfrom your program because you’ve overwritten the only reference you have to the string.Probably the most egregious error in the entire program — the one that is keeping you from making any real forward progress — is that you throw away all the exception information:
The
catch(Exception e) { /* print message */ }means that you don’t get any diagnostic information about what errors actually happened in your program. (Since you never use the parameter ofgo(), you should remove it completely and the needlessnullhere, as well.)One of these catch-all
catchstatements might be useful once you’re confident that your product catches everything more specific, is nearly bullet-proof, and your customers demand an always-on reliable product. But it has no place in development — you need to be alerted to faults in your programs with as much detail as possible so you can find and fix all your bugs.Remove this. Get rid of your
process()method completely — it is only harmful.