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

How to create a multithreaded local area network chat room with Java

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "how to create a multithreaded LAN chat room with Java", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to create a multithreaded LAN chat room with Java" article.

1.TCP

In order to connect to the local area network, we must know the principle of information transmission.

The information transmitted in the local area network is in the form of packets. I use the TCP packet to transmit data, and the TCP packet encapsulates the IP message.

The following sentence is to get the IP address of a server through a static IPV4 protocol class.

Address = InetAddress.getByName ("192.168.43.86") Socket

The IP address of the server is obtained in TCP, but the IP address alone cannot lock the specific application, so the concept of socket is introduced. The port number is used to lock the process of the host. The port number is generally 1024-49151, because 0-1023 is a well-known port number, which is generally used for some commonly used ports such as HTTP, etc., and 49152-65535 is a temporary port number that cannot be randomly occupied. It is generally a temporary assignment during the running of the program.

Socket = IP+ port number

This sentence defines a socket with port number 9998 and IP 192.168.43.86.

Int port = 9998 socket = new Socket (address,port); 3.C/S architecture

From the perspective of the Java class, the server and client abstract a connection through a socket connection. The server realizes the function of reading information by establishing its own socket port and listening for customers to connect on this port. The client transmits data to the server through the port number agreed upon by the server. After the server is turned on, run the client, and establish a TCP connection with the server through a three-way handshake, and then realize the communication between the client and the server on the basis of this connection.

4. Multithreading

Because the server may serve multiple objects at the same time, if the traditional method is used to connect to the server, there will be multiple clients waiting for a client to interact with the server. In order to solve this problem, the multithreading method is adopted.

Use the SuperSocket class to inherit the socket class and implement the Runnable interface to realize multi-thread operation.

Class SuperSocket extends Socket implements RunnableSuperSocket socket_1 = new SuperSocket (9999); SuperSocket socket_2 = new SuperSocket (9998); SuperSocket socket_3 = new SuperSocket (9997); Thread S1 = new Thread (socket_1); Thread S2 = new Thread (socket_2); Thread S3 = new Thread (socket_3); 5. Server

As shown in the third part of the architecture of the server, the code implementation is as follows

Package TCP;import java.io.IOException;import java.io.InputStream;import java.net.ServerSocket;import java.net.Socket;import java.util.Scanner;public class TCPserver2 {public static void main (String [] args) {Scanner scan = new Scanner (System.in); / / instantiate a server-specific socket class with multithreading / / the port number needs to be passed as the initialization variable SuperSocket socket_1 = new SuperSocket (9996) SuperSocket socket_2 = new SuperSocket (9998); SuperSocket socket_3 = new SuperSocket (9997); / / establish three child threads Thread S1 = new Thread (socket_1); Thread S2 = new Thread (socket_2); Thread S3 = new Thread (socket_3) Try {while (true) {/ / Open thread s1.start (); s2.start (); s3.start (); if (scan.next () = = "123") {/ / end thread break } finally {try {/ / close socket socket_1.close (); socket_2.close (); socket_3.close ();} catch (IOException e) {e.printStackTrace () } class SuperSocket extends Socket implements Runnable {InputStream is; byte [] buffer; Socket socket=null; ServerSocket serverSocket=null; public SuperSocket (int port) {try {/ / initial server-type socket serverSocket= new ServerSocket (port); buffer = new byte [1024];} catch (IOException e) {e.printStackTrace () }} @ Override public void run () {try {/ / wait for port connection socket = serverSocket.accept (); / / create input stream is = socket.getInputStream () after connection is complete; / / read client transfer information int len = is.read (buffer) String str = new String (buffer, 0, len); System.out.println (str);} catch (IOException e) {e.printStackTrace ();}

Client

As shown in the third part of the client architecture, the code implementation is as follows

Package TCP;import java.io.IOException;import java.io.OutputStream;import java.net.InetAddress;import java.net.Socket;import java.net.UnknownHostException;import java.util.Scanner;public class TCPclient {public static void main (String [] args) {InetAddress address=null; Socket socket=null; OutputStream os=null; String message=null; Scanner sca=null Try {/ / get the native IP address address = InetAddress.getByName ("192.168.43.86"); / / specify the port number and establish the socket int port = 9998; socket = new Socket (address,port); / / bind the socket output stream os = socket.getOutputStream () Sca = new Scanner (System.in); while (true) {message = sca.next (); / / write to the output stream, transfer os.write (message.getBytes ()) in the LAN;}} catch (UnknownHostException e) {e.printStackTrace () } catch (IOException e) {e.printStackTrace ();} finally {try {/ / close the port number, close the io stream os.close (); socket.close ();} catch (IOException e) {e.printStackTrace () The above is the content of this article on "how to create a multithreaded LAN chat room with Java". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report