?
Solved

Socket connection

Posted on 2003-03-21
2
Medium Priority
?
240 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:tamilselvi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Expert Comment

by:glottis
ID: 8185601
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
 
LVL 30

Accepted Solution

by:
Mayank S earned 90 total points
ID: 8193398
>> 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

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month11 days, 23 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question