TCP测试--模拟聊天室
此程序实现了多人聊天功能,运行结果截图如下:
代码:
1.服务器:
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;
public class Server {
private List<Channel> allChannel = new ArrayList<Channel>();
public static void main(String[] args) throws IOException {
new Server().start();
}
//服务器启动
public void start() throws IOException {
ServerSocket server = new ServerSocket(6597);
while(true) {
Socket client = server.accept();
Channel channel = new Channel(client);
allChannel.add(channel);
new Thread(channel).start();
}
}
//频道
private class Channel implements Runnable {
private String name;
private DataInputStream dis;
private DataOutputStream dos;
private boolean isRunning = true;
public Channel(Socket client) {
try{dis = new DataInputStream(client.getInputStream());
dos = new DataOutputStream(client.getOutputStream());
send("请输入昵称:");
name = dis.readUTF();
send("*****欢迎您进入聊天室!*****");
sendOthers("*****"+name+"进入了聊天室*****");
} catch (IOException e) {
CloseUtil.closeAll(dos, dis);
isRunning = false;
}
}
public String receive() {
String msg = null;
try {
msg = dis.readUTF();
} catch (IOException e) {
CloseUtil.closeAll(dis);
isRunning = false;
allChannel.remove(this);
sendOthers("*****"+name+"离开了聊天室*****");
}
return msg;
}
public void send(String msg) {
if(null==msg) {
return;
}
try {
dos.writeUTF(msg);
dos.flush();
} catch (IOException e) {
CloseUtil.closeAll(dos);
isRunning = false;
allChannel.remove(this);
sendOthers("*****"+name+"离开了聊天室*****");
}
}
public void sendOthers(String msg) {
if(null==msg) {
return;
}else if(msg.startsWith("****")) {
for(Channel temp:allChannel) {
if(this==temp) {
continue;
}
temp.send(msg);
}
}else if(msg.startsWith("@")&&msg.indexOf(":")>-1) {
String name = msg.substring(1, msg.indexOf(":"));
String content = msg.substring(msg.indexOf(":")+1);
for(Channel temp:allChannel) {
if(temp.name.equals(name)&&null!=content) {
temp.send("来自"+this.name+"的悄悄话:"+content);
}
}
}else {
for(Channel temp:allChannel) {
if(this==temp) {
continue;
}
temp.send(name+":"+msg);
}
}
}
public void run() {
while(isRunning) {
String msg = receive();
sendOthers(msg);
}
}
}
}
2.客户端
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
public static class Send implements Runnable {
private BufferedReader console;
private DataOutputStream dos;
private boolean isRunning = true;
public Send(Socket client) {
console = new BufferedReader(new InputStreamReader(System.in));
try {
dos = new DataOutputStream(client.getOutputStream());
} catch (IOException e) {
isRunning = false;
CloseUtil.closeAll(dos, console);
}
}
public void run() {
while(isRunning) {
String msg = null;
try {
msg = console.readLine();
} catch (IOException e) {
msg = null;
}
try {
if(null!=msg){
dos.writeUTF(msg);
dos.flush();
}
} catch (IOException e) {
isRunning = false;
CloseUtil.closeAll(dos, console);
}
}
}
}
public static class Receive implements Runnable {
private DataInputStream dis;
private boolean isRunning = true;
public Receive(Socket client) {
try {
dis = new DataInputStream(client.getInputStream());
} catch (IOException e) {
isRunning = false;
CloseUtil.closeAll(dis);
}
}
public void run() {
while(isRunning) {
String msg = null;
try {
msg=dis.readUTF();
} catch (IOException e) {
isRunning = false;
CloseUtil.closeAll(dis);
}
System.out.println(msg);
}
}
}
public static void main(String[] args) throws UnknownHostException, IOException {
Socket client = new Socket("mylaptop", 6597);
new Thread(new Send(client)).start();
new Thread(new Receive(client)).start();
}
}
3.工具类
import java.io.Closeable;
public class CloseUtil {
public static void closeAll(Closeable... io) {
for(Closeable temp:io) {
try {
if(null!=temp) {
temp.close();
}
} catch (Exception e) {
System.out.println("关闭流失败");
e.printStackTrace();
}
}
}
}
此程序在裴新老师教学视频下完成