📄 qqserver.java
字号:
package chat.server;
import java.io.*;
import java.net.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import chat.trans.*;
public class QQServer {
static final String USERFILE = "users.txt";
ServerSocket ss;
public QQServer() throws Exception{
File f = new File(USERFILE);
if(!f.exists()) f.createNewFile();
ss =
new ServerSocket(Protocol.SERVER_PORT);
while(true){
Socket s = ss.accept();
new ServerThread(s).start();
}
}
public static void main(String[] args) {
try {
new QQServer();
} catch (Exception e) {
e.printStackTrace();
}
}
}
class ServerThread extends Thread{
static Map<String, PrintWriter> users =
new HashMap<String, PrintWriter>();
Socket s;
String userName;
PrintWriter out;
BufferedReader in;
public ServerThread(Socket s)throws Exception{
this.s = s;
out =
new PrintWriter(s.getOutputStream());
InputStreamReader ir =
new InputStreamReader(
s.getInputStream());
in = new BufferedReader(ir);
}
public void run(){
try {
while(true){
String msg = in.readLine();
if(msg == null) {
users.remove(userName);
sendAll(Protocol.DEL_USER+userName);
s.close();
break;
}
System.out.println("client msg:"
+ msg);
process(msg);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void sendAll(String msg){
Iterator it = users.values().iterator();
while(it.hasNext()){
PrintWriter o = (PrintWriter) it.next();
if(o!=out){
o.println(msg);
o.flush();
}
}
}
private void process(String msg) throws Exception{
System.out.println("process message...");
char type = msg.charAt(0);
System.out.println("switch:" + type);
switch(type){
case Protocol.USER_REGISTER_MSG:
System.out.println("switch...");
String name = msg.substring(1,
msg.indexOf(Protocol.SPLIT_CHAR));
System.out.println("will validate..");
boolean b = validate(name);
if(b) {
save(msg.substring(1));
userName = name;
sendAll(Protocol.ADD_USER + name);
users.put(name, out);
out.println(msg(
Protocol.ONLINE_USER_MSG));
}
else{
out.println(msg(
Protocol.REGISTER_RESULT));
}
out.flush();
System.out.println("send to client!");
break;
case Protocol.USER_LOGIN_MSG:
String n = msg.substring(1,
msg.indexOf(Protocol.SPLIT_CHAR));
if(users.containsKey(n)){
out.println(Protocol.LOGIN_RESULT
+"不能重复登录!");
out.flush();
}else{
String p = msg.substring(
msg.indexOf(Protocol.SPLIT_CHAR)+1);
if(validate(n, p)){
userName = n;
sendAll(Protocol.ADD_USER + n);
users.put(n, out);
out.println(msg(
Protocol.ONLINE_USER_MSG));
out.flush();
}else{
out.println(Protocol.LOGIN_RESULT
+"登录信息错误!");
out.flush();
}
}
break;
case Protocol.CHAT_MSG:
String[] ss = msg.split(",");
if(ss[1].equals("all")) sendAll(msg);
else {
users.get(ss[1]).println(msg);
users.get(ss[1]).flush();
}
break;
}
}
private String msg(char type){
System.out.println("prepare message...");
StringBuffer s = new StringBuffer();
s.append(type);
switch(type){
case Protocol.REGISTER_RESULT:
s.append("该登录名已被使用!");
break;
case Protocol.ONLINE_USER_MSG:
Iterator it = users.keySet().iterator();
while(it.hasNext()){
s.append(it.next());
s.append(Protocol.SPLIT_CHAR);
}
s.deleteCharAt(s.length()-1);
break;
}
return s.toString();
}
private boolean validate(String name, String passwd)
throws Exception{
InputStream in = new
FileInputStream(QQServer.USERFILE);
InputStreamReader ir =
new InputStreamReader(in);
BufferedReader br = new BufferedReader(ir);
String s = null;
boolean ret = false;
while((s=br.readLine())!= null){
String[] ss = s.split(Protocol.SPLIT_CHAR+"");
if(ss[0].equals(name) && ss[1].equals(passwd)) {
ret = true;
break;
}
}
br.close();
return ret;
}
private boolean validate(String name) throws Exception{
System.out.println("validate loginname");
InputStream in = new
FileInputStream(QQServer.USERFILE);
InputStreamReader ir =
new InputStreamReader(in);
BufferedReader br = new BufferedReader(ir);
String s = null;
boolean ret = true;
while((s=br.readLine())!= null){
String n = s.substring(0,
s.indexOf(Protocol.SPLIT_CHAR));
if(n.equals(name)) {
ret = false;
break;
}
}
br.close();
return ret;
}
private void save(String user)throws Exception{
System.out.println("save user msg");
OutputStream o =
new FileOutputStream(QQServer.USERFILE, true);
PrintWriter pw = new PrintWriter(o);
pw.println(user);
pw.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -