Use java multithreading to implement communication between the server and multiple clients

Source: Internet
Author: User

Use java multithreading to implement communication between the server and multiple clients
Use multithreading to implement communication between servers and multithreading. 1. Create a ServerSocket on the server and call accept () cyclically () wait for the client link 2. The client creates a Socket and requests a link to the server. 3. The server accepts the client request, create socekt to establish a leased line connection with the client 4. Establish a connection socket to talk to the client in a separate thread 5. The Server continues to wait for the new link to the Server. java

Package test. concurrent. socket; import java. io. *; import java.net. inetAddress; import java.net. serverSocket; import java.net. socket;/*** Created by dong on 15-6-22. * TCP-based Socket communication enables users to log on to * Server */public class Server {public static void main (String [] args) {try {// 1. Create a server Socket, that is, ServerSocket. Specify the bound port and listen to this port ServerSocket serverSocket = new ServerSocket (8888); Socket socket Socket = null; // record the number of clients int count = 0; System. out. println ("*** the server is about to start, wait for the client link ***"); // The loop listener waits for the client link while (true) {// calls accept () method to start listening, wait for the client connection socket = serverSocket. accept (); // create a new thread ServerThread serverThread = new ServerThread (socket); // start the thread serverThread. start (); count ++; // count the number of clients. System. out. println ("number of clients:" + count); InetAddress address = socket. getInetAddress (); System. out. println ("current client IP:" + address. getHostAddress () ;}} catch (IOException e) {e. printStackTrace ();}}}
ServerThread. java
Package test. concurrent. socket; import java. io. *; import java.net. socket;/*** Created by dong on 15-6-22. * server-side Thread processing class */public class ServerThread extends Thread {// Socket socket related to this Thread = null; public ServerThread (Socket socket) {this. socket = socket;} // The operation executed by the thread, responding to the client's request public void run () {InputStream is = null; InputStreamReader isr = null; BufferedReader br = null; outputStream OS = null; PrintWriter pw = null; try {// get an input stream and read the client information is = socket. getInputStream (); isr = new InputStreamReader (is); // convert byte streams into bytes; br = new BufferedReader (isr); // Add a buffer String info = null; // read data cyclically while (info = br. readLine ())! = Null) {System. out. println ("I Am a server, the client said:" + info);} socket. shutdownInput (); // close the input stream // obtain the output stream and respond to the client request OS = socket. getOutputStream (); pw = new PrintWriter (OS); // wrap it as a print stream pw. write ("welcome"); pw. flush (); // outputs the cache} catch (IOException e) {e. printStackTrace ();} finally {try {// close the resource if (pw! = Null) pw. close (); if (OS! = Null) OS. close (); if (is! = Null) is. close (); if (isr! = Null) isr. close (); if (br! = Null) br. close (); if (socket! = Null) socket. close () ;}catch (IOException e) {e. printStackTrace ();}}}}
Client. java
Package test. concurrent. socket; import java. io. *; import java.net. socket;/*** Created by dong on 15-6-22. * Client */public class Client {public static void main (String [] args) {try {// 1. Create a Client Socket, specify the server port number and address Socket socket = new Socket ("localhost", 8888); // 2. Obtain the output stream and send the OutputStream OS = socket to the server. getOutputStream (); // byte output stream PrintWriter pw = new PrintWriter (OS); // wrap the output stream as a print stream pw. write ("User name: tom; Password: 4 56 "); pw. flush (); socket. shutdownOutput (); // close the output stream InputStream is = socket. getInputStream (); InputStreamReader isr = new InputStreamReader (is); BufferedReader br = new BufferedReader (isr); String info = null; // read while cyclically (info = br. readLine ())! = Null) {System. out. println ("I Am a client: the server says:" + info);} br. close (); is. close (); isr. close (); pw. close (); OS. close (); socket. close ();} catch (IOException e) {e. printStackTrace ();}}}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.