Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the java Socket multithreaded server source code?

Shulou Source: shulou.com Published: 2022-06-02 12:40:21 10月01日 Update

Today, I will talk to you about the source code of java Socket multithreaded server, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Java Socket multithreading requires us to keep learning, and when we learn, we will find that there are many problems in the configuration of the server. Let's take a look at how the server source code is written so that you can make better use of java Socket multithreading.

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.util.ArrayList

Import java.util.List

Import java.util.concurrent.ExecutorService

Import java.util.concurrent.Executors

Public class ChatServer {

Private static final int PORT = 1234; / / Port number

Private static List list = new ArrayList ()

/ / Save the connection object

Private ExecutorService exec

Private ServerSocket server

Public static void main (String [] args) {

New ChatServer ()

}

Public ChatServer () {

Try {

Server = new ServerSocket (PORT)

Exec = Executors.newCachedThreadPool ()

System.out.println ("Server started!")

Socket client = null

While (true) {

Client = server.accept (); / / receive customer connections

List.add (client)

Exec.execute (new ChatTask (client))

}

} catch (IOException e) {

E.printStackTrace ()

}

}

Static class ChatTask implements Runnable {

Private Socket socket

Private BufferedReader br

Private PrintWriter pw

Private String msg

Public ChatTask (Socket socket) throws IOException {

This.socket = socket

Br = new BufferedReader (new InputStreamReader (socket)

.getInputStream ())

Msg = "[" + this.socket.getInetAddress () + "]

Enter the chat room! The current chat room has ["

+ list.size () + "] person"

SendMessage ()

}

Public void run () {

Try {

While ((msg = br.readLine ())! = null) {

If (msg.trim () .equals ("bye")) {

List.remove (socket)

Br.close ()

Pw.close ()

Msg = "[" + socket.getInetAddress () + "]

Get out of the chat room! The current chat room has ["

+ list.size () + "] person"

Socket.close ()

SendMessage ()

Break

} else {

Msg = "[" + socket.getInetAddress () + "] says:" + msg

SendMessage ()

}

}

} catch (IOException e) {

E.printStackTrace ()

}

}

/ * *

* send mass messages to everyone in the chat room

, /

Private void sendMessage () throws IOException {

System.out.println (msg)

For (Socket client: list) {

Pw = new PrintWriter (client.getOutputStream (), true)

Pw.println (msg)

}

}

}

}

Import java.io.BufferedReader

Import java.io.InputStreamReader

Import java.io.PrintWriter

Import java.net.Socket

Import java.util.concurrent.ExecutorService

Import java.util.concurrent.Executors

Public class ChatClient {

Private static final int PORT = 7891

Private static ExecutorService exec =

Executors.newCachedThreadPool ()

Public static void main (String [] args) throws Exception {

New ChatClient ()

}

Public ChatClient () {

Try {

Socket socket = new Socket ("221.130.165.41", PORT)

Exec.execute (new Sender (socket))

System.out.println ("[" + socket.getInetAddress () + "]

Hello, welcome to the simple chat room!

BufferedReader br = new BufferedReader (new

InputStreamReader (socket.getInputStream ())

String msg

While ((msg = br.readLine ())! = null) {

System.out.println ("receive-- >" + msg)

}

} catch (Exception e) {

}

}

After reading the above, do you have any further understanding of the java Socket multithreaded server source code? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: Chat rooms servers services threads source code content learning hello constantly slogans customers objects everyone time more messages knowledge simplicity articles industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi OPPO Reno Shulou Tech Info Microsoft MySQL