A simple Java chat room

Source: Internet
Author: User

A group chat room written using a Java socket, you can try to copy the past


Server side:

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 void 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.printstac Ktrace ();}}} 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+ "on-line");} 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+ "already offline");} @Overridepublic void Run () {try {while (true) {if (!flag) break;receive ()}} catch (SocketException e) {Stop ();} catch (IO Exception e) {e.printstacktrace ();} finally {try {if (s! = null) S.close ();} catch (IOException e) {e.printstacktrace ();} }}}}


Client Side

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 ;p rivate bufferedreader BR; Private PrintWriter out;private Boolean flag = true;public static void Main (string[] args) {new Client (). Stratup (); private void Stratup () {BufferedReader sbr = null;try {s = new Socket ("127.0.0.1", 5858); out = new PrintWriter (s.getoutput Stream (), true), br = new BufferedReader (New InputStreamReader (S.getinputstream ())), Out.println ("Lim");//out.println ( "Old Week"); 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.equalsignorecase ("disconnect")) {flag = false; System.out.println ("Click 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 ();}}}}


A simple Java chat room

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.