Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the java Socket multithreaded server source code?

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report