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 7669485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:36:18+00:00 2026-05-31T15:36:18+00:00

Exactly as the title describes, I’ve got a homebrewed SocketImplFactory that is causing my

  • 0

Exactly as the title describes, I’ve got a homebrewed SocketImplFactory that is causing my code to break on

(ServerSocket).close();

I’m at my wits end. I’m now posting all the relevant files, as well as two runs.

First, server2.java:

import java.net.Socket;
import java.net.ServerSocket;

public class server2 {
  public static void main(String[] argv){

    if(argv.length!= 1){
      System.err.println("usage: server1 <hostport>");
      System.exit(1);
    }

    try{
      TCPStart.start();

      ServerSocket sock = new ServerSocket(Integer.parseInt(argv[0]));

      sock.close();
      /*Socket connSock = sock.accept();

      System.out.println("got socket "+connSock);

      Thread.sleep(1*1000);
      connSock.close();
      Thread.sleep(35*1000);            */
    }
    catch(Exception e){
      System.err.println("Caught exception "+e);
      e.printStackTrace();
    }
  }
}

Next TCPStart.java:

import java.net.*;


//---------------------------------------------------
//
// class TCPStart
//
// this is the hub of the entire socket implementation.
// all modules are initialized here.
//
//
// code that runs on TOP of this whole implementation will
// be put in this file, as separate threads.
//
// to start our implementation of TCP, type
//   java TCPStart <UDP port #>
//
//
//---------------------------------------------------
class TCPStart {

  public final static String PORTRESOURCE = "UDPPORT";
  public final static String LOSSRATERESOURCE = "LOSSRATE";

  static public void start() {

    // check command line args
    if (System.getProperty(PORTRESOURCE)==null) {
      System.err.println("Must set "+PORTRESOURCE+" for UDP port to use with "+
             "-D"+PORTRESOURCE+"=<num>");
      System.exit(1);
    }        


    // this number will initialize what port # you want your UDP
    // wrapper to run on.
    int portForUDP = Integer.parseInt(System.getProperty(PORTRESOURCE));


    // initialize TCPWrapper's port number for UDP wrapping
    TCPWrapper.setUDPPortNumber( portForUDP );


    // initialize more TCPWrapper stuff here, if you want to test packet
    // dropping, or if you want to change the sending-rate limit


    // create an instance of the Demultiplexer
    Demultiplexer D = new Demultiplexer( portForUDP );

    // create an instance of OUR SocketImplFactory
    StudentSocketImplFactory myFactory = new StudentSocketImplFactory(D);


    // tell all Socket objects of this program to use OUR
    // implementation of SockImpl
    try {
      Socket.setSocketImplFactory( myFactory );
      ServerSocket.setSocketFactory( myFactory ); //This is the problem line.
    } catch (Exception e) {
      System.out.println(e);
      System.exit(1);
    }


    // start the demultiplexer
    D.start();

    if (System.getProperty(LOSSRATERESOURCE)!=null) {
      TCPWrapper.dropRandomPackets
    (System.currentTimeMillis(),
     Double.parseDouble(System.getProperty(LOSSRATERESOURCE)));
    }        


  }
}

And, Finally, StudentSockImplFactory.java

import java.net.*;

//---------------------------------------------------
//
// class StudentSocketImplFactory
//
// this object is what actually creates each INSTANCE of a
// SocketImpl object.  In TCPStart.main(), we call
//
//     Socket.setSocketImplFactory( new StudentSocketImplFactory(D) );
//
// (this is a static function)
// so, when we create a java Socket, it will make a call to
// createSocketImpl(), and the Socket will use OUR code!!!
//
//---------------------------------------------------
class StudentSocketImplFactory implements SocketImplFactory {

    // the Demultiplexer has to be known to every SocketImpl, so that it
    // can communicate with it
    private Demultiplexer D;


    public StudentSocketImplFactory(Demultiplexer D) {
        super();
        this.D = D;
    }

    // Socket object makes this call to get one instance of SocketImpl.
    // reminder: each socket will get a DIFFERENT instance of
    // SocketImpl. this is GOOD, so that we will have one TCPConnection
    // for each Socket!!
    public SocketImpl createSocketImpl() {
        return ( new StudentSocketImpl(D) );
    }
}

If I comment out the aforementioned trouble line, it all works well, until I try to actually accept connections. A failed Run:

$ java -DUDPPORT=12345 server2 54321
java.lang.NullPointerException: null buffer || null address

Thanks in advance for all the help.

  • 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-31T15:36:19+00:00Added an answer on May 31, 2026 at 3:36 pm

    I found out my own answer, which probably won’t make sense to many people, but I’m going to post it so that if Anyone else has a similar Problem, they’ll be able to find this answer on the Google box:

    Turns out,

    (ServerSocket).close();
    

    Calls

    (SocketImpl).close();
    

    Which I did not previously realize. Since my factory was being reset to use a different SocketImpl (namely, the one I’m writing), when I tried to send a Fin Packet, it understandably didn’t have anyone to send it to, so it gave a null address error. Just goes to show, sometimes you have to sleep on a problem, and the answer will come in the morning.

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

Sidebar

Related Questions

Exactly like my title states i need a query that removes the first chracter
As the title says, I have exactly the same code needs to run in
exactly as title says, I need to put php inside of the javascript that
Sorry for the weird title, but well, that's exactly what I need. I know
Using MS Access. I'm sorry that the title is vague--i just don't exactly know
Im sure if the title is exactly what I am trying to describe so
Exactly what the title says...........any thoughts on other good options for relational database implementation
...basically exactly what the title says. NetworkStream.Length is not implemented. What's an alternative? I'm
The question title is a bit strange because I'm not exactly sure how to
My quetion is simple and its exactly the same as in the Title. How

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.