⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 server.java

📁 JAVA的高级应用以及网络编程
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/** =================================================== 
* Title: Server.java  
* Created: [2007-6-11 20:18:53] by  作者 
*=====================================================
* Copyright:  Copyright (c) 成都信息工程学院, 2007-2008
* =====================================================
* Description: 本程序包括对Server类和ServerThread类的定义
*=====================================================
*Modification Hisytory: 
*1. 2007-6-11,作者创建了此文件
*
*/ 

import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.Vector;

/** 
*<p> ServerThread</p> 
* 
* 为客户端服务功能,包括:
* 登录验证
* 新建用户
* 好友处理(查找好友,读取资料,添加好友,添加好友应答,删除好友)
* 退出下线
* @author 
* @version 
*/ 
class ServerThread extends Thread{//继承线程
	private Socket socket;//定义套接口
	private BufferedReader in;//定义输入流
	private PrintWriter out;//定义输出流
	int no;//定义申请的icq号码
	public ServerThread(Socket s) throws IOException {//线程构造函数
		socket=s;//取得传递参数
		in=new BufferedReader(new InputStreamReader(socket.getInputStream()));//创建输入流
		out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);//创建输出流
		start();//启动线程
	}

/** 
* run方法的功能描述 
* 
* @param
* @return
* @throws
*/ 
	public void run(){//线程监听函数
		try{ while(true){
			String str=in.readLine();//取得输入字符串
			if(str.equals("end"))break;//如果是结束就关闭连接
			else if(str.equals("login")) {//如果是登录请求
			//*********以下为处理客户的登录请求 *********   
			try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//连接数据库
				Connection c=DriverManager.getConnection("jdbc:odbc:javaicq","","");
				String sql="select nickname,password from icq where icqno=?";                                                               
				//准备从数据库选择呢称和密码                                                            
				PreparedStatement prepare=c.prepareCall(sql);//设定数据库查寻条件
				String icqno=in.readLine();
				int g=Integer.parseInt(icqno);//取得输入的jicq号码
				System.out.println(icqno);
				String passwd=in.readLine().trim();//取得输入的密码
				System.out.println(passwd);
				prepare.clearParameters();
				prepare.setInt(1,g);//设定参数
				ResultSet r=prepare.executeQuery();//执行数据库查寻
				if(r.next()){//以下比较输入的号码于密码是否相同
					String pass=r.getString("password").trim();
					System.out.println(pass);
					if(passwd.regionMatches(0,pass,0,pass.length()))
					{ out.println("ok");
					//如果相同就告诉客户ok
					//并且更新数据库用户为在线
					//以及注册用户的ip 地址
					//*************register ipaddress
					String setip="update icq set ip=? where icqno=?";
					PreparedStatement prest=c.prepareCall(setip);
					prest.clearParameters();
					prest.setString(1,socket.getInetAddress().getHostAddress());
					prest.setInt(2,g);
					int set=prest.executeUpdate();
					System.out.println("ip="+socket.getInetAddress().getHostAddress()+"   "+set);
					//*************ipaddress
					//set status online
					String status="update icq set status=1 where icqno=?";
					PreparedStatement prest2=c.prepareCall(status);
					prest2.clearParameters();
					prest2.setInt(1,g);
					int set2=prest2.executeUpdate();
					System.out.println("status = 1  "+set2);
					//set online
					}
					//否者告诉客户失败
					else out.println("false");r.close();c.close();
				}
				else{ out.println("false");
				System.out.println("false");
				r.close();
				c.close();}
               }catch (Exception e){e.printStackTrace();}
				socket.close();
               }//end login
            //登录结束
			//*********以下为处理客户的新建请求  *********  
			else  if(str.equals("new")){
			try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//连接数据库
				Connection c2=DriverManager.getConnection("jdbc:odbc:javaicq","sa","shift");
				String newsql="insert into icq(nickname,password,email,info,place,pic) values(?,?,?,?,?,?)";
				//准备接受用户的呢称,密码,email,个人资料,籍贯,头像等信息
				PreparedStatement prepare2=c2.prepareCall(newsql);
				String nickname=in.readLine().trim();
				String password=in.readLine().trim();
				String email=in.readLine().trim();
				String info=in.readLine().trim();
				String place=in.readLine().trim();
				int picindex=Integer.parseInt(in.readLine());
				prepare2.clearParameters();
				prepare2.setString(1,nickname);
				prepare2.setString(2,password);
				prepare2.setString(3,email);
				prepare2.setString(4,info);
				prepare2.setString(5,place);
				prepare2.setInt(6,picindex);
				int r3=prepare2.executeUpdate();//执行数据库添加
				String sql2="select icqno from icq where nickname=?";
				//以下告诉客户其注册的号码
				PreparedStatement prepare3=c2.prepareCall(sql2);
				prepare3.clearParameters();
				prepare3.setString(1,nickname);
				ResultSet r2=prepare3.executeQuery();
				while(r2.next()){
				//out.println(r2.getInt(1));
				no=r2.getInt(1);
				System.out.println(no);
				}
				out.println(no);
				out.println("ok");
				c2.close();
				//完毕
			}catch (Exception e){e.printStackTrace();out.println("false");}
				socket.close();
			}//end new
			//新建用户结束
			
			//**********以下处理用户查找好友*********
			else if(str.equals("find")){
			try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
				Connection c3=DriverManager.getConnection("jdbc:odbc:javaicq","sa","shift");
				//以下连接数据库,并且返回其他用户的呢称,性别,籍贯,个人资料等信息
				String find="select nickname,sex,place,ip,email,info from icq";
				Statement st=c3.createStatement();
				ResultSet result=st.executeQuery(find);
				while(result.next()){
					out.println(result.getString("nickname"));
					out.println(result.getString("sex"));
					out.println(result.getString("place"));
					out.println(result.getString("ip"));
					out.println(result.getString("email"));
					out.println(result.getString("info"));
				}//while end
				out.println("over");
				////////GET ICQNO
				int d,x;
				boolean y;
				//以下返回用户的icq号码,头像号,及是否在线
				ResultSet iset=st.executeQuery("select icqno,pic,status from icq");
				while(iset.next()){
					d=iset.getInt("icqno");
					out.println(d);
					x=iset.getInt("pic");//pic info
					out.println(x);
					y=iset.getBoolean("status");
					if (y){out.println("1");}
					else {out.println("0");}
				//System.out.println(d);
				}
				// end send icqno
				iset.close();
				/////////icqno end
				c3.close();result.close();
			}catch (Exception e){e.printStackTrace();System.out.println("false");}
			//socket.close();
			}//end find
			
			//查找好友结束
			//**********以下处理用户登录时读取其好友资料**********
			else if(str.equals("friend")){
			try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
				Connection c4=DriverManager.getConnection("jdbc:odbc:javaicq","sa","shift");
				//以下连接好友表,返回用户的好友名单
				String friend="select friend from friend where icqno=?";
				PreparedStatement prepare4=c4.prepareCall(friend);
				prepare4.clearParameters();
				int icqno=Integer.parseInt(in.readLine());
				System.out.println(icqno);
				prepare4.setInt(1,icqno);
				ResultSet r4=prepare4.executeQuery();
				Vector friendno=new Vector();//该矢量保存好友号码
				while(r4.next()){
				friendno.add(new Integer(r4.getInt(1)));
				}
				//read friend info
				//以下告诉客户其好友的呢称,号码,ip地址,状态,头像,个人资料等信息
				out.println(friendno.size());
				for(int i=0;i<friendno.size();i++){
					String friendinfo="select nickname,icqno,ip,status,pic,email,info from icq where icqno=?";
					PreparedStatement prepare5=c4.prepareCall(friendinfo);
					prepare5.clearParameters();
					prepare5.setObject(1,friendno.get(i));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -