Java网络编程Demo,使用TCP 实现简单群聊功能Groupchat,创建一个服务端,使多个客户端都能收到消息

效果图:

开启服务端

Java网络编程Demo,使用TCP 实现简单群聊功能Groupchat,创建一个服务端,使多个客户端都能收到消息

客户端一

Java网络编程Demo,使用TCP 实现简单群聊功能Groupchat,创建一个服务端,使多个客户端都能收到消息

客户端二

Java网络编程Demo,使用TCP 实现简单群聊功能Groupchat,创建一个服务端,使多个客户端都能收到消息

客户端三

Java网络编程Demo,使用TCP 实现简单群聊功能Groupchat,创建一个服务端,使多个客户端都能收到消息

实现代码:

客户端类

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List; public class SocketServer {
public static List<Socket> socketList=new ArrayList<Socket>();
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8088);
System.out.println("聊天服务已开启");
while(true){
//当客户端发出请求,服务端建立的通向客户端的专属通道
Socket socket = serverSocket.accept();
socketList.add(socket);
new Thread(new ServerThread(socket)).start();
}
}
}

服务器线程类

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket; public class ServerThread implements Runnable{
public Socket socket; public ServerThread(Socket socket) {
super();
this.socket = socket;
} @Override
public void run() {
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); while (true) {
String readLine = bufferedReader.readLine();
for (Socket item : SocketServer.socketList) {
PrintWriter pw=new PrintWriter(item.getOutputStream());
pw.println("用户"+socket.getPort()+"说"+readLine);
pw.flush();
}
}
} catch (Exception e) {
}
} }

读取的线程类

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket; public class ReaderThread implements Runnable{
public Socket socket; public ReaderThread(Socket socket) {
super();
this.socket = socket;
} @Override
public void run() {
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (true) {
String data=bufferedReader.readLine();
System.out.println(data);
}
} catch (IOException e) {
e.printStackTrace();
} } }

写出的线程类

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket; public class WriterThread implements Runnable{
public Socket socket; public WriterThread(Socket socket) {
super();
this.socket = socket;
} @Override
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter printWriter = new PrintWriter(socket.getOutputStream());
while (true) {
String data=br.readLine();
printWriter.println(data);
printWriter.flush();
}
} catch (Exception e) { }
} }

客户端类

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException; public class SocketClient {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket socket = new Socket("127.0.0.1",8088);
System.out.println("您已上线");
new Thread(new WriterThread(socket)).start();
new Thread(new ReaderThread(socket)).start();
}
}
上一篇:c++ 网络编程(二) linux 下多进程socket通信 多个客户端与单个服务端交互代码实现回声服务器


下一篇:JVM-高效并发