Java network programming realizes the chat room function (multiple computers are connected)

Java network programming realizes the chat room function (multiple computers are connected)

The so-called network programming is the information exchange between computers, and it is relatively simple for programmers to master a programming method. The java SDK provides us with some APIs to simplify the writing of the program. All classes of network programming exist in java.net.*. Just import it to enter the world of network programming. There are many methods of network programming, Socket is one of them.
The code is directly below

package com;

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.net.URLDecoder;
import java.util.ArrayList;

import javax.xml.ws.handler.MessageContext.Scope;
/**Server-side class
**/
public class ServerClient extends Thread{ //Define a collection to add connected clients private static ArrayList clients = new ArrayList();

public static void main(String[] args) {
	try {
	  //设置服务端的端口
	  /*ps:当你ServerSocket serverSocket  = new ServerSocket (8888)就已经建立了一个固定的位置可以让其它的计算机来访问你了。这里要稍微补充一下端口的支持,端口是为了唯一标识每台计算机唯一服务的,另外端口号是从0~65535之间的,前1024个端口已经被T

Guess you like

Origin blog.csdn.net/MyAzhe0ci3/article/details/109293071