(1)用戶的基本信息表(表名icq)
序號 字段名 含義 數(shù)據(jù)類型 NULL
1 Icqno 用戶的號碼 Int No
2 Nickname 用戶的呢稱 Char No
3 Password 用戶的密碼 Char No
4 Status 用戶在線否 Bit No
5 Ip 用戶的IP地址 Char Yes
6 Info 用戶的資料 Varchar Yes
7 Pic 用戶的頭像號 Int Yes
8 Sex 用戶性別 Char Yes
9 Email 用戶的email Char Yes
10 Place 用戶的籍貫 Char yes
其中Icqno字段為自動增加。(其他還可以添加諸如電話號碼等字段作為更多選擇)
(2)用戶的好友表(表名friend)
序號 字段名 含義 數(shù)據(jù)類型 NULL
1 Icqno 用戶的號碼 Int No
2 Friend 好友的號碼 Int No
1. 服務器程序:
服務器與客戶間通過套接口Socket(TCP)連接。
在java中使用套接口相當簡單,Java API為處理套接口的通信提供了一個類java.net.Socket.
,使得編寫網(wǎng)絡應用程序相對容易.服務器采用多線程以滿足多用戶的請求,通過JDBC與后臺數(shù)據(jù)庫連接,
并通過創(chuàng)建一個ServerSocket對象來監(jiān)聽來自客戶的連接請求,默認端口為8080,
然后無限循環(huán)調(diào)用accept()方法接受客戶程序的連接
服務器程序代碼如下:(部分)
import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.Vector;
class ServerThread extends Thread{//繼承線程
private Socket socket;//定義套接口
private BufferedReader in;//定義輸入流
private PrintWriter out;//定義輸出流
int no;//定義申請的jicq號碼
public ServerThread(Socket s) throws IOException {//線程構造函數(shù)
socket=s;//取得傳遞參數(shù)
in=new BufferedReader(new InputStreamReader(socket.getInputStream()));//創(chuàng)建輸入流
out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);//創(chuàng)建輸出流 start();//啟動線程
}
public void run(){//線程監(jiān)聽函數(shù)
try{
while(true)
{ String str=in.readLine();//取得輸入字符串
if(str.equals("end")) break;//如果是結(jié)束就關閉連接
else if(str.equals("login"))
{//如果是登錄
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//連接數(shù)據(jù)庫
Connection c=DriverManager.getConnection("jdbc:odbc:javaicq"," "," ");
String sql="select nickname,password from icq where icqno=?"; //準備從數(shù)據(jù)庫選擇呢稱和密碼 PreparedStatement prepare=c.prepareCall(sql);//設定數(shù)據(jù)庫查尋條件
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);//設定參數(shù)
ResultSet r=prepare.executeQuery();//執(zhí)行數(shù)據(jù)庫查尋
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//并且更新數(shù)據(jù)庫用戶為在線
//以及注冊用戶的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(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(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 //登錄結(jié)束 //以下為處理客戶的新建請求
else if(str.equals("new")){
try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//連接數(shù)據(jù)庫
Connection c2=DriverManager.getConnection("jdbc:odbc:javaicq"," "," ");
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();//執(zhí)行數(shù)據(jù)庫添加
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 //新建用戶結(jié)束 //以下處理用戶查找好友
else if(str.equals("find")){
try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c3=DriverManager.getConnection("jdbc:odbc:javaicq"," "," ");
//以下連接數(shù)據(jù)庫,并且返回其他用戶的呢稱,性別,籍貫,個人資料等信息
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;
//以下返回用戶的jicq號碼,頭像號,及是否在線
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 jicqno
iset.close(); /////////icqno end
c3.close();
result.close(); }
catch (Exception e){
e.printStackTrace();
System.out.println("false");} //socket.close(); }//end find
//查找好友結(jié)束 //以下處理用戶登錄時讀取其好友資料
else if(str.equals("friend")){
try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c4=DriverManager.getConnection("jdbc:odbc:javaicq"," "," ");
//以下連接好友表,返回用戶的好友名單
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地址,狀態(tài),頭像,個人資料等信息
out.println(friendno.size());
for(int i=0;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));
ResultSet r5=prepare5.executeQuery();
boolean status;
while(r5.next()){
out.println(r5.getString("nickname"));
out.println(r5.getInt("icqno"));
out.println(r5.getString("ip"));
status=r5.getBoolean("status");
if (status) out.println("1");
else {out.println("0");}
out.println(r5.getInt("pic"));
out.println(r5.getString("email"));
out.println(r5.getString("info")); } //while
r5.close();}//for//發(fā)送完畢
out.println("over");
System.out.println("over");
c4.close();
r4.close(); }
catch (Exception e){
e.printStackTrace();
System.out.println("false");} //socket.close(); }//end friend //讀取好友信息完畢
//以下處理用戶添加好友
else if(str.equals("addfriend")){
System.out.println("add");
try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c6=DriverManager.getConnection("jdbc:odbc:javaicq"," "," "); //連接數(shù)據(jù)庫,根據(jù)接受的用戶號碼及好友號碼向好友表添加記錄
int friendicqno=Integer.parseInt(in.readLine());
System.out.println(friendicqno);
int myicqno=Integer.parseInt(in.readLine());
System.out.println(myicqno);
String addfriend="insert into friend values(?,?)";
PreparedStatement prepare6=c6.prepareCall(addfriend);
prepare6.clearParameters();
prepare6.setInt(1,myicqno);
prepare6.setInt(2,friendicqno);
int r6=0; r6=prepare6.executeUpdate();
if(r6==1) System.out.println("ok addfrien");
else System.out.println("false addfriend"); }
catch (Exception e){
e.printStackTrace();
System.out.println("false");} //socket.close();
System.out.println("over addfriend"); }//end addfriend //用戶添加好友結(jié)束 //add new friend who add me
//以下處理其他用戶如果加我,我就加他
else if(str.equals("addnewfriend")){
System.out.println("add");
try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c6=DriverManager.getConnection("jdbc:odbc:javaicq"," "," "); //連接數(shù)據(jù)庫,根據(jù)接受的用戶號碼及好友號碼向好友表添加記錄
int friendicqno=Integer.parseInt(in.readLine());
System.out.println(friendicqno);
int myicqno=Integer.parseInt(in.readLine());
System.out.println(myicqno);
String addfriend="insert into friend values(?,?)";
PreparedStatement prepare6=c6.prepareCall(addfriend);
prepare6.clearParameters();
prepare6.setInt(1,myicqno);
prepare6.setInt(2,friendicqno);
int r6=0;
r6=prepare6.executeUpdate();
if(r6==1) System.out.println("ok addfrien");
else System.out.println("false addfriend");
String friendinfo="select nickname,icqno,ip,status,pic,email,info from icq where icqno=?"; //如果成功,就向用戶傳遞好友的基本信息,比如呢稱等
PreparedStatement prepare5=c6.prepareCall(friendinfo);
prepare5.clearParameters();
prepare5.setInt(1,friendicqno);
ResultSet r5=prepare5.executeQuery();
boolean status;
while(r5.next()){
System.out.println("dsf");
out.println(r5.getString("nickname"));
out.println(r5.getInt("icqno"));
out.println(r5.getString("ip"));
status=r5.getBoolean("status");
if (status) out.println("1");
else {out.println("0");}
out.println(r5.getInt("pic"));
out.println(r5.getString("email"));
out.println(r5.getString("info")); } //while out.println("over"); r5.close(); c6.close(); }
catch (Exception e){
e.printStackTrace();
System.out.println("false");}
System.out.println("over addnewfriend"); }//end addfriend //結(jié)束處理其他用戶如果加我,我就加他
//delete friend //以下執(zhí)行用戶刪除好友
else if(str.equals("delfriend")){
System.out.println("del");
try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c7=DriverManager.getConnection("jdbc:odbc:javaicq"," "," "); //連接數(shù)據(jù)庫,根據(jù)接受的用戶號碼及好友號碼向好友表刪除記錄
int friendicqno=Integer.parseInt(in.readLine());
System.out.println(friendicqno);
int myicqno=Integer.parseInt(in.readLine());
System.out.println(myicqno);
String addfriend="delete from friend where icqno=? and friend=?";
PreparedStatement prepare7=c7.prepareCall(addfriend);
prepare7.clearParameters();
prepare7.setInt(1,myicqno);
prepare7.setInt(2,friendicqno);
int r7=0;
r7=prepare7.executeUpdate();
if(r7==1) System.out.println("ok delfrien");//成功
else System.out.println("false delfriend");//失敗
}
catch (Exception e){e.printStackTrace();System.out.println("del false");}
}//end delete friend //執(zhí)行用戶刪除好友結(jié)束 //以下處理用戶退出程序
else if(str.equals("logout")){
try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c8=DriverManager.getConnection("jdbc:odbc:javaicq"," "," "); //連接數(shù)據(jù)庫,根據(jù)接受的用戶號碼,將其狀態(tài)字段設為0,及ip地址設為空
int myicqno=Integer.parseInt(in.readLine());
System.out.println(myicqno);
String status="update icq set status=0 , ip=′ ′ where icqno=?";
PreparedStatement prest8=c8.prepareCall(status);
prest8.clearParameters();
prest8.setInt(1,myicqno);
int r8=prest8.executeUpdate();
if(r8==1) System.out.println("ok logout");
else System.out.println("false logout"); }
catch (Exception e){e.printStackTrace();System.out.println("logout false");}
}//logout end //處理用戶退出程序結(jié)束 //get who add me as friend
//以下處理那些人加了我為好友,以便上線通知他們
else if(str.equals("getwhoaddme")){
System.out.println("getwhoaddme");
try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c9=DriverManager.getConnection("jdbc:odbc:javaicq"," "," "); //連接數(shù)據(jù)庫,根據(jù)我的號碼,從好友表中選擇誰加了我
int myicqno=Integer.parseInt(in.readLine());
System.out.println(myicqno);
String getwhoaddme="select icqno from friend where friend=?";
PreparedStatement prepare6=c9.prepareCall(getwhoaddme);
prepare6.clearParameters();
prepare6.setInt(1,myicqno);
ResultSet r6=prepare6.executeQuery();
Vector who=new Vector();
while(r6.next()){ who.add(new Integer(r6.getInt(1))); }//end while //然后告訴這些好友的ip地址,然后發(fā)給用戶以便告訴其他客戶我上線了
for(int i=0;i { String whoinfo="select ip from icq where icqno=? and status=1";
PreparedStatement prepare=c9.prepareCall(whoinfo);
prepare.clearParameters();
prepare.setObject(1,who.get(i));
ResultSet r=prepare.executeQuery();
while(r.next()){ out.println(r.getString("ip"));} //while
r.close(); }//for
out.println("over");
System.out.println("over");
c9.close();
r6.close(); }
catch (Exception e)
{e.printStackTrace();System.out.println("false");}
}//end get who add me as friend //處理上線結(jié)束
System.out.println("Echo ing :"+str); }
System.out.println("Close..."); }
catch(IOException e){}//捕或異常
finally {try{socket.close();}
catch(IOException e){} } }}
public class Server
{//主服務器類
public static void main(String args[])
throws IOException{ ServerSocket s=new ServerSocket(8080);//在8080端口創(chuàng)建套接口
System.out.println("Server start.."+s);
try{
while(true)
{Socket socket=s.accept();//無限監(jiān)聽客戶的請求
System.out.println("Connectino accept:"+socket);
try{new ServerThread(socket);}//創(chuàng)建新線程
catch(IOException e){socket.close();}
}
}
finally{s.close();}//捕或異常
}
}//服務器程序結(jié)束