1.服務(wù)端: 接收一個用戶的消息,將其轉(zhuǎn)發(fā)給其他連接的用戶。
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
/**
* 創(chuàng)建服務(wù)器
* 發(fā)送數(shù)據(jù):輸出流
* 接收數(shù)據(jù):輸入流
*/
public class MyServer {
private List<MyChannel> all = new ArrayList<>();
public static void main(String[] args) throws IOException {
new MyServer().start();
}
public void start() throws IOException {
ServerSocket server = new ServerSocket(8888);
while(true) {
//main線程:用于不斷連接用戶,將連接放到list中保存,啟動專門用于轉(zhuǎn)發(fā)消息的線程。
Socket socket = server.accept();
MyChannel myChannel = new MyChannel(socket);
all.add(myChannel);
new Thread(myChannel).start();
}
}
/**
* 一個客戶端一條道路,
* 一個客戶端一個線程。
* 1.輸入流
* 2.輸出流
* 3.接收數(shù)據(jù)
* 4.發(fā)送數(shù)據(jù)
* 成員內(nèi)部類,靜態(tài)方法不能訪問。
* 服務(wù)端轉(zhuǎn)發(fā)數(shù)據(jù)線程。
*/
class MyChannel implements Runnable{
//輸入流
private DataInputStream dis;
//輸出流
private DataOutputStream dos;
//控制線程的標(biāo)識符
private boolean isRunning = true;
//初始化建立管道
public MyChannel(Socket socket) {
try {
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
CloseUtil.closeAll(dis, dos);
all.remove(this);
isRunning = false;
}
}
//讀取數(shù)據(jù)
private String receive() {
String msg = "";
try {
msg = dis.readUTF();
} catch (IOException e) {
e.printStackTrace();
CloseUtil.closeAll(dis, dos);
all.remove(this);
isRunning = false;
}
return msg;
}
//發(fā)送給this數(shù)據(jù)
private void send(String msg) {
if(msg == null || msg.equals("")) {
return;
}
try {
dos.writeUTF(msg);
dos.flush();
} catch (IOException e) {
e.printStackTrace();
CloseUtil.closeAll(dis, dos);
all.remove(this);
isRunning = false;
}
}
//發(fā)送給其他客戶端
private void sendOthers() {
String msg = receive();
//遍歷容器
for(MyChannel other : all) {
if(other == this) {
continue;
}
//發(fā)送給其他客戶端
other.send(msg);
}
}
@Override
public void run() {
while(isRunning){
sendOthers();
}
}
}
}
2.客戶端:
import java.io.IOException;
import java.net.Socket;
/**
* 創(chuàng)建客戶端:發(fā)送數(shù)據(jù)+接收數(shù)據(jù)
* 發(fā)送數(shù)據(jù):輸出流
* 接收數(shù)據(jù):輸入流
* 輸出流和輸入流應(yīng)該彼此獨(dú)立,使用線程處理。
*/
public class MyClient {
public static void main(String[] args) throws IOException {
Socket client = new Socket("localhost", 8888);
//發(fā)送 控制臺輸入并發(fā)送到服務(wù)端
new Thread(new Send(client)).start();//一條路徑
//接收 接收服務(wù)端數(shù)據(jù)并打印
new Thread(new Receive(client)).start();//一條路徑
}
}
客戶端發(fā)送數(shù)據(jù)--線程 從控制臺接收數(shù)據(jù),發(fā)送到服務(wù)端。
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
/**
* 發(fā)送數(shù)據(jù) 線程
*/
public class Send implements Runnable{
//控制臺輸入流
private BufferedReader console;
//管道輸出流
private DataOutputStream dos;
//控制線程的標(biāo)識符
private boolean isRunning = true;
//初始化建立管道
public Send(Socket socket) {
try {
console = new BufferedReader(new InputStreamReader(System.in));
dos = new DataOutputStream(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
isRunning = false;
CloseUtil.closeAll(dos, console);
}
}
/**
* 1.從控制臺接收數(shù)據(jù)
* 2.向服務(wù)端發(fā)送數(shù)據(jù)
*/
//1.從控制臺接收數(shù)據(jù)
private String getMsgFromConsole() {
String msg = "";
try {
msg = console.readLine();
} catch (IOException e) {
e.printStackTrace();
isRunning = false;
CloseUtil.closeAll(dos, console);
}
return msg;
}
//2.向服務(wù)端發(fā)送數(shù)據(jù)
private void send() {
String msg = getMsgFromConsole();
try {
if(msg != null && !msg.equals("")) {
dos.writeUTF(msg);
dos.flush();//強(qiáng)制刷新
}
} catch (IOException e) {
e.printStackTrace();
isRunning = false;
CloseUtil.closeAll(dos, console);
}
}
@Override
public void run() {
//線程體
while(isRunning) {
send();
}
}
}
客戶端接收數(shù)據(jù)--線程 從服務(wù)端接收數(shù)據(jù)并打印
import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;
public class Receive implements Runnable{
//輸入流
private DataInputStream dis;
//線程標(biāo)識符
private boolean isRunning = true;
//初始化建立管道
public Receive(Socket socket) {
try {
dis = new DataInputStream(socket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
isRunning = false;
CloseUtil.closeAll(dis);
}
}
/**
* 接收數(shù)據(jù)
*/
private String receive() {
String msg = "";
try {
msg = dis.readUTF();
} catch (IOException e) {
e.printStackTrace();
isRunning = false;
CloseUtil.closeAll(dis);
}
return msg;
}
@Override
public void run() {
//線程體
while(isRunning) {
System.out.println(receive());
}
}
}
3.關(guān)閉流工具類:
import java.io.Closeable;
import java.io.IOException;
/**
* 關(guān)閉io流的工具
*/
public class CloseUtil {
public static void closeAll(Closeable... io) {
for(Closeable temp : io) {
try {
if(temp != null) {
temp.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
更多建議: