package com.itshixun.steven.net;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class ChattingRoom {
/**
* map中key是nickName
*/
private Map
public Map
return playerMap;
}
public void setPlayerMap(Map
this.playerMap = playerMap;
}
/**
* 说话
*
* @param msg
*/
public synchronized void threadSay(String msg) {
Player p = (Player) Thread.currentThread();
if (msg.equalsIgnoreCase("bye")) {
notifyToAllPlayer(p.getName() + " 退出了聊天室!");
try {
p.getSocket().close();
} catch (IOException e) {
}
// 从map中删除!
getPlayerMap().remove(p.getName());
} else {
notifyToAllPlayer(new Date().toLocaleString() + " " + p.getName()
+ " 说: " + msg);
p.getWriter().println("请输入您的发言(输入/"bye/"退出):");
}
// for (Map.Entry
// System.out.println(entry.getValue().getName() + "是否关闭:"
// + entry.getValue().getSocket().isClosed());
// }
}
public synchronized void notifyToAllPlayer(String msg) {
System.out.println(msg);
for (Map.Entry
entry.getValue().getWriter().println(msg);
}
}
public synchronized void addPlayer(Player player) {
}
public synchronized boolean checkName(Player player) {
if (player.getName() == null || player.getName().length() == 0) {
return false;
} else {
if (!this.getPlayerMap().containsKey(player.getName())) {
getPlayerMap().put(player.getName(), player);
notifyToAllPlayer("欢迎 " + player.getName() + " 进入!!!");
return true;
} else {
player.getWriter().println("昵称已存在,请修改!");
return false;
}
}
}
public synchronized void removePlayer(Player player) {
}
}
第二个类:
package com.itshixun.steven.net;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
public class ChattingServer {
private ServerSocket ss;
private static final int serverPort = 8888;
private ChattingRoom room;
public ChattingServer() {
this.setRoom(new ChattingRoom());
}
public void start() {
try {
this.setSs(new ServerSocket(serverPort));
System.out.println("服务器启动(port:" + serverPort + ") at "
+ new Date().toLocaleString());
while (true) {
Socket s = ss.accept();
new Player(s, getRoom()).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void end() {
System.exit(0);
}
public ServerSocket getSs() {
return ss;
}
public void setSs(ServerSocket ss) {
this.ss = ss;
}
public static int getServerPort() {
return serverPort;
}
public ChattingRoom getRoom() {
return room;
}
public void setRoom(ChattingRoom room) {
this.room = room;
}
}
第三个类:
package com.itshixun.steven.net;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class Player extends Thread {
private Socket socket;
private String nickName;
private ChattingRoom room;
private BufferedReader reader;
private PrintWriter writer;
public BufferedReader getReader() {
return reader;
}
public void setReader(BufferedReader reader) {
this.reader = reader;
}
public PrintWriter getWriter() {
return writer;
}
public void setWriter(PrintWriter writer) {
this.writer = writer;
}
public ChattingRoom getRoom() {
return room;
}
public void setRoom(ChattingRoom room) {
this.room = room;
}
public Socket getSocket() {
return socket;
}
public void setSocket(Socket socket) {
this.socket = socket;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public Player(Socket socket, ChattingRoom room) {
super();
this.socket = socket;
this.room = room;
}
@Override
public void run() {
try {
// 设置输入输出对象
this.setReader(new BufferedReader(new InputStreamReader(this
.getSocket().getInputStream())));
this.setWriter(new PrintWriter(this.getSocket().getOutputStream(),true));
// 处理登记手续;
do {
this.getWriter().println("请输入昵称:");
this.setName(this.getReader().readLine());
} while (!getRoom().checkName(this));
//循环说话
try {
while (true) {
String msg = this.getReader().readLine();
getRoom().threadSay(msg);
}
} catch (Exception e) {
//使之不处理socket关闭异常!
}
} catch (IOException e) {
e.printStackTrace();
}
}
public synchronized void writeMessage(String msg) {
writer.append(msg);
}
}
运行类:
package com.itshixun.steven.net;
public class Run {
public static void main(String[] args) {
ChattingServer s = new ChattingServer();
s.start();
}
}