A simple java chat room and a simple java chat room

Source: Internet
Author: User

A simple java chat room and a simple java chat room

Using the group chat room compiled by java Socket, you can copy it and try it on your own


Server:

Package net3; import java. io. bufferedReader; import java. io. IOException; import java. io. inputStreamReader; import java. io. printWriter; import java.net. serverSocket; import java.net. socket; import java.net. socketException; import java. util. arrayList; import java. util. list; public class Server {private List <ServerThread> clients = null; public static void main (String [] args) {new Server (). startUp ();} private voi D startUp () {ServerSocket ss = null; Socket s = null; try {ss = new ServerSocket (5858); clients = new ArrayList <ServerThread> (); while (true) {s = ss. accept (); ServerThread st = new ServerThread (s); new Thread (st ). start () ;}} catch (IOException e) {e. printStackTrace ();} finally {try {if (ss! = Null) ss. close ();} catch (IOException e) {e. printStackTrace () ;}} private class ServerThread implements Runnable {private Socket s = null; private BufferedReader br; private PrintWriter out; private String name; private boolean flag = true; public ServerThread (Socket socket) throws IOException {this. s = socket; br = new BufferedReader (new InputStreamReader (socket. getInputStream (); out = new PrintWriter (Socket. getOutputStream (), true); String str = br. readLine (); name = str + "[" + socket. getInetAddress (). getHostAddress () + ":" + socket. getPort () + "]"; clients. add (this); send (name + "online");} private void send (String msg) {for (ServerThread st: clients) st. out. println (msg);} private void receive () throws IOException {String str = null; while (str = br. readLine ())! = Null) {if (str. equalsIgnoreCase ("quit") {stop (); out. println ("disconnect"); break;} send (name + ":" + str) ;}} private void stop () {clients. remove (this); flag = false; send (name + "offline") ;}@ Overridepublic void run () {try {while (true) {if (! Flag) break; receive () ;}} catch (SocketException e) {stop ();} catch (IOException e) {e. printStackTrace ();} finally {try {if (s! = Null) s. close () ;}catch (IOException e) {e. printStackTrace ();}}}}}


Client

Package net3; import java. io. bufferedReader; import java. io. IOException; import java. io. inputStreamReader; import java. io. printWriter; import java.net. socket; import java.net. unknownHostException; public class Client {private Socket s; private BufferedReader br; private PrintWriter out; private boolean flag = true; public static void main (String [] args) {new Client (). stratUp ();} private void stratUp () {Buffer EdReader sbr = null; try {s = new Socket ("127.0.0.1", 5858); out = new PrintWriter (s. getOutputStream (), true); br = new BufferedReader (new InputStreamReader (s. getInputStream (); out. println ("laolin"); // out. println (""); sbr = new BufferedReader (new InputStreamReader (System. in); new Thread (new ClientThread ()). start (); String str = null; while (flag & (str = sbr. readLine ())! = Null) {if (! Flag) break; out. println (str) ;}} catch (UnknownHostException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} finally {try {if (s! = Null) s. close ();} catch (IOException e) {e. printStackTrace ();} try {if (sbr! = Null) s. close ();} catch (IOException e) {e. printStackTrace () ;}} private void receive () {try {String rs = br. readLine (); if (rs. repeated signorecase ("disconnect") {flag = false; System. out. println ("press enter to exit");} System. out. println (rs);} catch (IOException e) {e. printStackTrace () ;}} private class ClientThread implements Runnable {@ Overridepublic void run () {while (true) {if (! Flag) break; receive ();}}}}


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.