1.创建客户端口
package com.test4;
import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;
public class KHD {
public static void main(String[] args) throws IOException {
Socket link = new Socket("127.0.0.1", 6666);
XcReader reader = new XcReader(link);
Thread t = new Thread(reader);
t.start();
XcWriter writer = new XcWriter(link);
writer.start();
}
}
2.创建服务端口
package com.test4;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class FWD {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(6666);
System.out.println("启动");
//装载所有与服务端连接的客户端信息
List<Socket> allLink = new ArrayList<>();
for (int i = 0; i < 50; i++) { //循环50个客户端
Socket link = serverSocket.accept();
allLink.add(link);
//需要一个能读且能写的
Thread t = new RorW(link,allLink);
t.start();
}
}
}
3.创建用于读取(接收消息)的线程
package com.test4;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.Socket;
public class XcReader implements Runnable {
private Socket link;
public XcReader(Socket link) {
this.link = link;
}
@Override
public void run() {
try {
InputStream is = link.getInputStream();
Reader r = new InputStreamReader(is);
while (true){
StringBuilder str = new StringBuilder();
while (true){
int a = r.read();
str.append((char)a);
if (str.toString().endsWith("[over]")){
break;
}
}
int i = str.indexOf("[over]");
System.out.println("接受一条信息: "+str.substring(0,i));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
4.创建用于说出(发送)的线程
package com.test4;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
public class XcWriter extends Thread{
private Socket link;
public XcWriter(Socket link) {
this.link = link;
}
@Override
public void run() {
try {
OutputStream os = link.getOutputStream();
while (true){
Scanner input = new Scanner(System.in);
System.out.println("发送一条消息:");
os.write(input.nextLine().getBytes());
os.write("[over]".getBytes());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
5.创建能读能写的线程
package com.test4;
import java.io.*;
import java.net.Socket;
import java.util.List;
public class RorW extends Thread {
private Socket link;
private List<Socket> allLink;
public RorW(Socket link, List<Socket> allLink) {
this.link = link;
this.allLink = allLink;
}
@Override
public void run() {
try {
InputStream is = link.getInputStream();
Reader reader = new InputStreamReader(is);
while (true) {
StringBuilder str = new StringBuilder();
//一遍一遍读
char[] chars = new char[0x10];
while (true) {
int len = reader.read(chars);
str.append(chars, 0, len);
if (str.toString().endsWith("[over]")) {
break;
}
}
//代码至此 收到发送信息
String r = str.toString().replace("[over]", "");
System.out.println("收到了来自【" + link.getInetAddress() + "】的客户信息" + r);
//找到所有的客户端 写给他们信息
for (Socket s : allLink) {
if (link == s) { //找到了本身
continue;
}
//找到别的客户端
OutputStream os = s.getOutputStream();
os.write(r.getBytes());
os.write("[over]".getBytes());
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
6.运行结果:
客户端发送消息通过服务端来为其他客户端发送消息 其他客户端完成接受消息已实现群聊效果
标签:java,Socket,群聊,link,new,import,public,服务端,客户端 From: https://blog.csdn.net/weixin_53755148/article/details/141497869