1

I am trying to create a multi-threaded client-server communication program that uses 2 threads to connect to multiple clients (but only 2 at a time). The characteristics of the program are:

  1. The clients can terminate the communication program from their side but the server thread does not exit.

  2. The threads in the server do not close ServerSocket until the exit condition is fulfilled by the server program, i.e. the server keeps running continuously connecting to various clients if requested.

  3. Every time a client terminates the program only the communication (related) streams are closed.

Now the problem is the line of code where the Socket object is created. After calling the accept() method of ServerSocket object, a NullPointerException is thrown. Any insight as to where I am going wrong would be very helpful.

My Server side code:

 class Clientconnect implements Runnable
 {
   ServerSocket ss;
   Socket s;
   String n;
   int f;

   Clientconnect() throws Exception
   {
    new ServerSocket(776);
    new Socket();
   }

   public void run()
   {
    n = Thread.currentThread().getName();
    while(true) // thread iteration
    {
        try
        {
            System.out.println("Thread "+Thread.currentThread().getName()+" is ready to accept a 
             connection.....");

            s = ss.accept(); // ------**The NullPointerException occurs here**

            System.out.println("Thread "+Thread.currentThread().getName()+" has accepted a connection:\n----------");

            PrintStream ps = new PrintStream (s.getOutputStream());

            BufferedReader cl = new BufferedReader (new InputStreamReader (s.getInputStream()));

            BufferedReader kb = new BufferedReader (new InputStreamReader (System.in));

            String in, out;

            ps.println("you are connected via thread "+n);

            ps.println("----------------------");

            while (true)
            {                   
                in = cl.readLine();
                if( in.equalsIgnoreCase("system_exit"))
                {
                    break;
                }
                System.out.println("Client : "+in);

                System.out.print("Server "+n+" :");

                out = kb.readLine();

                ps.println(out);
            }
            s.close();
            ps.close();
            cl.close();
            System.out.print("do you want to close the server socket\n1:close\n2:continue\nenter");
            f = Integer.parseInt(kb.readLine());
            if(f == 1)
            {
                ss.close();
                break;
            }
            else
            {
                continue;
            }

        }
        catch (Exception e){e.printStackTrace();}
      }
  }
   }

   class test2g
   {
       public static void main (String args[]) throws Exception
       {
           Clientconnect cc = new Clientconnect();  

           Thread t1 = new Thread (cc, "t1");
           Thread t2 = new Thread (cc, "t2");

           t1.start();
           t2.start();
       }
   }

It is a fairly simple communications program with no complex resource accessing or retrieval. I am running the client end on the same machine so it's "localhost".

My client side code is merely a reciprocation of the try{} block.

P.S. I have tried declaring the ServerSocket & Socket objects as static in the Clientconnect class but it did not help.

1
  • 2
    You never initialize ss, so it's null. Commented Feb 20, 2020 at 17:59

1 Answer 1

1

I believe ss needs to be assigned in the constructor:

   Clientconnect() throws Exception
   {
    ss = new ServerSocket(776);
    new Socket();  // Not needed because it creates a Socket that is immediately thrown away.
   }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.