Multi-User chat implemented in java (console input)

Source: Internet
Author: User

I don't know how to do it when I want to create a multi-person chat room while learning android.

I want to make a simple multi-person chat room with pure java.

Data transmission through Socket

Knowledge points used: Socket, ServerSocket, and Multithreading

 

Server program:

Import java. io. bufferedReader; import java. io. IOException; import java. io. inputStream; import java. io. inputStreamReader; import java. io. outputStream; import java. io. printWriter; import java.net. serverSocket; import java.net. socket; import java.net. URLDecoder; import java.net. unknownHostException; import java. util. arrayList; // This program is a connected program, commonly known as a server program // In the program world, the network refers to the interaction of information between two programs, therefore, the network must contain more than two programs. One end is called a server and the other is called a client. The correspondence with the client is 1-Npublic class SimpleServer {// used to save the public static ArrayList of the currently connected user <Socket> socketList = new ArrayList <Socket> (); public static void main (String [] args) throws Exception {// create a serverSocket to listen to the client Socket connection request ServerSocket serverSocket = new ServerSocket (10002 ); // 10002 the port number for this server: System. out. println ("service start"); // uses a loop to continuously receive requests from the client while (true) {// whenever a request from the client is received, the server also generates a SocketSocket socket = serverSocket. Accept (); System. out. println ("service connection"); // Add the connected client to the socketList in the list. add (socket); // start a new thread // task thread, each connection has a dedicated Task thread // This thread will process the message sending and response new MyThread (socket, socketList ). start ();}/* BufferedReader br = new BufferedReader (new InputStreamReader (System. in); PrintWriter pw = new PrintWriter (socket. getOutputStream (), true); // output the input content to clientwhile (true) {String msg = br. readLine (); pw. println ("server:" + msg) ;} */} Class MyThread extends Thread {Socket client; ArrayList <Socket> clients; BufferedReader br; public MyThread (Socket client, ArrayList <Socket> clients) throws Exception {super (); this. client = client; this. clients = clients; br = new BufferedReader (new InputStreamReader (this. client. getInputStream ();} // send the received message to the public void run () {try {String content = null; while (true) {// read information from a client if (content = br. re AdLine ())! = Null) {for (Socket socket: clients) {if (client! = Socket) {// send the read message to the PrintWriter pw = new PrintWriter (socket. getOutputStream (), true); pw. println (content) ;}} content = URLDecoder. decode (content, "UTF-8"); System. out. println (content) ;}} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}

Client Program:

Import java. io. bufferedReader; import java. io. IOException; import java. io. inputStream; import java. io. inputStreamReader; import java. io. printWriter; import java.net. socket; import java.net. URLDecoder; import java.net. URLEncoder; public class Client {/*** @ param args */public static void main (String [] args) throws Exception {// TODO Auto-generated method stub // create a socketSocket client Connected to the Local Machine with port 10002 = new Socket ("192.168.3.26", 10002); new MyThread (client ). start (); InputStream is = client. getInputStream (); BufferedReader br = new BufferedReader (new InputStreamReader (is); while (true) {String msg = br. readLine (); // decodes the received information. msg = URLDecoder. decode (msg, "UTF-8"); System. out. println (msg) ;}} class MyThread extends Thread {Socket client; public MyThread (Socket client) {super (); this. client = client;} public void run () {// send a message try {BufferedReader br = new BufferedReader (new InputStreamReader (System. in); PrintWriter pw = new PrintWriter (client. getOutputStream (), true); // output the input content to socketwhile (true) {String msg = br. readLine (); // encode the sent message msg = URLEncoder. encode ("Zhang San said:" + msg, "UTF-8"); pw. println (msg) ;}} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}

This is just a design concept, a little simple, and can only send and receive messages from the console

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.