70 lines of code? Realize text chat between two devices [Hello, 10086 ♥], Simplified chat room, understand Socket project implementation [java development]

Keywords: Java Eclipse udp

Java learning punch in: day 33

Java cultivation program (punch in day 32)

Simplified chat [70 lines of code to realize text chat between two devices] ##Simplified version - chat between two computers

For ease of understanding, let's simplify Socket communication

A large part of yesterday's project is interface design and event monitoring, so here we will not use the interface, but simply use the console output

Instance requirements

  • Connect the computer with the specified ip address and operate on the specified port. If the connection is successful, the connection is successful
  • Users of two computers can chat with each other on the console [just like QQ, but only text chat]

Project code: [text communication can be realized in 70 lines]

Let's take a look at the running effect. It's still fun. The main reason is that these 70 lines of code may let you understand some doubts in the previous Socket project


The green text is the message entered by the computer console, and the black text is the received message. The above is the server console and the below is the client console

Server side

package ChatDemo;

import java.io.*;
import java.net.*;

public class ServerSocketDemo extends Thread {
	private static Socket skt;
	@Override
	public void run() {
		try {//Console read and output
			BufferedReader breader = new BufferedReader(new InputStreamReader(System.in));//Layers of decoration, the innermost layer is InputStream media stream
			PrintWriter pw = new PrintWriter(skt.getOutputStream());//Socket output stream
			while(true)
			{
				pw.println(breader.readLine());
				pw.flush();
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) throws Exception {
		ServerSocket server = new ServerSocket(10086);
		skt = server.accept();//Refers to connected equipment
		System.out.println("server Connection succeeded");
		new ServerSocketDemo().start();
		BufferedReader brin = new BufferedReader(new InputStreamReader(skt.getInputStream()));//Network socket stream
		while(true)
		{
			System.out.println(brin.readLine());
		}
		
	}
}
//Here is read by line. When a message is entered, press enter to send the message

client

package ChatDemo;

import java.io.*;
import java.net.*;


public class SocketDemo extends Thread {
	private static Socket skt;
	@Override
	public void run() {
		try {//Console input, here is only text, so character flow is used to improve efficiency; this thread controls output
			BufferedReader breader = new BufferedReader(new InputStreamReader(System.in));//Layers of decoration, the innermost layer is InputStream media stream
			PrintWriter pw = new PrintWriter(skt.getOutputStream());//Socket output stream
			while(true)
			{
				pw.println(breader.readLine());
				pw.flush();
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) throws Exception {
		skt = new Socket("127.0.0.1",10086);
		System.out.println("client Connection succeeded");
		new SocketDemo().start();//Start thread
		BufferedReader brin = new BufferedReader(new InputStreamReader(skt.getInputStream()));//Network socket stream
		while(true)
		{
			System.out.println(brin.readLine());
		}
	}
}
//Like the server, the main thread receives messages and the new thread sends messages, because if it is the same thread, there will be confusion

The code is simple and easy to understand. A simple 70 lines of code can realize the LAN communication between two computers. Don't you think it's very simple? You can run the code. 127.0.0.1 here refers to the local machine, that is, chatting with yourself, which can alleviate boredom. You can interact by changing the IP address. This is a simple C/S architecture.
Today is mainly to simplify yesterday's complex code, so that everyone can roughly clarify the implementation method of the function, mainly 70 lines of code, which is very concise. You can quickly try it on the computer and chat with words~~

Posted by jayant on Sat, 16 Oct 2021 01:30:24 -0700