Socket connection

I have to write a program in which the parent thread should listen in one particular port and the child thread should listen in different port.Please let me know whether it is possible or not. if possible, pl give some pointers to implement
tamilselviAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

glottisCommented:
create a main program which handles 2 threads.
the main thread is handling 2 child threads, and thse child threads listne on 2 different ports
0
Mayank SAssociate Director - Product EngineeringCommented:
>> the parent thread should listen in one particular port

Let the main thread listen to a port using ServerSocket.

>> the child thread should listen in different port

Spawn a new thread from the main () thread and let it listen to a different port, again using ServerSocket.

class MyApp
{
  public static void main ( String args[] )
    throws Exception
  {
    new ChildThread () ;
    ServerSocket ss = new ServerSocket ( 1786 ) ; // listens to 1786
    ....
    ....

  } // end of main ()

} // class definition over

class ChildThread extends Thread
{
  ServerSocket ss ;

  ChildThread ()
  {
    start () ;

  } // end of constructor ()

  public void run ()
  {
    try
    {
      ss = new ServerSocket ( 2025 ) ; // listens to 2025
      ..
      ..

    } // end of try block

    catch ( Exception e )
    {
      ..

    } // end of catch block

  } // end of run ()

} // class definition over

The accept () method of ServerSocket returns a Socket object after accepting a connection from a client at the specified port no. And don't forget to put a call to the join () method in main (), in case there are chances of the main thread finishing execution before the child thread.

Mayank.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.