Java Network programming--------------------based on TCP/IP (join multithreading)
1. Server class:
Package Com.serversocket;import Java.io.ioexception;import Java.net.serversocket;import java.net.Socket;import Java.util.arraylist;import Java.util.collections;import Java.util.list;public class Server_02 {// Define the ArrayList that holds all sockets and wrap them as thread-safe public static list<socket> socketlist = Collections.synchronizedlist (new Arraylist<> ());p ublic static void Main (string[] args) throws IOException {ServerSocket ss = new ServerSocket (30000) while (true) {//This line of code will block and will wait for someone else's link to the socket s = ss.accept (); Socketlist.add (s);// Whenever the client connection succeeds, start a serverthread thread for the client service new thread (new SERVERTHREAD02 (s)). Start ();}}
2. Server Thread class
package com.serversocket;import java.io.bufferedreader;import java.io.ioexception;import java.io.inputstreamreader;import java.io.printstream;import java.net.socket;public class serverthread02 implements runnable {//defines the current thread-handling socketsocket s = null;// The input stream of the Socket processed by the thread bufferedreader br = null;public serverthread02 (socket s) throws ioexception {this.s = s;//initializes the input stream corresponding to the socket Br = new bufferedreader (new inputstreamreader (S.getinputstream ()));} Public void run () {try {string content = null;// The while ((Content = readfromclient ()) != null) of the data sent by the client from the socket is continuously read by the Loop {// Traverse each socket//in the socketlist to send the read to each Socket once for (socket s : server_02.socketlist) { Printstream ps = new printstream (S.getoutputstream ());p s.println (content);}} catch (Ioexception e) {e.printstacktrace ();}} Defines a method for reading client data private string readfromclient () {try { return br.readline ();// If an exception is caught, the client that corresponds to the socket has shut down} catch (ioexception e) {//Delete the socketserver_02. Socketlist.remove (s);} Return null;}}
3. Client class
Package Com.socket;import Java.io.bufferedreader;import Java.io.inputstreamreader;import java.io.PrintStream; Import Java.net.socket;public class Client_02 {public static void main (string[] args) throws Exception {Socket s = new Soc Ket ("127.0.0.1", 30000);//client-initiated CLIENTTHREAD02 continuously reads the data from the server new Thread (new ClientThread02 (s)). Start (); PrintStream PS = new PrintStream (S.getoutputstream ()); String line = null; System.out.println ("Please enter message:");//constantly read the keyboard input bufferedreader br = new BufferedReader (new InputStreamReader (system.in)); while (line = Br.readline ())!=null) {//writes the user's keyboard input to the socket corresponding to the output stream ps.println (line);}}}
4. Client Thread class
Package Com.socket;import Java.io.bufferedreader;import Java.io.ioexception;import java.io.InputStreamReader; Import Java.net.socket;public class ClientThread02 implements Runnable {//The thread is responsible for processing the socketprivate Socket s = null;// The thread handles the input stream for the socket corresponding to BufferedReader br = Null;public ClientThread02 (socket s) throws IOEXCEPTION{THIS.S = S;br = new Buffered Reader (New InputStreamReader (S.getinputstream ()));} @Overridepublic void Run () {try{string content = null;//continuously reads the contents of the socket input stream and prints the output while ((Content=br.readline ())! = NULL) {System.out.println (content);}} catch (IOException e) {e.printstacktrace ();}}}
After writing the above code, start the server class first, and then start the client class (you can start multiple client classes)
This article is from the "@coder" blog, be sure to keep this source http://smallcoder.blog.51cto.com/11941149/1853699
Java Network programming--------------------based on TCP/IP (join multithreading)