基于JAVA的ICQ系統(tǒng)的設(shè)計于實現(xiàn)_第1頁
基于JAVA的ICQ系統(tǒng)的設(shè)計于實現(xiàn)_第2頁
基于JAVA的ICQ系統(tǒng)的設(shè)計于實現(xiàn)_第3頁
基于JAVA的ICQ系統(tǒng)的設(shè)計于實現(xiàn)_第4頁
基于JAVA的ICQ系統(tǒng)的設(shè)計于實現(xiàn)_第5頁
已閱讀5頁,還剩11頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、 . . . 基于JAVA的ICQ系統(tǒng)的設(shè)計于實現(xiàn)容:一序言二設(shè)計三程序界面四程序的使用參考資料關(guān)于作者何剛 (he_gumchinaren.)2001 年 10 月分析ICQ系統(tǒng),并嘗試用Java編寫。一序言ICQ是英文"I seek you"的簡稱,中文意思是我找你。ICQ最大的功能就是即時信息交流,只要記得對方的,上網(wǎng)時可以呼他,無論他在哪里,只要他上網(wǎng)打開ICQ,人們就可以隨時交流。ICQ源于以色列特拉維夫的Mirabils公司。該公司成立于年月,也就是在這個時候,互聯(lián)網(wǎng)上最出名,下載使用人數(shù)最多的免費軟件ICQ誕生了??赡苁瞧洳粩嘣黾拥挠脩艉蛷V闊的前景以與廣泛的應(yīng)

2、用前景和巨大的市場潛力,Mirabils的ICQ最終被美國在線AOL收購。由于ICQ的成功,推動了ICQ的本土化,就中文的ICQ而言,現(xiàn)在已經(jīng)越來越多,比如著名的騰迅公司推出的OICQ(現(xiàn)在由于問題,已改名為 2001),還有由TOM 推出的Tomq等,這些軟件技術(shù)都很好,而且簡單易用,成為中國網(wǎng)民最喜歡的通信軟件。但是這些公司都只提供軟件的客戶端程序免費下載,而不提供其服務(wù)器程序,因此對于未與互聯(lián)網(wǎng)連接的私有網(wǎng)絡(luò),這些軟件就用不上了。當(dāng)然網(wǎng)上也有免費的類似ICQ的服務(wù)器提供下載,但是好多都不提供源程序,即使有,其說明也很簡單,我很想知道它是怎么回事,所以我就試著做了。二設(shè)計為什么選擇JAVA

3、?Java是Sun Microsystem公司的JamesGosling開發(fā)的編程語言。它以C+為基礎(chǔ),但是卻是一個全新的軟件開發(fā)語言。Java是一個簡單,面象對象,分布式,解釋性,強壯,安全,與系統(tǒng)無關(guān),可移植,高性能,多線程和動態(tài)的語言-這是Sun給Java的定義。Sun公司的口號就是"網(wǎng)絡(luò)就是計算機",Java能使所有東西從桌面計算平穩(wěn)的轉(zhuǎn)變?yōu)榛诰W(wǎng)絡(luò)的計算,它是專門為此而建立的,并顯然是為了完成這個任務(wù)而來的。使用Java,我們可以相對輕松的一天編寫一個有條理的網(wǎng)絡(luò)程序。今天,Java的網(wǎng)絡(luò)功能正在飛躍發(fā)展,不斷有新的特性增加到這個有價值的基礎(chǔ)上,JavaSoft實

4、驗室正在不斷努力使Java更加完善。2數(shù)據(jù)庫設(shè)計系統(tǒng)可以采用任何一種流行的,Java支持的數(shù)據(jù)庫,本系統(tǒng)采用了Microsoft公司的SQLServer2000作為后臺數(shù)據(jù)庫。通過對現(xiàn)在流行的一些Icq的參考,建立數(shù)據(jù)庫,名為javaicq,數(shù)據(jù)庫共建立兩個表,一個是用戶的基本信息,包括呢稱,Jicq等。一個是用戶的好友表,包括用戶自己的和好友的。(1)用戶的基本信息表(表名icq)序號字段名含義數(shù)據(jù)類型NULL1Icqno用戶的IntNo2Nickname用戶的呢稱CharNo3Password用戶的密碼CharNo4Status用戶在線否BitNo5Ip用戶的IP地址CharYes6Inf

5、o用戶的資料VarcharYes7Pic用戶的頭像號IntYes8Sex用戶性別CharYes9Email用戶的emailCharYes10Place用戶的籍貫Charyes其中Icqno字段為自動增加。(其他還可以添加諸如等字段作為更多選擇)(2)用戶的好友表(表名friend)序號字段名含義數(shù)據(jù)類型NULL1Icqno用戶的IntNo2Friend好友的IntNo3系統(tǒng)模式與程序(具體程序參看源程序)系統(tǒng)采用客戶/服務(wù)器摸式(如圖)1. 服務(wù)器程序:服務(wù)器與客戶間通過套接口Socket(TCP)連接。在java中使用套接口相當(dāng)簡單,JavaAPI為處理套接口的通信提供了一個類.Socket

6、.,使得編寫網(wǎng)絡(luò)應(yīng)用程序相對容易服務(wù)器采用多線程以滿足多用戶的請求,通過JDBC與后臺數(shù)據(jù)庫連接,并通過創(chuàng)建一個ServerSocket對象來監(jiān)聽來自客戶的連接請求,默認端口為8080,然后無限循環(huán)調(diào)用accept()方法接受客戶程序的連接服務(wù)器程序代碼如下:(部分)import java.io.*; import .*; import java.sql.*; import java.util.Vector;class ServerThread extends Thread/繼承線程private Socket socket;/定義套接口private BufferedReader in;/定

7、義輸入流private PrintWriter out;/定義輸出流int no;/定義申請的jicqpublic ServerThread(Socket s) throws IOException /線程構(gòu)造函數(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)建輸出流sta

