📄 server.java.bak
字号:
out.println("1");
else {out.println("0");}
out.println(rr.getString("EMail").trim( ));
out.println(rr.getString("HomePage").trim( ));
out.println(rr.getString("Phone").trim());
out.println(rr.getString("Resume").trim());
}
else{
out.println("FALSE");
out.println("好友数据不存在!");
String delFriendStr="DELETE Friend WHERE ImNo=?AND Friend=?";
prepare=c.prepareCall(delFriendStr);
prepare.clearParameters( );
prepare.setInt(1,myImNo);
prepare.setInt(2,friendImNo);
r=prepare.executeUpdate( );
}
rr.close( );
out.println("OVER");
}
c.close( );
}
catch(Execption e)
{
e.printStackTrace( );
}
}
//
public void processDelFriend( )
{
try
{
Connection c=DriverManager.getConnection(odbcDnsStr,user,password);
int friendImNo=Integer.parseInt(in.readLine( ));
int myImNo=Integer.parseInt(in.readLine( ));
String sqlStr="DELETE FROM Friend WHERE ImNo=? and Friend=?";
PreparedStatement prepare=c.prepareCall(sqlStr);
prepare.clearParameters( );
prepare.setInt(1,myImNo);
prepare.setInt(2,friendImNo);
int r=0;
r=prepare.executeUpdate( );
c.close( );
}
catch (Exception e)
{
e.printStackTrace( );
}
}
public void processLogout( )
{
try
{
Connection c=DriverManager.getConnection(odbcDnsStr,user,password);
int ImNo=Integer.parseInt(in.readLine( ));
String Online="UPDATE Im SET Online=0,IP='',RPort=-1 WHERE ImNo=?";
PreparedStatement prep=c.prepareCall(Online);
prep.clearParameters( );
prep.setInt(1,ImNo);
int r=prep.executeUpdate( );
c.close( );
}
catch (Exception e)
{
e.printStackTrace( );
}
//
public void processClientPort( )
{
try
{
Connection c=DriverManager.getConnection(odbcDnsStr,user,password);
int RPort=Integer.parseInt(in.readLine( ));
int ImNo=Integer.parseInt(in.readLine( ));
String Online="UPDATE Im SET RPort=?WHERE ImNo=?";
PreparedStatement prep=c.prepareCall(Online);
prep.clearParameters( );
prep.setInt(1,RPort);
prep.setInt(2,ImNo);
int r=prep.executeUpdate( );
c.close( );
}
catch (Exception e)
{
e.printStackTrace( );
}
}
//
public void processGetWhoAddMe()
{
try
{
Connection c=DriverManager.getConnection(odbcDnsStr,user,password);
int myImNo=Integer.parseInt(in.readLine( ));
String sqlStr="SELECT Ip,RPort,ImNo FROM Im WHERE (ImNo IN(SELECT ImNo FROM Friend WHERE Friend=?))and Online=1";
PreparedStatement prepare=c.prepareCall(sqlStr);
prepare.clearParameters( );
prepare.setInt(1,myImNo);
ResultSet r=prepare.executeQuery( );
while(r.next( ))
{
out.println(r.getString("Ip").trim( ));
out.println(r.getInt("RPort"));
out.println(r.getInt("ImNo"));
}
r.close( );
out.println("OVER");
c.close( );
}
catch (Exception e)
{
e.printStackTrace( );
out.println("OVER");
}
}
//
public void run()
{
try
{
while(true)
{
String str=in.readLine( );//取得输入字符串
System.out.println("Process"+str+"...");
if(str.equals("END")) break;//如果是结束就关闭连接
else if(str.equals("NEW"))
processNew( );
else if(str.equals("LOGIN"))
processLogin( );
else if(str.equals("CLIENTPORT"))
processClientPort( );
else if(str.equals("FRIEND"))
processFriend( );
else if(str.equals("ADDNEWFRIEND"))
processAddNewFriend( );
else if(str.equals("DELFRIEND"))
processDelFriend( );
else if(str.equals("GETWHOADDME"))
processGetWhoAddMe( );
else if(str.equal("LOGOUT"))
processLogout( );
}
System.out.println("Connection close:"+this.socket);
}
catch(IOException e){}
finally
{
try
{Socket.close( );}
catch(IOException e){}
}
}
}
//
public class Server//主服务器类
{
static String jdbcDrvstr="sun.jdbc.odbc.JdbcOdbcDriver";
//JDBC-ODBC桥驱动程序类
InetAddress IP;
static int serverPort=8080;
String dbServerUser;
String dbServerPassword;
public Server( )
{
ip=getHostIp( );
getDbServerUser( );
try
{
Class.forName(jdbcDrvStr);
ServerSocket s=new ServerSocket(serverPort,100,ip) ;
System.out.println("Server starting in"+ip.getHostAddress( )+"......");
while(true)
{
Socket socket=s.accept( );
System.out.println("Connection accept:"+socket);
try
{
new ServerThread(dbServerUser,dbServerPassword,socket);
}
catch(IOException e)
{
socket.close( );
}
}
}
catch(Exception e1)
{
e1.printStackTrace( );
}
}
public static InetAddress getHostIp( )
{
InetAddress ip=null;
try
{
Enumeration netInterfaces=NetworkInterface.getNetworkInterfaces( );
while(netInetfaces.hasMoreElements( ))
{
NetworkInterface ni=(NetworkInterface)netInterfaces.nextElement( );
ip=(InetAddress)ni.getInetAddresses( ).nextElement( );
if(!ip.isSiteLocalAddress( )&&! ip.isLoopbackAddress( )
&&ip.getHostAddree( ).indexOf(":")==-1)
{
break;
}
else
{
ip=InetAddress.getLocalHost( );
}
}
}
catch(Exception e1)
{
e1.printStackTrace( );
}
return ip;
}
public void getDbServerUser( )
{
try
{
File fileConfig=new File("config.txt");
BufferReader br=new BufferReader(new FileReader(fileConfig));
dbServerUser=br.readLine( ).substring(5);
dbServerPassword=br.readLine( ).substring(9);
}
catch(Exception e1)
{
dbServerUser="sa";
dbServerPassword="";
}
}
public static void main (String args[ ])throws IOException
{
Server jimServer=new Server( );
}
}//服务器程序结束
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -