📄 newchatserver.java
字号:
import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.*;
public class NewChatServer
{
private String m_dbDriver="sun.jdbc.odbc.JdbcOdbcDriver";
private String m_url="jdbc:odbc:jzz1";
private String user="";
private String password="";
private Connection conn=null;
private Statement stmt=null;
private ResultSet rs=null;
private Vector m_threads=null;
String sbroadcast="公告:局域网聊天";
public NewChatServer() throws Exception
{
Class.forName(m_dbDriver).newInstance();
conn=DriverManager.getConnection(m_url,user,password);
stmt=conn.createStatement();
}
public static void main(String[] args) throws Exception
{
NewChatServer ncs=new NewChatServer();
ServerSocket socket=new ServerSocket(9000);
ncs.m_threads=new Vector();
System.out.println("NewChatServerStart");
while(true)
{
Socket s=socket.accept();
ServerThread st=ncs.new ServerThread(s);
//ncs.m_threads.addElement(st);
}
// TODO 自动生成方法存根
}
class ServerThread implements Runnable
{
Socket m_socket=null;
PrintWriter out=null;
BufferedReader in=null;
String mid=null; //....
boolean state=true;
ServerThread(Socket s) throws Exception
{
m_socket=s;
out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(m_socket.getOutputStream())),true);
in=new BufferedReader(new InputStreamReader(m_socket.getInputStream()));
new Thread(this).start();
}
public void run()
{
String s=null;
try
{
continueBlock:
while(true)
{
s=null;
s=in.readLine();
System.out.println("s server="+s);
if(s==null) continue;
int i=0;
while(i<s.length()&&s.charAt(i)!='$')i++;
String stemp=s.substring(0,i);
if(stemp.equals("@login")) //
{
int j=++i;
while(i<s.length()&&s.charAt(i)!='$')i++;
String name=s.substring(j,i);
j=++i;
while(i<s.length()&&s.charAt(i)!='$')i++;
String psw=s.substring(j,i);
rs=null;
//System.out.println("select * from chatdb.dbo.chatuser where name='"+name+"';");
rs=stmt.executeQuery("select * from chatdb.dbo.chatuser where name='"+name+"';");
if(rs.next())
{
if(rs.getString("psw").trim().equals(psw))
{
boolean flag=false;
String nickname=rs.getString("nickname").trim();
String state=rs.getString("state").trim();
for(int k=0;k<m_threads.size();k++)
{
ServerThread st=(ServerThread)m_threads.elementAt(k);
//System.out.println(st.getID());
if(st!=this&&st.getID().equals(nickname)) //..
{
this.out.println("@errors$"+"改用户已经登陆!");
//state=false;
flag=true;
break ;
}
}
if(flag) continue ;
m_threads.addElement(this); //bug?
this.setID(nickname);
out.println("@loginsuccess$"+name+"$"+psw+"$");
if(state.equals("99"))
out.println("@seteditable$");
//System.out.println("@loginsuccess$");
for(int k=0;k<m_threads.size();k++)
{
ServerThread st=(ServerThread)m_threads.elementAt(k);
if(st!=this)
{
st.out.println("@say$"+this.getID()+" 进来了");
st.out.println("@addjlist$"+this.getID());
this.out.println("@addjlist$"+st.getID());
}
else
{
out.println("@say$"+"你的昵称是:"+this.getID());
}
}
this.out.println("@公告$"+sbroadcast);
}
else
{
out.println("@errors$"+"用户密码错误$");
//System.out.println("@error"+"用户密码错误");
}
}
else
{
out.println("@errors$"+"该用户不存在$");
//System.out.println("@errors"+"该用户不存在");
}
//this.out.println("@updatajlist$");
}
else if(stemp.equals("@register"))
{
int j=++i;
while(i<s.length()&&s.charAt(i)!='$')i++;
String name=s.substring(j,i);
j=++i;
while(i<s.length()&&s.charAt(i)!='$')i++;
String psw=s.substring(j,i);
j=++i;
while(i<s.length()&&s.charAt(i)!='$')i++;
String nickname=s.substring(j,i);
rs=null;
rs=stmt.executeQuery("select * from chatdb.dbo.chatuser where name='"+name+"'");
if(rs.next())
{
out.println("@errors$"+"该用户已经注册了");
}
else
{
ResultSet rs1=null;
rs1=stmt.executeQuery("select * from chatdb.dbo.chatuser where nickname='"+nickname+"'");
if(rs1.next())
{
out.println("@errors$"+"该昵称已经被使用了");
}
else
{
stmt.executeUpdate("insert into chatdb.dbo.chatuser values ('"
+name+"','"+psw+"','"+nickname+"',0)");
out.println("@registersuccess$"+name+"$"+psw); //...
}
}
}
else if(stemp.equals("@setnickname"))
{
int j=++i;
while(i<s.length()&&s.charAt(i)!='$')i++;
String name=s.substring(j,i);
j=++i;
while(i<s.length()&&s.charAt(i)!='$')i++;
String psw=s.substring(j,i);
j=++i;
while(i<s.length()&&s.charAt(i)!='$')i++;
String nickname=s.substring(j,i);
if(nickname.equals(this.getID()))
{
out.println("@errors$"+"你的昵称已经是:"+this.getID());
continue continueBlock;
}
ResultSet rs1=null;
rs1=stmt.executeQuery("select * from chatdb.dbo.chatuser where nickname='"+nickname+"'");
if(rs1.next())
{
out.println("@errors$"+"该昵称已经被使用了");
continue continueBlock;
}
String oldname=this.getID();
this.setID(nickname);//同步修改数据库,并通知所有在线user,和jlist
//System.out.println("update chatdb.dbo.chatuser set nickname='"+nickname+
// "' where name='"+name+"' and psw='"+psw+"'");
stmt.executeUpdate("update chatdb.dbo.chatuser set nickname='"+nickname+
"' where name='"+name+"' and psw='"+psw+"'");
for(int k=0;k<m_threads.size();k++)
{
ServerThread st=(ServerThread)m_threads.elementAt(k);
if(st!=this)
{
st.out.println("@say$"+oldname+" 改名为 :"+this.getID()+'$');
st.out.println("@alterjlist$"+oldname+"$"+this.getID()+'$');
}
else
{
this.out.println("@say$"+"你改名为 :"+this.getID()+'$');
this.out.println("@updatajlist$");
}
}
}
else if(stemp.equals("@say"))
{
int j=++i;
while(i<s.length()&&s.charAt(i)!='$')i++;
String sayobject=s.substring(j,i);
j=++i;
while(i<s.length()&&s.charAt(i)!='$')i++;
String saycontent=s.substring(j,i);
//saycontent=saycontent.replace('#','\n'); //
if(sayobject.equals("所有人"))
{
for(int k=0;k<m_threads.size();k++)
{
ServerThread st=(ServerThread)m_threads.elementAt(k);
st.out.println("@saytext$"+this.getID()+" 对大家说:"+saycontent);
}
}
else if(sayobject.equals(this.getID()))
{
out.println("@errors$"+"请不要自己对自己说,或者存在同名昵称");
}
else
{
for(int k=0;k<m_threads.size();k++)
{
ServerThread st=(ServerThread)m_threads.elementAt(k);
if(st.getID().equals(sayobject))
{
st.out.println("@saytext$"+this.getID()+" 对你说:"+saycontent);
this.out.println("@saytext$"+"你对"+st.getID()+"说:"+saycontent);
continue continueBlock; //.......
}
}
this.out.println("@errors$"+"该用户不在线,或者已经改名");
}
}
else if(stemp.equals("@公告"))
{
int j=++i;
while(i<s.length()&&s.charAt(i)!='$')i++;
String name=s.substring(j,i);
j=++i;
while(i<s.length()&&s.charAt(i)!='$')i++;
String stext=s.substring(j,i);
rs=stmt.executeQuery("select state from chatdb.dbo.chatuser where name='"+name+"'");
if(rs.next())
{
if(rs.getString("state").trim().equals("99"))
{
for(int k=0;k<m_threads.size();k++)
{
ServerThread st=(ServerThread)m_threads.elementAt(k);
st.out.println("@公告$"+stext);
}
sbroadcast=stext; //...
}
else
{
this.out.println("@errors$"+"该用户权限不够");
}
}
else
{
this.out.println("@errors$"+"找不到该用户");
}
}
else if(stemp.equals("@leave"))
{
break;
}
else
{
out.println("@errors$"+s);
}
}
}
catch(SQLException e1)
{
e1.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
finally //leave
{
if(this.getID()==null)
{
}
else
{
for(int k=0;k<m_threads.size();k++)
{
ServerThread st=(ServerThread)m_threads.elementAt(k);
//System.out.println("st.id="+st.getID());
if(st!=this)
{
st.out.println("@say$"+this.getID()+"走了~~");
st.out.println("@removejlist$"+this.getID());
}
}
}
m_threads.removeElement(this);
try
{
in.close();
out.close();
m_socket.close();
}
catch(Exception ee)
{
ee.printStackTrace();
}
}
}
private void setID(String s)
{
mid=s;
}
private String getID()
{
return mid;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -