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 understand the core code of Java Socket chat program

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

How to understand the core code of Java Socket chat program, in view of this problem, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

Java Socket chat program in the preparation of a lot of things we need to pay attention to, this program is based on Java Socket chat program, using TCP transmission protocol to achieve the exchange of information between two people. Next, we will introduce this procedure to you in detail.

Before forming the final result, I went through two processes, both of which were semi-finished products, which reflected the thinking process of my whole course design. After a more systematic thinking process, socket thought strengthened layer by layer, which impressed me deeply and gained a lot. In this document, I will demonstrate the thinking process of my two process programs, and then explain the source code, but the final uploaded code is a finished product.

In the two process programs, TestServer1 and TestClient1 are * processes, TestServer2 and TestClient2 are the second process, and MySingleThreadServer1 and MySingleThreadClient1 are the final programs. Among them, TestServer2 and TestClient2 implement multithreading, one thread is responsible for receiving, the other thread is responsible for sending, MySingleThreadServer1 and MySingleThreadClient1 implement GUI-based chat. Control is not as easy as MFC can be achieved by dragging components, java is achieved through programming, I use awt component coding to achieve GUI, the interface is very simple, but it takes a lot of time to layout.

The core code explains:

The three procedures all involve the core idea of the Java Socket chat program, the following is the core explanation.

1. Server side

The Java Socket server needs to introduce two packages, the java. Io package and the java.net package. The io package solves the problem of input and output streams, while the net package contains the API needed for socket programming. The server first needs to get the object of ServerSocket, that is, ServerSocket ss = new ServerSocket (5555); 5555 is the port number of the server. Socket s = ss.accept (); the server-side Socket object starts listening for linked client information through the accept () method. If there is information from the client, the object s calls the method of the input and output stream, such as s.getInputStream (), and encapsulates the resulting InputStream in DataInputStream. When the client communicates with the server, it is possible that the two ends exist in different operating systems. Encapsulating in DataInputStream can solve this problem.

two。 Client

In fact, most of the client-side code is similar to the server-side, with one significant difference: there is no ServerSocket class on the client side, that is, the client does not have to listen for any links, it just needs to send the links. Socket s = new Socket (String IPAddr,int port), IPAddr is the server-side IP address, and port is the server-side port number 5555. Since the server and client of this program are on the same host, the server-side IP address is 127.0.0.1. The Java Socket chat program object s can be obtained through the IPAddr and port parameters, and the next step is similar to the server program.

A detailed explanation of the three processes.

Procedure1: the server-side core code is as follows:

ServerSocket ss = new ServerSocket (5555)

Socket s = ss.accept ()

OutputStream os = s.getOutputStream ()

DataOutputStream dos = new DataOutputStream (os)

InputStream is = s.getInputStream ()

DataInputStream dis = new DataInputStream (is)

InputStreamReader isr = new InputStreamReader (System.in)

/ / re-keypad reading data

BufferedReader br = new BufferedReader (isr)

/ / put data read from the keyboard into the buffer

String info

While (true) {

Info = dis.readUTF ()

System.out.println ("client says:" + info)

If (info.equals ("goodbye")) {

Break

}

Info = br.readLine ()

Dos.writeUTF (info)

System.out.println ("Server says" + info)

If (info.equals ("goodbye")) {

Break

}

}

The client core code is as follows:

Socket s = new Socket ("127.0.0.1", 5555); InputStream is = s.getInputStream (); DataInputStream dis = new DataInputStream (is); OutputStream os = s.getOutputStream (); DataOutputStream dos = new DataOutputStream (os); InputStreamReader isr = new InputStreamReader (System.in); BufferedReader br = new BufferedReader (isr); String info; while (true) {info = br.readLine (); System.out.println ("client says:" + info); dos.writeUTF (info) If (info.equals ("goodbye")) {break;} info = dis.readUTF (); / / blocking function System.out.println ("the server says:" + info); if (info.equals ("goodbye")) {break;}}

The server side and the client side are different in the while (true) loop. The server side is dis.readUTF (). You must first read the information sent by the client side before passing info = br.readLine (); dos.writeUTF (info); read the information from the keyboard and then send it to the client side. Instead, the client must first read the keyboard information through br.readLine (); before it can receive the information sent by the server.

Thinking 1: this simple chat program has realized the information exchange between the server and the client, but at this time there has been an inevitable problem, for example, on the server side, when the server sends a message to the client through dos.writeUTF (info), in the while loop, it has to execute the info = dis.readUTF () code, and readUTF () is a blocking function, if the client does not send it. It is blocked in that place, at this time the following part of the code dos.writeUTF (info) can not be executed, that is, the server cannot send messages.

How can Java Socket chat programs solve this problem? How can readUTF () block and writeUTF (info) send messages at the same time? Obviously, when one path fails, you should consider taking another path, so multithreading is introduced here. That's how Procedure2 came out.

This is the answer to the question about how to understand the core code of the Java Socket chat program. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

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