A client sends data to the server. The server forwards the data demo to the connected client and forwards the demo.
Server code:
// The Thread class public class ServerThread implements Runnable responsible for processing the communication between each thread {// defines the SocketSocket s = null processed by the current thread; // The input stream BufferedReader br = null for the Socket processed by this thread; public ServerThread (Socket s) throws IOException {this. s = s; // initialize the input stream br = new BufferedReader (new InputStreamReader (s. getInputStream (), "UTF-8"); // ②} public void run () {try {String content = null; // uses a loop to continuously read data sent from the client from the Socket while (content = read FromClient ())! = Null) {// traverse each Socket in the socketList, // send the read content to each Socket once for (Socket s: MyServer. socketList) {OutputStream OS = s. getOutputStream (); OS. write (content + "\ n "). getBytes ("UTF-8") ;}} catch (IOException e) {e. printStackTrace () ;}// defines the method for reading client data. private String readFromClient () {try {return br. readLine ();} // if an exception is caught, the client corresponding to the Socket has closed catch (IOException e) {// Delete the Socket. MyServer. socketList. remove (s); // ①} return null ;}}
Public class MyServer {// defines the ArrayListpublic static ArrayList <Socket> socketList = new ArrayList <Socket> (); public static void main (String [] args) for saving all sockets) throws IOException {ServerSocket ss = new ServerSocket (30000); while (true) {// This line of code will be blocked and will always wait for others' connection Socket s = ss. accept (); socketList. add (s); // each time the client connects, A ServerThread Thread is started to serve the client new Thread (new ServerThread (s )). start ();}}}
Client code:
Public class ClientThread implements Runnable {private Socket s; // defines the Handler object private Handler handler that sends messages to the UI thread; // defines the Handler object public Handler revHandler that receives messages from the UI thread; // input stream of the Socket processed by this thread BufferedReader br = null; OutputStream OS = null; public ClientThread (Handler handler) {this. handler = handler;} public void run () {try {s = new Socket ("192.168.1.88", 30000); br = new BufferedReader (new InputStreamReader (s. GetInputStream (); OS = s. getOutputStream (); // start a Thread to read the server response data. new Thread () {@ Overridepublic void run () {String content = null; // continuously read contents from the Socket input stream. Try {while (content = br. readLine ())! = Null) {// after reading data from the server, the Message notification program interface displays the data Message msg = new Message (); msg. what = 0x123; msg. obj = content; handler. sendMessage (msg) ;}} catch (IOException e) {e. printStackTrace ();}}}. start (); // initialize LooperLooper for the current thread. prepare (); // create the revHandler object revHandler = new Handler () {@ Overridepublic void handleMessage (Message msg) {// receives user input data in the UI thread if (msg. what = 0x345) {// write the content entered by the user in the text box to the network try {OS. write (msg. obj. toStrin G () + "\ r \ n "). getBytes ("UTF-8");} catch (Exception e) {e. printStackTrace () ;}}}; // start LooperLooper. loop ();} catch (SocketTimeoutException e1) {System. out. println ("network connection timed out !! ");} Catch (Exception e) {e. printStackTrace ();}}}
Public class MultiThreadClient extends Activity {// defines two textbox EditText input; TextView show; // defines a Button send on the interface; Handler handler; // define the ClientThread clientThread of the subthread communicating with the server; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); input = (EditText) findViewById (R. id. input); send = (Button) findViewById (R. id. send); show = (TextView) findViewById (R. id. show); handler = new Handler () // ① {@ Overridepublic void handleMessage (Message msg) {// if the Message comes from the subthread if (msg. what = 0x123) {// append the read content to show in the text box. append ("\ n" + msg. obj. toString () ;}}; clientThread = new ClientThread (handler); // The client starts the ClientThread Thread to create a network connection and read data from the server new Thread (clientThread ). start (); // ① send. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {try {// when the user presses the send button, the data entered by the user is encapsulated into a Message, // HandlerMessage msg = new Message (); msg sent to the subthread. what = 0x345; msg. obj = input. getText (). toString (); clientThread. revHandler. sendMessage (msg); // clear the input text box input. setText ("");} catch (Exception e) {e. printStackTrace ();}}});}}