The example of this article for you to share the Java Group chat function, for your reference, the specific content as follows
Java support for TCP protocols:
the--> java.net package defines two classes of serversocket and sockets, respectively, for both the server and client sides of a two-way connection.
--> Client Classes define Clients
Package com.dragon.java.tcpchat;
Import java.io.IOException;
Import Java.net.Socket;
Import java.net.UnknownHostException;
/** *
Client
* *
* @author auser
*/public
class Client {public
static void main (String Args[]) throws Unknownhostexception,
IOException {
Socket client = new socket ("192.168.1.188", 10000);
Threading
New Clientsend (client) for sending information. Start ();
Creates a thread that receives information
new clientreceive (client). Start ();
The output stream and the client cannot be closed because they want to implement the chat feature instead of sending only one message at a time.
//Client.shutdownoutput ();
Client.close ();
}
--> Clientsend class defines the thread that the client sends information to the server
Package com.dragon.java.tcpchat;
Import java.io.IOException;
Import Java.io.PrintStream;
Import Java.net.Socket;
Import Java.util.Scanner;
/**
* The thread that the client sends information to the server *
* @author auser * */Public
class Clientsend extends thread {
Private Scanner Scanner;
private socket socket;
Public clientsend (socket socket) {
this.socket = socket;
}
@Override public
Void Run () {
scanner = new Scanner (system.in);
try {
PrintStream PS = new PrintStream (Socket.getoutputstream ());
String line = "";
Blocking send information while
(line = Scanner.nextline ())!= null) {
ps.println (line);
}
catch (IOException e) {
e.printstacktrace ();}}}
--> clientreceive class defines a thread that clients receive service-side information
Package com.dragon.java.tcpchat;
Import Java.io.BufferedReader;
Import Java.io.InputStreamReader;
Import Java.net.Socket;
/**
* Client receives information thread
*
* @author auser */Public
class Clientreceive extends thread {
private socket socket;
Public clientreceive (socket socket) {
this.socket = socket;
}
@Override public
Void Run () {
try {
BufferedReader br = new BufferedReader InputStreamReader
( Socket.getinputstream ()));
Receive information by line
= "";
while (line = Br.readline ())!= null) {
System.out.println (line);
}
catch (Exception e) {
E.printstacktrace ();}}
--> Server class defines service side
Package com.dragon.java.tcpchat;
Import java.io.IOException;
Import Java.net.ServerSocket;
Import Java.net.Socket;
Import java.util.ArrayList;
Import java.util.List;
/** *
Server
* *
@author auser
* */public
class Server {public
static void Main (string[) args) throws IOException,
interruptedexception {
list<socket> List = new arraylist<> ();
Create server-side sockets
ServerSocket server = new ServerSocket (10000);
while (true) {
///Receive client blocking method
Socket socket = server.accept ();
The process of
synchronized (list) {
list.add (socket) to
be synchronized is designed to be added to or deleted from the collection by multiple threads
; Start a new thread to handle this client's AC
new Handlesocket (socket, list). Start ();
Because you do not know when the client will send the message, so the server must always open and can not be closed.
}
}
The--> Handlesocket class operates on clients connected to the server (up and down, masking, and sending information to each client ...).
Package com.dragon.java.tcpchat;
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.io.PrintStream;
Import java.net.InetAddress;
Import Java.net.Socket;
Import java.util.List; /** * Process each (single) client connected to the server * * @author auser * */public class Handlesocket extends thread {private Socket Soc
Ket
Private list<socket> List; /** * Construction Method * * @param socket * The currently connected client * @param list * Stores the set of connected clients/public Handlesock
ET (socket socket, list<socket> List) {this.socket = socket;
This.list = list; /** * Thread Run method */@Override public void Run () {inetaddress address = socket.getinetaddress ();//Get Connected to service
This client's address String IP = address.gethostaddress (); SYSTEM.OUT.PRINTLN (IP + "online!")
"); if (Ip.equals ("192.168.1.117")) {synchronized (list) {Sendtoall (IP +) has been pulled into the blacklist due to a violation!
");
List.remove (socket);
} return; } try {bufferedreader br = new BufferedReader (New InputStreamReader (Socket.getinputstream (), "GBK"));
String line = "";
while (line = Br.readline ())!= null) {String msg = IP + ': ' + line; SYSTEM.OUT.PRINTLN (msg);
Output to the server side of the console//to the client said, sent to all other client Sendtoall (msg);
The catch (IOException e) {//E.printstacktrace (); SYSTEM.OUT.PRINTLN (IP + "Offline!")
");
Synchronized (list) {list.remove (socket); /** * sends information to all clients to remove the information sent by the current socket * * @param MSG * * * private void Sendtoall (Strin G msg) {synchronized (list) {for (socket s:list) {if (s!= socket) {try {Pr
Intstream PS = new PrintStream (S.getoutputstream ());
Ps.println ();
catch (IOException e) {e.printstacktrace ();
}
}
}
}
}
}
--> Note: Because to achieve client connection to the server, that is, the client must first find the server, so the server to open first, then open the service side ...
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.