这是聊天室第二版
客户端使用了两个线程一个收,一个发,但只能实现根据地址私聊,每次都要输地址加:
public class ChatServer {
public static void main(String[] args) throws IOException {
//1创建ServerSocket
ServerSocket listener=new ServerSocket(9098);
Map<String,Socket> maps= Collections.synchronizedMap(new HashMap<>());
//2监听
System.out.println("聊天服务器已启动....");
try {
while(true) {
Socket socket = listener.accept();
System.out.println("连接成功:"+socket.getInetAddress().getHostAddress());
maps.put(socket.getInetAddress().getHostAddress(), socket);
new ChatThread(socket,maps).start();
}
} catch (IOException e) {
System.out.println("聊天室结束了");
}finally {
listener.close();
}
}
}
public class ChatClient {
public static void main(String[] args) throws Exception{
//1创建socket
Socket socket=new Socket("10.9.21.178", 65524);
//2发送线程
new Thread(new Runnable() {
@Override
public void run() {
BufferedWriter bw=null;
try {
//2 获取输出流
OutputStream os = socket.getOutputStream();
bw=new BufferedWriter(new OutputStreamWriter(os));
//3发送
Scanner input=new Scanner(System.in);
while(true){
String data = input.nextLine();
bw.write(data);
bw.newLine();
bw.flush();
if(data.equals("baibai")||data.equals("over")||data.equals("886")){
break;
}
}
//4关闭
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
bw.close();
if(!socket.isClosed()) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
//3接受数据线程
new Thread(new Runnable() {
@Override
public void run() {
BufferedReader br=null;
try {
InputStream is = socket.getInputStream();
br=new BufferedReader(new InputStreamReader(is));
while (true) {
String data = br.readLine();
System.out.println(data);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
br.close();
if(!socket.isClosed()) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
}
}``
public class ChatThread extends Thread {
private Socket socket;
private Map<String,Socket> maps;
public ChatThread(Socket socket,Map<String,Socket> maps) {
this.socket = socket;
this.maps=maps;
}
@Override
public void run() {
BufferedReader br=null;
if(socket!=null){
try {
InputStream is = socket.getInputStream();
br=new BufferedReader(new InputStreamReader(is));
while(true){
String data = br.readLine();
if(data==null){
System.out.println(socket.getInetAddress().getHostAddress()+"退出了");
maps.remove(this.socket.getInetAddress().getHostAddress());
break;
}
System.out.println(socket.getInetAddress().getHostAddress()+"说:"+data);
if(data.equals("baibai")||data.equals("over")||data.equals("886")){
System.out.println(socket.getInetAddress().getHostAddress()+"退出了");
maps.remove(this.socket.getInetAddress().getHostAddress());
break;
}
//转发数据 10.9.21.188:你好
//1截取ip地址
String ip=data.substring(0, data.indexOf(":"));
String pattern = "((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}";
if(ip!=null){
boolean result = ip.matches(pattern);
if(result){
Socket socket = maps.get(ip);
if(socket!=null){
//2转发
synchronized (socket){
OutputStream os = socket.getOutputStream();
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(os));
String address = this.socket.getInetAddress().getHostAddress();
bw.write(address+"说:"+data.substring(data.indexOf(":")+1));
bw.newLine();
bw.flush();
System.out.println(address+"给"+ip+"转发了数据");
}
}
}
}
}
} catch (Exception e) {
//e.printStackTrace();
//e.printStackTrace();
System.out.println(socket.getInetAddress().getHostAddress()+"异常退出了...");
maps.remove(this.socket.getInetAddress().getHostAddress());
}finally {
try {
br.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}