KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > phoenixdemo > block > SocketThread


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package phoenixdemo.block;
9
10 import java.io.IOException JavaDoc;
11 import java.net.ServerSocket JavaDoc;
12 import java.net.Socket JavaDoc;
13 import phoenixdemo.server.PDKDemoServerImpl;
14
15 /**
16  * Class SocketThread
17  *
18  *
19  * @author Paul Hammant <a HREF="mailto:Paul_Hammant@yahoo.com">Paul_Hammant@yahoo.com</a>
20  * @version $Revision: 1.3 $
21  */

22 public class SocketThread
23     extends Thread JavaDoc
24 {
25     private PDKDemoServerImpl m_pdkDemoServerImpl;
26     private ServerSocket JavaDoc m_serverSocket;
27
28     protected SocketThread( final PDKDemoServerImpl pdkDemoServerImpl,
29                             final int port )
30     {
31
32         m_pdkDemoServerImpl = pdkDemoServerImpl;
33
34         try
35         {
36             m_serverSocket = new ServerSocket JavaDoc( port );
37         }
38         catch( final IOException JavaDoc ioe )
39         {
40             final String JavaDoc message = "Unable to open listening port. " +
41                 "It is probably already being listened to.";
42             throw new RuntimeException JavaDoc( message );
43         }
44     }
45
46     /**
47      * Method run
48      *
49      *
50      */

51     public void run()
52     {
53
54         while( true )
55         {
56             try
57             {
58                 ConnectionThread ct = new ConnectionThread( m_serverSocket.accept() );
59
60                 ct.start();
61             }
62             catch( IOException JavaDoc ioe )
63             {
64                 System.out.println( "Some problem with getting a socket for the connetion." );
65             }
66         }
67     }
68
69     /**
70      * Class ConnectionThread
71      *
72      * @author Paul Hammant <a HREF="mailto:Paul_Hammant@yahoo.com">Paul_Hammant@yahoo.com</a>
73      * @version $Revision: 1.3 $
74      */

75     class ConnectionThread extends Thread JavaDoc
76     {
77         private Socket JavaDoc m_socket;
78
79         private ConnectionThread( final Socket JavaDoc socket )
80         {
81             m_socket = socket;
82         }
83
84         public void run()
85         {
86             m_pdkDemoServerImpl.processSocket( m_socket );
87         }
88     }
89 }
90
Popular Tags