The TestThread.java code
Code:
import java.net .*;
import java.io. *;
class TestThread implements Runnable
{
private thr Thread; / / Will contain the client thread
private Socket soc; / / Receive socket linking the customer
private PrintWriter pw; / / For managing the output stream
private BufferedReader buff; / / For managing the inflow
private TestServer ser; / / To use methods of main class
private int _numClient = 0; / / Will contain the number of customers managed by the thread
/ / ** Constructor: create the necessary dialogue with the customer **
TestThread (Socket s, TestServer tser) / / The parameter s is given in TestServer by ss.accept ()
{
ser = tser; / / Transition from local comprehensive (for management in other methods)
soc = s; / / Passing local global
try
{
/ / Making a variable to use the output stream with string
pw = new PrintWriter (soc.getOutputStream ());
/ / Making a variable for the use of inflows with string
buff = new BufferedReader (new InputStreamReader (soc.getInputStream ()));
/ / Add the output stream from the list and the number of recovery
_numClient = tser.addCli (pw);
}
catch (IOException e) ()
thr = new Thread (this); / / Instantiate the thread
thr.start (); / / Start the thread, the function run () is launched here
}
/ / ** Method: run to start the thread by t.start () **
/ / ** It waits for messages from the server and redirects **
/ / This method must necessarily be implemented because the interface Runnable
Public void run ()
{
String message = ""; / / Declaration of the variable that will receive messages from client
/ / On the console shows the connection of a new customer
System.out.println ("A new client has connected, no"_numClient +);
try
{
/ / Read the incoming data is done character by character ...
/ / ... to find a character string end
char ch [] = new char[1]; / / Declaration of an array of char than 1 element, buff.read () to store the char read
while(buff.read (ch, 0, 1)! =- 1) / / Loop waiting for messages from the client (blocking on buff.read ())
{
/ / Look if we reach the end of a string ...
if (ch [0]! = '\ u0000' & & ch [0]! = '\ n' & & ch [0]! = '\ r')
message + = ch [0]; / / ... if not, it concatenates the character in the message
else if(! message.equalsIgnoreCase ("")) / / Just checking in principle
{
if(ch [0] == '\ u0000') / / The last character is '\ u0000' (null termination char)
/ / We send the message saying it will concatenate '\ u0000' when sending to the client
ser.sendAll (message""+ ch [0]);
else ser.sendAll (message""); / / Otherwise we send the message to all
message = ""; / / We empty the message string for it to be reused
}
}
}
catch (Exception e) ()
finally / / Finally happen most often when the client disconnects
{
try
{
/ / It tells the console client disconnects
System.out.println ("Customer No."+ + _numClient"Disconnected");
ser.delCli (_numClient); / / We remove the client from the list
soc.close (); / / Close socket if it has not already been (because of the exception thrown above)
}
catch (IOException e) ()
}
}
}
The socket created by ss.accept () is received by a parameter, from _s, thus creating the buffer managing the inflow and outflow. The thread is then started (run () method). A waiting loop data takes place. Once a character arrives as input, it is concatenated to the string message. The message is sent a termination character of string is detected. If it is '\ u0000', that will be appended to the message when sending (sendall method ()). This is necessary mainly because of the Flash clients for which the received message must end with the zero terminal. Other types of customers are generally more tolerant. At least TestServer operate independently of the type of customers. Finally, after any event leading to the end of the loop, the block finally () will be executed properly to end the existence of the client thread.
Bookmarks