功能预览
服务器
客户端(可以同时登录多个客户端)
演示(点击看大图效果比较好,直接看变形了)
下载地址:https://download.youkuaiyun.com/download/gbstyle/11092879
github地址:https://github.com/w-g-b/IRS/tree/master/src/main/java/com/gb/chat
服务器代码
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class ChatServer{
public static void main(String[] args) throws Exception {
ChatServer cs = new ChatServer();
cs.setUpSever(Integer.parseInt(args[0]));
}
HashMap<String, Socket> scMap = new HashMap<>();
public void setUpSever(int port) {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
return;
}
System.out.println("服务器创建成功!!!");
Scanner scan = new Scanner(System.in);
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
String msg = "管理员消息: " + scan.nextLine();
for (Map.Entry<String, Socket> entry : scMap.entrySet()) {
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(entry.getValue().getOutputStream());
bos.write(msg.getBytes());
bos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}).start();
while (true) {
try {
Socket client = serverSocket.accept();
BufferedInputStream bis = new BufferedInputStream(client.getInputStream());
byte[] bytes = new byte[1024];
int len = bis.read(bytes);
String str = new String(bytes, 0, len);
for (Map.Entry<String, Socket> entry : scMap.entrySet()) {
String msg = str + " 加入到聊天室中!";
BufferedOutputStream bos = new BufferedOutputStream(entry.getValue().getOutputStream());
bos.write(msg.getBytes());
bos.flush();
}
scMap.put(str, client);
System.out.println(str + ",连接到服务器");
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
int len = bis.read(bytes);
String msg = str + " say :" + new String(bytes, 0, len);
System.out.println(msg);
for (Map.Entry<String, Socket> entry : scMap.entrySet()) {
if (str.equals(entry.getKey())) {
continue;
}
BufferedOutputStream bos = new BufferedOutputStream(entry.getValue().getOutputStream());
bos.write(msg.getBytes());
bos.flush();
}
} catch (SocketException se) {
scMap.remove(str);
System.out.println(str + "退出聊天室!");
for (Map.Entry<String, Socket> entry : scMap.entrySet()) {
String msg = str + " 退出聊天室!";
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(entry.getValue().getOutputStream());
bos.write(msg.getBytes());
bos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
break;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
客户端代码
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.util.Scanner;
public class ChatClient {
public static void main(String[] args) throws Exception {
ChatClient cc = new ChatClient();
cc.setUpClient(args[0], Integer.parseInt(args[1]));
}
public void setUpClient(String ip, int port) throws Exception {
System.out.print("请输入用户名: ");
Scanner scan = new Scanner(System.in);
String clientName = scan.nextLine();
Socket socket = new Socket(ip, port);
System.out.println("客户端 " + clientName + " 连接成功");
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
bos.write(clientName.getBytes());
bos.flush();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
byte[] bytes = new byte[1024];
int len = bis.read(bytes);
String str = new String(bytes, 0, len);
System.out.println(str);
} catch (SocketException se) {
System.out.println("服务器断开,退出程序");
System.exit(0);
} catch (IOException e) {
continue;
}
}
}
}).start();
while (true) {
String str = scan.nextLine();
bos.write(str.getBytes());
bos.flush();
}
}
}