root/as3/OSCemote/branches/saqoosha/server/flosc-0.3.1/TcpClient.java

リビジョン 1048, 3.7 kB (コミッタ: uranodai, コミット時期: 4 年 前)

--

Line 
1 import java.io.*;
2 import java.net.*;
3
4 /**
5  *
6  * TcpClient
7  * <BR><BR>
8  * TCP client for the TcpServer.
9  *
10  * Based on CSClient by Derek Clayton.
11  *
12  * @author  Ben Chun        ben@benchun.net
13  * @version 1.0
14  */
15
16 public class TcpClient extends Thread {
17     private Thread thrThis;         // client thread
18     private Socket socket;          // socket for connection
19     private TcpServer server;      // server to which the client is connected
20     private String ip;              // the ip of this client
21     protected BufferedReader in;    // captures incoming messages
22     protected PrintWriter out;      // sends outgoing messages
23
24     /**
25      * Constructor for the TcpClient.  Initializes the TcpClient properties.
26      * @param   server    The server to which this client is connected.
27      * @param   socket    The socket through which this client has connected.
28     */
29     public TcpClient(TcpServer server, Socket socket) {
30         this.server = server;
31         this.socket = socket;
32         this.ip = socket.getInetAddress().getHostAddress();
33
34         // --- init the reader and writer
35         try {
36             in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
37             out = new PrintWriter(socket.getOutputStream(), true);
38         } catch(IOException ioe) {
39             Debug.writeActivity("Client IP: " + ip + " could not be "
40             + "initialized and has been disconnected.");
41             killClient();
42         }
43     }
44
45     /**
46      * Thread run method.  Monitors incoming messages.
47     */ 
48     public void run() {
49         try {
50             char charBuffer[] = new char[1];
51
52             // --- while we have an incoming stream
53             while(in.read(charBuffer,0,1) != -1) {
54
55                 // --- create a string buffer to hold the incoming stream
56                 StringBuffer stringBuffer = new StringBuffer(8192);
57            
58                 // --- while the stream hasn't ended
59                 while(charBuffer[0] != '\0') {
60                     // --- add the character to our buffer
61                     stringBuffer.append(charBuffer[0]);
62                     in.read(charBuffer, 0 ,1);
63                 }
64                
65                 // --- incoming messages should be XML-encoded flash
66                 server.handleOsc(stringBuffer.toString());
67                 }
68
69         } catch(IOException ioe) {
70             Debug.writeActivity("Client IP: " + ip + " caused a read error "
71             + ioe + " : " + ioe.getMessage() + "and has been disconnected.");
72         } finally {
73             killClient();
74         }
75     }
76
77     /**
78      * Gets the ip of this client.
79      * @return   ip    this client's ip
80     */
81     public String getIP() {
82         return ip;
83     }
84    
85     /**
86      * Sends a message to this client. Called by the server's broadcast method.
87      * @param   message    The message to send.
88     */
89     public void send(String message) {
90         // --- put the message into the buffer
91         out.print(message);
92        
93         // --- flush the buffer and check for errors
94         // --- if error then kill this client
95         if(out.checkError()) {
96             Debug.writeActivity("Client IP: " + ip + " caused a write error "
97             + "and has been disconnected.");
98             killClient();
99         }
100     }
101  
102     /**
103      * Kills this client.
104     */   
105     private void killClient() {
106         // --- tell the server to remove the client from the client list   
107         server.removeClient(this);
108
109         // --- close open connections and references
110         try {
111             in.close();
112             out.close();
113             socket.close();           
114             thrThis = null;
115         } catch (IOException ioe) {
116             Debug.writeActivity("Client IP: " + ip + " caused an error "
117             + "while disconnecting.");
118         }       
119     }
120 }
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。