public class server implements Runnable {// 服务端
static List socketList=new ArrayList();
// 读取 In
static Socket socket = null;
static ServerSocket serverSocket = null;
public server() {// 构造方法
try {
serverSocket = new ServerSocket(9999);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("****服务端*****");
server t = new server();
int count = 0;
while (true) {
try {
// System.out.println("端口9999等待被连接......");
socket = serverSocket.accept();
count++;
System.out.println("第" + count + "个客户已连接");
socketList.add(socket);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Print p = new Print(socket);
Thread read = new Thread(t);
Thread print = new Thread(p);
read.start();
print.start();
}
}
@Override
public void run() {
// 重写run方法
try {
Thread.sleep(1000);
BufferedReader in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
while (true) {
String jieshou = in.readLine();
System.out.println( jieshou);
for (int i = 0; i < socketList.size(); i++) {
Socket socket=socketList.get(i);
PrintWriter out = new PrintWriter(socket.getOutputStream());
if (socket!=this.socket) {
out.println(jieshou);
}else{
out.println("(你)"+jieshou);
}
out.flush();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Print implements Runnable {
static List socketList=new ArrayList();
Scanner input = new Scanner(System.in);
public Print(Socket s) {// 构造方法
try {
socketList.add(s);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
Thread.sleep(1000);
while (true) {
String msg = input.next();
for (int i = 0; i < socketList.size(); i++) {
Socket socket=socketList.get(i);
PrintWriter out = new PrintWriter(socket.getOutputStream());
// System.out.println("对客户端说:");
out.println("服务端说:"+msg);
out.flush();
}
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
//client.java
package Socket;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class client implements Runnable {// 客户端
static Socket socket = null;
Scanner input = new Scanner(System.in);
static String name=null;
public static void main(String[] args) {
int x=(int)(Math.random()*100);
client.name="client"+x;
System.out.println("****客户端"+x+"*****");
try {
socket = new Socket("127.0.0.1", 9999);
System.out.println("已经连上服务器了");
} catch (Exception e) {
e.printStackTrace();
}
client t = new client();
Read r = new Read(socket);
Thread print = new Thread(t);
Thread read = new Thread(r);
print.start();
read.start();
}
@Override
public void run() {
try {
Thread.sleep(1000);
PrintWriter out = new PrintWriter(socket.getOutputStream());
while (true) {
String msg = input.next();
out.println(name+"说:"+msg);
out.flush();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Read implements Runnable {
static Socket socket = null;
public Read(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
Thread.sleep(1000);
BufferedReader in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
while (true) {
System.out.println( in.readLine());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
————————————————
原文链接:https://blog.csdn.net/qq_29606255/article/details/78679815