java.io.IOException Import; Import a java.io.InputStream; Import a java.io.OutputStream; Import the java.net.Socket; Import java.util.Scanner; public class Client { the Socket Socket; Private Final static int message_size = 1024; // maximum length allowed per data receiving public static void main (String [] args) { new new Client (); } public Client () { the try { Socket new new = the Socket ( "127.0.0.1", 45678); IF ( socket.isConnected () == to true) { System.out.println ( "connection successful"); new new the thread () {// open a thread to accept data @Override public void RUN () { super.run (); InputStream in; try { in = socket.getInputStream(); byte[] b; while (true) { b = new byte[MESSAGE_SIZE]; in.read(b); System.out.println("接收到服务端消息:" + new String(b)); } } catch (IOException e) { e.printStackTrace(); } } }.start(); } OutputStream out = null; while (true) { Scanner scanner = new Scanner(System.in); String str = scanner.nextLine(); out = socket.getOutputStream(); out.write(str.getBytes()); out.flush(); if (str.equals("end")) { System.exit(0);//关闭客户端 } } } catch (IOException e) { e.printStackTrace(); } } }
java.io.IOException Import; Import a java.io.InputStream; Import the java.net.ServerSocket; Import the java.net.Socket; Import of java.util.ArrayList; Import java.util.List; public class Server { List <ReceiveThread> receiveList = new ArrayList <> () ; // store connected client class private final static int mESSAGE_SIZE = 1024; // maximum length of each accepted data allows int num = 0; // client ID public static void main ( String [] args) { new new Server (); } // end service processing logic Server () { the serverSocket serverSocket = null; the try { serverSocket the serverSocket new new = (45678); // for listening socket port number designated while (true) { Socket socket = serverSocket.accept (); // listen for client connections, blocked thread System.out.println ( "client connection:" + NUM); // receiving a message from a client process in the other thread ReceiveThread receiveThread = new receiveThread (socket, NUM); receiveThread.start (); receiveList.add (receiveThread); // there is a new on-line client, the server notifies other clients String notice = "new client on the line, now online clients are: client: "; for (ReceiveThread Thread: receiveList) { Notice Notice = +" "+ thread.num; } for (ReceiveThread Thread: receiveList) { new new SendThread (thread.socket, Notice) .start (); } ++ NUM; } } the catch (IOException E) { e.printStackTrace (); } } // accept message thread (also corresponding to the recording action of the client socket) class ReceiveThread the extends the Thread { int NUM; the Socket socket; // client corresponding socket boolean continueReceive = true; // identifying whether further need to maintain the connection receiving public ReceiveThread (the socket socket, int NUM) { this.socket = socket; this.num NUM =; the try { // the client is connected to the sends, client ID assignment notification . socket.getOutputStream () write (( "your client ID is" NUM +) .getBytes ()); } the catch (IOException E) { e.printStackTrace (); } } @Override public void RUN () { super.run (); // receiving the client message sent by the InputStream inputStream = null; the try { inputStream Socket.getInputStream = (); byte [] B; the while (continueReceive) { B = new new byte [message_size]; InputStream.read (B); B = splitByte (B); // remove unnecessary portions array // transmitting end client disconnects if (new String (b) .equals ( "End")) { continueReceive = to false; receiveList.remove (the this); // notify other clients + NUM + "Disconnect \ n" String message = "client" + "are now online, the client:"; for (ReceiveThread receiveThread: receiveList) { + = Message Message "" + receiveThread.num; } System.out.println (Message); for (receiveThread receiveThread: receiveList) { new new SendThread (receiveThread.socket, Message) .start (); } } {the else the try { String [] data = new String ( b) .split ( "", 2); // with the first space, the character string divided into two parts int clientNum = Integer.parseInt (data [0 ]); // convert digital, i.e. the client ID number // sends a message to the specified client for (ReceiveThread receiveThread: receiveList) { IF (receiveThread.num == clientNum) { new new SendThread (receiveThread.socket, "client" + num + " message: "Data + [. 1]) Start ();. System.out.println (" client "+ num +" sends a message to the client "+ receiveThread.num +": "+ data [1]); } } } catch (Exception e) { new SendThread (socket, "input error, please re-enter") .start (); System.out.println ( "Client input error"); } } } } the catch (IOException E) { e.printStackTrace (); } {the finally the try {// Close the resource IF (inputStream = null!) { inputStream.close (); } IF (Socket = null!) { the Socket.close (); } } the catch (IOException E) { e.printStackTrace () ; } } } } // Send message thread class the extends the Thread {SendThread the Socket Socket; String STR; public SendThread (the Socket Socket, String STR) { this.socket = Socket; this.str STR =; } @Override public void RUN ( ) { super.run (); the try { Socket.getOutputStream () Write (str.getBytes (.)); } the catch (IOException E) { e.printStackTrace (); } } } // remove excess portions of the byte array private byte[] splitByte(byte b[]) { int i = 0; for (; i < b.length; i++) { if (b[i] == 0) { break; } } byte[] b2 = new byte[i]; for (int j = 0; j < i; j++) { b2[j] = b[j]; } return b2; } }