TCPConnectionListener.java
Upload User: liulanlin
Upload Date: 2017-12-08
Package Size: 1274k
Code Size: 2k
Category:

VOIP program

Development Platform:

Java

  1. /*
  2.  * TCPConnectionListener.java
  3.  *
  4.  * Created on November 19, 2003, 10:38 AM
  5.  */
  6. package gov.nist.applet.phone.media.protocol.transport;
  7. import java.net.ServerSocket;
  8. import java.net.Socket;
  9. /**
  10.  * This class is a thread waiting for connection on this TCP port and ip address
  11.  * As soon as it gets a connection it notifies the Adapter for RTP.
  12.  * @author  DERUELLE Jean
  13.  */
  14. public class TCPConnectionListener implements Runnable {
  15.     private ServerSocket serverSocket;
  16.     private Socket socket;
  17.     //private TCPReceiveAdapter adapter;
  18.     private Thread listener;
  19.     private boolean ctrl;
  20.     private boolean connected;
  21.     
  22.     /** Creates a new instance of TCPConnectionListener.
  23.      * @param serverSocket - ServerSocket that will be waiting for connection.
  24.      * @param adapter - the TCP Adapter for RTP we have to notify that we get a new connection.
  25.      * @param ctrl - boolean to know if it's RTP connection (false) or a RTCP connection (true).
  26.      */
  27.     public TCPConnectionListener(ServerSocket serverSocket, boolean ctrl) {
  28.         this.serverSocket=serverSocket;
  29.         //this.adapter=adapter;
  30.         this.ctrl=ctrl;
  31.         connected=false;
  32.     }
  33.     
  34.     /**
  35.      * Start the listener thread.
  36.      */
  37.     public void start(){
  38.         if(listener==null){
  39. listener=new Thread(this);
  40. listener.setName("TCPConnectionListener Thread");
  41.         }
  42.             
  43.         listener.start();
  44.     }
  45.     
  46.     /**
  47.      * Task of the listener thread.
  48.      */
  49.     public void run() {
  50.         try{
  51.             socket=serverSocket.accept(); 
  52.             System.out.println("Socket from "+ socket.getInetAddress() +
  53.                                 " connected to the port "+socket.getLocalPort()+
  54.                                 " control : "+ctrl);
  55.             //adapter.setRemoteSocket(socket,ctrl);
  56.             connected=true;
  57.             listener=null;
  58.         }
  59.         catch(java.io.IOException ioe){
  60.             ioe.printStackTrace();
  61.         }
  62.     }
  63.     
  64.     /**
  65.      * Tells if this listener for TCP Connection has received a connection
  66.      * @return the socket when the listener has received a new connection
  67.      */
  68.     public Socket waitForConnections(){
  69.         while(!connected){
  70.             try{
  71.                 Thread.sleep(1);
  72.             }
  73.             catch(InterruptedException ie){
  74.                 ie.printStackTrace();
  75.             }
  76.         }
  77.         return socket;
  78.     }
  79. }