📄 serverthread.java
字号:
package chat;
import java.io.*;
import java.util.*;
import java.net.*;
/**
*
* <p>Title:服务器线程类 </p>
* <p>Description: 消息转发</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class ServerThread extends Thread {
String _name;
String _key;
int counter;
DataOutputStream dos=null;
DataInputStream dis=null;
Socket socket=null;
Hashtable peopleList=null;
// ChatUserDB chatUserDB=new ChatUserDB();
XmlDispose xmlDispose=new XmlDispose();
/**
*
* @param socket Socket 客户套接
* @param peopleList Hashtable 用户列表
*/
public ServerThread(Socket socket,Hashtable peopleList){
this.socket=socket;
this.peopleList=peopleList;
try{
dos=new DataOutputStream(socket.getOutputStream());
dis=new DataInputStream(socket.getInputStream());
}catch(IOException e){
e.printStackTrace();
}
}
public void run() {
String s="LOGIN_SUCCEED:";
while(true){
String _message = null;
try {
_message = dis.readUTF();
if(_message.startsWith("LOGIN_NAME:")){//用户登陆
//调用数据库函数,确认用户
//存线程
_name=_message.substring(_message.indexOf(":")+1,_message.indexOf("LOGIN_PD:"));
_key=_message.substring(_message.indexOf("LOGIN_PD:")+9);
//if(chatUserDB.checkUser(_name,_key)){
try{
if (xmlDispose.checkUser(_name, _key)) {//验证登陆成功
peopleList.put(_name, this);
dos.writeUTF("LOGIN_SUCCEED:");
dos.writeUTF("LOGIN_PEOPLE:大家");
Enumeration _enum = peopleList.elements();
while (_enum.hasMoreElements()) { //发送名字
ServerThread serverThread = (ServerThread) _enum.nextElement();
serverThread.dos.writeUTF("LOGIN_PEOPLE:" + _name);
if (serverThread != this) {
dos.writeUTF("LOGIN_PEOPLE:" + serverThread._name);
}
}
dos.flush();
}
else {
//System.out.println("eeee");
dos.writeUTF("LOGIN_ERROR");
}
}catch(Exception e){}
}
else if(_message.startsWith("PRIVATE_MESSAGE:")){//私人消息
String _toPeoples=_message.substring(_message.indexOf("#TOPEOPLE#")+10,_message.indexOf("#MESSAGECONTENT#"));
String _content=_message.substring(_message.indexOf("#MESSAGECONTENT#")+16);
StringTokenizer fenxi=new StringTokenizer(_toPeoples,"$");
counter=fenxi.countTokens();
for(int i=0;i<counter;i++){//循环转发消息
String _toPeople=fenxi.nextToken();
ServerThread serverThread=(ServerThread)peopleList.get(_toPeople);
if(serverThread!=null){
serverThread.dos.writeUTF("PRIVATE_MESSAGE:"+_name+"对您说:"+"#CONTENT#"+_content);
}else{
serverThread.dos.writeUTF("PEOPLE_LEAVE:"+_toPeople+"已经离开");
}
}
}
else if(_message.startsWith("PUBLIC_MESSAGE:")){//公共消息
//转发消息
String _content=_message.substring(_message.indexOf("#MESSAGECONTENT#")+16);
Enumeration enum=peopleList.elements();
while(enum.hasMoreElements()){//循环转发消息
ServerThread serverThread=(ServerThread)enum.nextElement();
if(serverThread!=this){//不发消息发给自己
serverThread.dos.writeUTF("PUBLIC_MESSAGE:"+ _name + "对大家说:" +"#CONTENT#"+ _content);
}
}
}
else if(_message.startsWith("CONSOLE_PRIVATE_MESSAGE:")){//控制台消息
String _toPeoples=_message.substring(_message.indexOf("#TOPEOPLE#")+10,_message.indexOf("#MESSAGECONTENT#"));
String _content=_message.substring(_message.indexOf("#MESSAGECONTENT#")+16);
StringTokenizer fenxi=new StringTokenizer(_toPeoples,"$");
counter=fenxi.countTokens();
for(int i=0;i<counter;i++){
String _toPeople=fenxi.nextToken();
ServerThread serverThread=(ServerThread)peopleList.get(_toPeople);
if(serverThread!=null){
serverThread.dos.writeUTF("CONSOLE_PRIVATE_MESSAGE:"+_name+"对您说:"+"#CONTENT#"+_content);
}else{
serverThread.dos.writeUTF("PEOPLE_LEAVE:"+_toPeople+"已经离开");
}
}
}
else if(_message.startsWith("CONSOLE_PUBLIC_MESSAGE:")){//控制台公共消息
//转发消息
String _content=_message.substring(_message.indexOf("#MESSAGECONTENT#")+16);
Enumeration enum=peopleList.elements();
while(enum.hasMoreElements()){
ServerThread serverThread=(ServerThread)enum.nextElement();
if(serverThread!=this){
serverThread.dos.writeUTF("CONSOLE_PUBLIC_MESSAGE:"+ _name + "对大家说:" +"#CONTENT#"+ _content);
}
}
}
else if(_message.startsWith("SEND_FILE:")){//发送文件
String _toPeople=_message.substring(_message.indexOf("#TOPEOPLE#")+10,_message.indexOf("#PORT#"));
String _toSendFile=_message.substring(_message.indexOf("SEND_FILE:")+10,_message.indexOf("#TOPEOPLE#"));
String _port=_message.substring(_message.indexOf("#PORT#")+6,_message.indexOf("#FILE_LENGTH#"));
String fileLength=_message.substring(_message.indexOf("#FILE_LENGTH#")+13);
ServerThread serverThread=(ServerThread)peopleList.get(_toPeople);
InetAddress ipAddress=this.socket.getInetAddress();
if(serverThread!=null){
serverThread.dos.writeUTF("SEND_FILE:"+_name+"#TOSENDFILE#"+_toSendFile+"#IPADDRESS#"+ipAddress+"#PORT#"+_port+"#FILE_LENGTH#"+fileLength);
// System.out.println("SEND_FILE:"+_name+"#TOSENDFILE#"+_toSendFile+"#IPADDRESS#"+ipAddress+"#PORT#"+_port+"#FILE_LENGTH#"+fileLength);
}else{
serverThread.dos.writeUTF("PEOPLE_LEAVE:"+_toPeople+"已经离开");
}
//File f=new File(_toSendFile);
//System.out.println(_toPeople+" "+f.getName()+" "+_port);
}
else if(_message.startsWith("CANCEL_SENDING:")){//取消发送文件
//System.out.println(_message);
String _fromPeople=_message.substring(_message.indexOf("CANCEL_SENDING:")+15,_message.indexOf("#TOPEOPLE#"));
String _content=_message.substring(_message.indexOf("#TOPEOPLE#")+10);
ServerThread serverThread=(ServerThread)peopleList.get(_fromPeople);
if(serverThread!=null){
serverThread.dos.writeUTF("CANCEL_SENDING:"+_content);
}
}
else if(_message.startsWith("CANCEL_RECEIVED:")){//取消接收
// System.out.println(_message);
String _fromPeople=_message.substring(_message.indexOf("CANCEL_RECEIVED:")+16,_message.indexOf("#TOPEOPLE#"));
String _content=_message.substring(_message.indexOf("#TOPEOPLE#")+10);
ServerThread serverThread=(ServerThread)peopleList.get(_fromPeople);
if(serverThread!=null){
serverThread.dos.writeUTF("CANCEL_RECEIVED:"+_content);
}
}
else if(_message.startsWith("PRIVATEMESSAGELOG:")){//私人聊天记录
String _content=_message.substring(18);
String filePath="./chatLog";
File chatLog=new File(filePath,this._name);
if(!chatLog.exists()){
chatLog.mkdir();
}
File saveFile;
RandomAccessFile outputStream;
try{
saveFile = new File(chatLog,this._name + "私人");
outputStream = new RandomAccessFile(saveFile, "rw");
outputStream.seek(outputStream.length());
outputStream.write(_content.getBytes());
outputStream.close();
}catch(Exception e){
}
}
else if(_message.startsWith("PUBLICMESSAGELOG:")){//公共聊天记录
String _content=_message.substring(17);
String filePath="./chatLog";
File chatLog=new File(filePath,this._name);
if(!chatLog.exists()){
chatLog.mkdir();
}
File saveFile;
RandomAccessFile outputStream;
try{
saveFile = new File(chatLog,this._name + "公共");
outputStream = new RandomAccessFile(saveFile, "rw");
outputStream.seek(outputStream.length());
outputStream.write(_content.getBytes());
outputStream.close();
}catch(Exception e){
}
}
else if(_message.startsWith("REGISTER_NAME:")){//用户注册
_name=_message.substring(_message.indexOf(":")+1,_message.indexOf("#REGISTER_PD#"));
_key=_message.substring(_message.indexOf("#REGISTER_PD#")+13,_message.indexOf("#REGISTER_NAME#"));
String _trueName=_message.substring(_message.indexOf("#REGISTER_NAME#")+15,_message.indexOf("#REGISTER_EMAIL#"));
String _email=_message.substring(_message.lastIndexOf("#REGISTER_EMAIL#")+16);
byte result=xmlDispose.registerUser(_name,_key,_trueName,_email);
if(result==1){
dos.writeUTF("REGISTER_SUCCEED:");
}else if(result==-1){
dos.writeUTF("REGISTER_ERROR_EXIST");
}else if(result==0){
dos.writeUTF("REGISTER_ERROR");
}
}
}
catch (IOException ioe) {
Enumeration enum=peopleList.elements();
while(enum.hasMoreElements()){
try{
ServerThread serverThread=(ServerThread)enum.nextElement();
if(serverThread!=this&&serverThread.isAlive()){
serverThread.dos.writeUTF("PEOPLE_LEAVE:"+this._name);
}
}catch(Exception e){
}
}
try{
if (peopleList.containsKey(_name)) {
peopleList.remove(_name);
}
}catch(Exception e){
}
//System.out.println();
//ioe.printStackTrace();
try{
this.socket.close();
this.dos.flush();
this.dos.close();
this.dis.close();
break;
}catch(Exception e){
//System.out.println("serverthread");
//e.printStackTrace();
}
}
}
}
/* public static void main(String[] args) {
}*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -