Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6839757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:45:29+00:00 2026-05-26T23:45:29+00:00

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;

  • 0
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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:45:29+00:00Added an answer on May 26, 2026 at 11:45 pm

    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:

               ServerSocket sock = new ServerSocket(5000);
               s = new Socket("127.0.0.1",5000);
               s = sock.accept();
    

    This code creates a server socket, binds it to a port.

    Then you create a new socket s to connect to the server socket. (Which isn’t yet listening.)

    You destroy your new socket s with the sock.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:

              String message = "connection successful";
              message = (String) istream.readObject();
    

    You’ll never see connection successful from 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:

     try{
           go(null);
        }
     catch(Exception e){
         System.err.print("Connection terminated");
      }
    

    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 of go(), you should remove it completely and the needless null here, as well.)

    One of these catch-all catch statements 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

package net.learn2develop.PopularAttractions; import java.io.IOException; import java.util.List; import java.util.Locale; import com.google.android.maps.GeoPoint; import com.google.android.maps.MapActivity; import com.google.android.maps.MapController;
package com.n; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable;
package com.crumbin.tabs; //java package import java.io.IOException; import java.util.ArrayList; import org.json.JSONException; import android.app.Activity; import android.app.AlertDialog;
package com.rest; import java.io.IOException; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button;
package com.login.android; import java.net.InetSocketAddress; import java.net.Socket; import java.nio.ByteBuffer; import java.nio.channels.SocketChannel; import com.login.android.R; import android.app.Activity;
package mp1similar; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.StringTokenizer;
package mp1similar; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.StringTokenizer;
package myProg; import java.util.Scanner; public class oving4a { /** *Øving 4a. Aleksander Pettersen */
package com.crumbin.tabs; import java.util.ArrayList; import java.util.HashMap; import org.apache.http.client.HttpClient; import android.content.Context; import android.database.Cursor; import android.location.Location;
package com.test.methods; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.