Java realizes a simple tcpsocket chat room function sharing _java

Source: Internet
Author: User
Tags readline sin socket

This example for you to share the Java implementation of Tcpsocket chat room function of the relevant code for your reference, the specific content as follows

1.tcpserver.java

Import java.net.*;
Import java.io.*;
Import java.util.*;
Import java.util.concurrent.*;
  public class tcpserver{private static final int serverport = 8888;
  Private ServerSocket MyServer = null;
  Private list<socket> Clients = new arraylist<socket> ();
  Private Executorservice Mexecutorservice;
      Public TCPServer () {try{MyServer = new ServerSocket (serverport); 
      Mexecutorservice = Executors.newcachedthreadpool ();
      System.out.println ("Start:");
      Socket mysocket = null;
      while (true) {mysocket = Myserver.accept ();
      Clients.add (Mysocket); 
      Mexecutorservice.execute (New Threadserver (Mysocket)); 
        }}catch (Exception e) {e.printstacktrace ();
      System.exit (0);
    } class Threadserver implements runnable{private Socket msocket=null;
    Private BufferedReader in= null;
    Private printwriter out = null; 
    Private String mstrmsg = null;     Public threadserver (socket socket) {try{ 
      This.msocket=socket;
      in = new BufferedReader (New InputStreamReader (Msocket.getinputstream (), "GB2312")); 
      mstrmsg = "User:" + msocket.getinetaddress () + "Come total:" + clients.size ();
      Sendmassage ();
        }catch (IOException e) {System.out.println ("Erorr");
      System.exit (0);
        }} private void Sendmassage () {try{System.out.println (mstrmsg); for (Socket mysocket:clients) {out = new PrintWriter (New OutputStreamWriter (Mysocket.getoutputstream (), GB
        2312 "), true);
        Out.println (MSTRMSG);
        }}catch (IOException e) {System.out.println ("Erorr");
      System.exit (0); } public void Run () {try{while (mstrmsg = In.readline ())!=null) {if (Mstrmsg.trim (). Equa
            LS ("exit")) {Clients.remove (msocket);
            In.close ();
            Out.close (); mstrmsg = "User:" + msocket.getinetaddress () + "Exit Tatal:" + clients.size();
            ;
            Msocket.close ();           
            Sendmassage (); 
          Break 
            } else{mstrmsg = msocket.getinetaddress () + ":" + mstrmsg;
          Sendmassage ();
        }}catch (IOException e) {System.out.println ("Erorr");
      System.exit (0);
  }} public static void Main (string[] args) {new TCPServer ();
 }
}

2.tcpclient.java

Import java.net.*;
Import java.io.*;
Import java.util.concurrent.*;
  public class TcpClient {private static final int PORT = 8888;
  Private Socket Client = null;
  private BufferedReader sin = null;
  Private Executorservice Mexecutorservice;
    Public TcpClient () {try{Client = new Socket ("120.27.126.174", PORT);
    Sin = new BufferedReader (New InputStreamReader (Client.getinputstream (), "GB2312"));
    Mexecutorservice = Executors.newcachedthreadpool (); 
    Mexecutorservice.execute (New Threadclient (Client));
   String msg = NULL; 
      while (msg = Sin.readline ())!= null) {System.out.println (msg); 
        }}catch (IOException e) {System.out.println (E.getmessage ());
    } class Threadclient extends thread{private Socket msocket = null;
    Private String msg = null;
    BufferedReader in = null;
      PrintWriter out = null;
      Public threadclient (socket socket) {this.msocket = socket;
public void Run () {        try{in = new BufferedReader (new InputStreamReader (system.in));
          out = new PrintWriter (New OutputStreamWriter (Msocket.getoutputstream (), "GB2312"), true);
          while (true) {msg = In.readline ();
            OUT.PRINTLN (msg);
              if (Msg.trim (). Equals ("Exit")) {in.close ();
               Out.close ();        
            Mexecutorservice.shutdownnow ();
            Break
          }}catch (IOException e) {System.out.println ("you");
        System.exit (0);
  }} public static void Main (string[] args) {new TcpClient ();
 }
}

The above is the Java implementation of Tcpsocket chat room function code, I hope to help you learn.

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.