8、rt();/啟動線程 public void run()/線程監(jiān)聽函數(shù) try while(true)String str=in.readLine();/取得輸入字符串if(str.equals("end")break;/如果是結(jié)束就關(guān)閉連接else if(str.equals("login") /如果是登錄try Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");/連接數(shù)據(jù)庫 Connection c=DriverManager.getConnection("jdbc:odbc:j

9、avaicq"," "," "); String sql="select nickname,password from icq where icqno=?"/準備從數(shù)據(jù)庫選擇呢稱和密碼PreparedStatement prepare=c.prepareCall(sql);/設(shè)定數(shù)據(jù)庫查尋條件 String icqno=in.readLine();int g=Integer.parseInt(icqno);/取得輸入的jicq System.out.println(icqno);String passwd=in.readLi

10、ne().trim();/取得輸入的密碼 System.out.println(passwd); prepare.clearParameters();prepare.setInt(1,g);/設(shè)定參數(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

11、.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 se

12、t=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); /s

13、et 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 Cla

14、ss.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,個人資料,籍貫,頭像等信息 Prep

15、aredStatement 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();

16、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=?"/以下告訴客戶其注冊的 Prepared

17、Statement 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

18、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"," "," ");/以下連

19、接數(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"); ou

20、t.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=s

21、t.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

22、.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.JdbcOdb

23、cDriver"); 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.par

24、seInt(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<

25、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); ResultSet r5=prepare5.executeQuery(); boolean status; while

26、(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

27、.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();

28、 /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ù)接受的用戶與好友向好友表添加記錄

29、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(); prep

30、are6.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(); Sy

31、stem.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(

32、"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(?,?)" P

33、reparedStatement 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 frien

34、dinfo="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() Sy

35、stem.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.getI

36、nt("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");

37、 /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"," "," ")

38、;/連接數(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(ad

39、dfriend); 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

40、.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)字段設(shè)為0,

41、與ip地址設(shè)為空 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

42、=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") S

43、ystem.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

44、); 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); /

45、end while/然后告訴這些好友的ip地址,然后發(fā)給用戶以便告訴其他客戶我上線了 for(int i=0;i<who.size();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(

46、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(&

47、quot;Echo ing :"+str); System.out.println("Close.");catch(IOException e)/捕或異常 finally trysocket.close(); catch(IOException e) public class Server/主服務(wù)器類 public static void main(String args)throws IOExceptionServerSocket s=new ServerSocket(8080);/在8080端口創(chuàng)建套接口 System.out.println("Se

48、rver start."+s); trywhile(true)Socket socket=s.accept();/無限監(jiān)聽客戶的請求 System.out.println("Connectino accept:"+socket);trynew ServerThread(socket);/創(chuàng)建新線程 catch(IOException e)socket.close(); finallys.close();/捕或異常 /服務(wù)器程序結(jié)束2. 客戶程序如下(部分)客戶通過Socket(InetAddress,port)建立與服務(wù)器的連接。服務(wù)器與客戶都通過構(gòu)造Buffer

49、edReader,PrintWriter來建立輸入輸出流,然后雙方通過該輸入輸出流來相互傳遞信息,一旦收到客戶方的連接請求,服務(wù)器accept()方法返回一個新建的Socket對象??蛻舳巳缓笙蚍?wù)器發(fā)送消息,比如注冊,登錄,查找好友等,服務(wù)器收到來自客戶的請求后,針對不同的消息處理請求,雖然UDP不可靠但是對于icq可靠性并不太重要,而且UDP快速,所以客戶間發(fā)送信息通過UDP。用戶登錄時通過類DatagramPacket和DatagramSocket創(chuàng)建UDP包括其本地接受端口以與發(fā)送端口,默認端口為5000和5001,通過取得的好友的IP地址來向好友發(fā)送消息(send(DatagramP

50、acket)和接受消息(receive(DatagramPacket)。當(dāng)用戶通過UDP收到消息后,可以通過DatagramPacket的方法InetAddressgetAddress()得到對方的ip地址,通過對好友列表比較以判斷是誰并提示用戶收到某某的消息,然后用戶選擇該用戶查看消息,如果好友列表沒有該人就顯示收到陌生人的消息。用戶可以按陌生人按鈕查看消息。1. 用戶注冊。當(dāng)服務(wù)器收到用戶的注冊請求,便開始接受客戶傳第的信息,諸如客戶的呢稱啦,性別,籍貫,頭像,個人資料等,接受完畢后,便通過JdbcOdbc與后臺數(shù)據(jù)庫連接,然后向數(shù)據(jù)庫添加記錄,如果成功,便向客戶返回其Jicq,并在數(shù)據(jù)庫

51、中注冊用戶的IP地址,然后更新其Status為1即用戶在線??蛻羰盏椒?wù)器返回的信息后,便打開主程序窗口,并同時開始創(chuàng)建UDP以便在用戶之間建立聯(lián)系。*部分程序如下: void utton1_mouseClicked(MouseEvent e) try Socket socket=new Socket(InetAddress.getByName(sername),serverport);/連接服務(wù)器 BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream(); PrintWriter ou

52、t=new PrintWriter(new BufferedWriter( new OutputStreamWriter(socket.getOutputStream(),true);out.println("new");/告訴服務(wù)器我要注冊out.println(nickname.getText().trim();/告訴服務(wù)器我的呢稱,密碼,email,資料out.println(password.getPassword();/以與頭像號等信息 out.println(email.getText().trim(); out.println(info.getText().t

53、rim(); out.println(place.getSelectedItem(); out.println(headpic.getSelectedIndex();/head picindex int no; no=Integer.parseInt(in.readLine(); /System.out.print(no);String str=" ";str=in.readLine().trim();/從服務(wù)器取得狀態(tài) if(str.equals("false")JOptionPane.showMessageDialog(this,"對不起,出錯了:- (","ok",JOptionPane.INFORMATION_MESSAGE);/失敗就警告els

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論