📄 chatserver.java
字号:
package net.yangzhe.chatSocket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Vector;
public class ChatServer extends Thread{
/**
* @param args
*/
private Socket client;
static private String cache="";
private String localcache="";
private String str;
PrintWriter out;
BufferedReader in;
static ServeStart servestart;
private String currentUser=null;
static boolean isWork=true;
static public Vector <ChatServer> serverCollection= new Vector<ChatServer>();
static public Vector <String> userList= new Vector<String>();
private static HashMap<String,String> user= new HashMap<String,String>();
public ChatServer(Socket c, ServeStart ss) throws IOException{
this.client=c;
servestart=ss;
}
public void run(){
try{
in=new BufferedReader(new InputStreamReader(client.getInputStream()));
out=new PrintWriter(client.getOutputStream());
//Mutil User but can't parallel
boolean flag=true;
while(flag&&isWork){
str=in.readLine();
if(str.equals("%1%1")){//添加用户
String username=in.readLine();
String password=in.readLine();
if(user.get(username)==null){
user.put(username,password);
out.println("%4%4");//succeed
out.flush();
}else{
out.println("%5%5");
out.flush();
}
}else if(str.equals("%t%tTestServer")){
out.println("%t%tOK");
out.flush();
}else if(str.equals("%0%0")){//jump out of cycle 1 at server
this.currentUser=in.readLine();
userList.add(currentUser);
flag=false;
break;
}else if(str.equals("%end")){
//退出thread,登陆任务完成
break;
}else if(str.equals("%2%2")){//检测用户密码,若无则新用户
String username=in.readLine();
String password=in.readLine();
String realpass=user.get(username);
if(realpass==null){
out.println("%3%3");//无此用户
out.flush();
//user.put(username,password);
}else if(realpass.equals(password)){
out.println("%4%4");//密码正确
out.flush();
flag=false;
break;
}else{
out.println("%5%5");//错误
out.flush();
}
}
}
while(isWork){
//getUserList();
try{
str=in.readLine();
}catch(SocketException e){
userList.remove(currentUser);
client.close();
this.stop();
}
if(str.equals("%6%6")){//refresh
if(!localcache.equals(cache)){
localcache=cache;
out.println(cache);
out.flush();
}
}else if(str.equals("%end")){
//退出thread,chat任务完成
break;
}else if(!str.equals("%7%7")){
System.out.println(str);
cache+=now()+str+"%8%8%8%8";
localcache=cache;
System.out.println(cache);
out.println(cache);
out.flush();
}
}
out.println("%ServerEnd");
out.flush();
userList.remove(currentUser);
client.close();
}catch(IOException ex){
ex.printStackTrace();
}finally{
}
}
public static String now()
{
Date today = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("[hh:mm:ss] ");
String dateString=null;
try
{
dateString = formatter.format(today);
return dateString;
} catch (IllegalArgumentException iae) {
//result.setForeground(Color.red);
// result.setText("Error: " + iae.getMessage());
}
return dateString;
}
public static Vector<String> getUserList(){
return userList;
}
public static void main(String[] args)throws IOException{
ServerSocket server=new ServerSocket(5678);
while(true){
//transfer location change Single User or Multi User
ChatServer mu=new ChatServer(server.accept(),null);
mu.start();
}
}
public static void end() {
// TODO Auto-generated method stub
isWork=false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -