JAVA編寫QQ聊天系統(tǒng)指導(dǎo)書_第1頁
JAVA編寫QQ聊天系統(tǒng)指導(dǎo)書_第2頁
JAVA編寫QQ聊天系統(tǒng)指導(dǎo)書_第3頁
JAVA編寫QQ聊天系統(tǒng)指導(dǎo)書_第4頁
JAVA編寫QQ聊天系統(tǒng)指導(dǎo)書_第5頁
已閱讀5頁,還剩29頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、 . . . JAVA編寫 聊天系統(tǒng)指導(dǎo)書一、 聊天系統(tǒng)簡介1、 模擬聊天系統(tǒng)包含兩個方面,服務(wù)器端和客戶端。2、 服務(wù)器端任務(wù):建立一個ServerSocket,與客戶端建立通信通道,不斷偵聽是否有客戶端連接或者斷開連接。 服務(wù)器端是一個信息轉(zhuǎn)發(fā)中心,所有客戶端的信息都傳到服務(wù)器端,由服務(wù)器端根據(jù)要求分發(fā)信息。3、 客戶端任務(wù):建立一個Socket,與服務(wù)器端建立通信通道,向服務(wù)器端發(fā)送信息,并接收來自服務(wù)器的信息。二、 聊天系統(tǒng)測試1、教師機(jī)新建一個java project,命名為 chat,將“ 聊天系統(tǒng)素材”下服務(wù)器端解壓,將解壓后的兩個包c(diǎn)om,client復(fù)制到 chat/src下

2、,運(yùn)行Server.java。彈出如圖1-1所示圖形界面,點(diǎn)擊“開始”,將彈出如圖1-2所示圖形界面,服務(wù)器即打開。圖1-1 圖1-22、指導(dǎo)學(xué)生新建一個java project,命名為 chat,在“ 聊天系統(tǒng)素材”下載客戶端,將客戶端下載解壓,將解壓后的兩個包c(diǎn)om,client復(fù)制到 chat/src下,運(yùn)行Client.java,將彈出如圖1-3所示圖形界面,輸入服務(wù)器的ip,port,以與名(自已)登錄,將彈出如圖1-4所示圖形界面。點(diǎn)擊如圖1-5所示圖形界面的下拉列表,在發(fā)送框中輸入信息發(fā)送,就可以進(jìn)行群聊或私聊。圖1-3 圖1-4 圖1-54、 指導(dǎo)同學(xué)們下載服務(wù)器端,同學(xué)們輪流

3、運(yùn)行服務(wù)器程序,其它同學(xué)通過某個同學(xué)的服務(wù)器程序進(jìn)行聊天。不同的服務(wù)器以IP地址區(qū)別。需要提醒學(xué)生注意的是:只有登錄同一個服務(wù)器的客戶端才能相互聊天,測試時服務(wù)器端只能有一個,學(xué)生們需要輪流做服務(wù)器,而客戶端可以有多個,沒有限制。三、 聊天系統(tǒng)設(shè)計與實(shí)現(xiàn)操作步驟。F任務(wù)一 目的:通過網(wǎng)絡(luò)編程,讓同學(xué)們與老師互通字節(jié)信息。老師做服務(wù)器,學(xué)生做客戶端。具體操作步驟:教師與學(xué)生都新建一個java project,命名如zhangsan chat(以本人+ chat為文件名),在src下新建一個other包,教師在other包下新建Server.java類并運(yùn)行。指導(dǎo)學(xué)生在other包下新建Clien

4、t.java類并運(yùn)行,學(xué)生逐個與教師連接測試。通過測試,教師檢查學(xué)生完成情況并記錄。Server.java代碼如下:package other;import java.io.*;import .*;publicclass Server publicstaticvoid main(String args) ServerSocket server;try server=new ServerSocket(1234);Socket sk=server.accept();InputStream is=sk.getInputStream();int i;System.out.println("這

5、是服務(wù)器端接收到的信息");while(i=is.read()!=-1)System.out.print(char)i);server.close(); catch (IOException e) e.printStackTrace();Client.java代碼如下:package other;import java.io.*;import .*;publicclass Client publicstaticvoid main(String args) Socket client;try client=new Socket("localhost",1234);O

6、utputStream os=client.getOutputStream();os.write('z');os.write('h');os.write('a');os.write('n');os.write('s');bw.close();client.close(); catch (UnknownHostException e) e.printStackTrace(); catch (IOException e) e.printStackTrace();F任務(wù)二 目的:通過網(wǎng)絡(luò)編程,讓同學(xué)們與老師互通字節(jié)信息。

7、學(xué)生做服務(wù)器,老師做客戶端。指導(dǎo)學(xué)生在other包下新建Server.java類并運(yùn)行。教師在other包下新建Client.java類并運(yùn)行,逐個與學(xué)生連接測試,教師檢查學(xué)生完成情況并記錄。代碼同上。網(wǎng)絡(luò)編程總結(jié)如下:1、 在服務(wù)器端先定義一個Server.java類。在此類中的main方法中,創(chuàng)建一個ServerSocket 實(shí)例,指定打開一個端口,讓其它機(jī)器訪問。此ServerSocket 實(shí)例調(diào)用方法accept()來等待其它機(jī)器連接,若有連接返回一個Socket實(shí)例Socket實(shí)例可以調(diào)用getInputStream()方法獲取客戶端發(fā)來的信息。將這些信息輸出,關(guān)閉各種流與連接。2、

8、 在客戶端定義一個Client.java類。在此類中main方法中,通過對指定服務(wù)器端的連接獲取一個Socket實(shí)例調(diào)用這個Socket實(shí)例的getOutputStream()方法獲取一個輸出流,通過輸出流往服務(wù)器發(fā)送信息關(guān)閉各種流與連接3、 學(xué)生先跟老師通信,然后兩個同學(xué)一組,互相做客戶端與服務(wù)器端,理解網(wǎng)絡(luò)編程的基本知識。4、 由于沒有引入線程知識,在做服務(wù)器端時,每連接一次后,都要重新運(yùn)行Server.java。F任務(wù)三 目的:通過網(wǎng)絡(luò)編程,讓同學(xué)們與老師互通字符信息。將Server.java與 Client.java修改并運(yùn)行。通過教師與學(xué)生互動測試,教師檢查學(xué)生完成情況并記錄。Ser

9、ver.java代碼修改如下:package other;import java.io.*;import .*;publicclass Server publicstaticvoid main(String args) ServerSocket server;try server=new ServerSocket(1234);Socket sk=server.accept();/返回一個字節(jié)輸入流InputStream is=sk.getInputStream();/將字節(jié)流轉(zhuǎn)化為字符流InputStreamReader isr=new InputStreamReader(is);/將字符流轉(zhuǎn)

10、化為緩存流BufferedReader br=new BufferedReader(isr);String s=br.readLine();System.out.println("這是服務(wù)器端接收到的信息");System.out.println(s);br.close();server.close(); catch (IOException e) e.printStackTrace();Client.java代碼修改如下:package other;import java.io.*;import .*;publicclass Client publicstaticvoid

11、 main(String args) Socket client;try client=new Socket("localhost",1234);/獲取一個字節(jié)輸出流OutputStream os=client.getOutputStream();/將字節(jié)輸出流轉(zhuǎn)化為字符輸出流OutputStreamWriter osw=new OutputStreamWriter(os);/將字符輸出流轉(zhuǎn)化為緩存流BufferedWriter bw=new BufferedWriter(osw);bw.write("我是三,羅老師好");bw.close();clie

12、nt.close(); catch (UnknownHostException e) e.printStackTrace(); catch (IOException e) e.printStackTrace();F任務(wù)總結(jié):從上面的案例,在服務(wù)器端發(fā)現(xiàn)了一個缺點(diǎn),每次只能與一個客戶端連接,連接成功后就會退出。如何解決問題,在服務(wù)器端加上線程。F任務(wù)四 目的:客戶端代碼不變,在服務(wù)器端加上線程,要求服務(wù)器能與多個客戶端聯(lián)系。教師做服務(wù)器端,并啟動服務(wù)器端,與多個同學(xué)連接,通過教師與學(xué)生互動測試,教師檢查學(xué)生完成情況并記錄。public class Server public static voi

13、d main(String args) System.out.println("這是包含線程的服務(wù)器");ConServerThread cst=new ConServerThread();cst.startServer();new Thread(cst).start();public class ConServerThread implements Runnable/這是包含線程的服務(wù)器ServerSocket serverSocket;int count=0;public void startServer()try serverSocket=new ServerSock

14、et(8000); catch (IOException e) e.printStackTrace();public void run() try while(true)Socket clientSocket=serverSocket.accept();InputStream ins=clientSocket.getInputStream();/*以下三行代碼也可以接收字符流InputStreamReader isr=new InputStreamReader(ins);BufferedReader bfr=new BufferedReader(isr); String str= bfr.re

15、adLine();*/推薦使用下面方法接收字符DataInputStream dis=new DataInputStream(ins);String str=dis.readUTF();System.out.println("這是從客戶端傳過來的信息:"+str);count+;System.out.println("這是第"+count+"個連接客戶"); catch (IOException e) e.printStackTrace();F任務(wù)五 目的:客戶端代碼不變,在服務(wù)器端加上線程。學(xué)生做服務(wù)器端,并啟動服務(wù)器端,教師逐個與

16、每個同學(xué)連接,通過教師與學(xué)生互動測試,教師檢查學(xué)生完成情況并記錄。F任務(wù)總結(jié):流的知識回顧,BufferedWriter 中的方法BufferedWriter()有這個一樣缺點(diǎn),當(dāng)我們輸入文字太少的時候,如果緩存沒裝滿的時候,它不會發(fā)送出去,除非調(diào)用close方法。在實(shí)際操作中,同學(xué)們信息發(fā)送不成功的同學(xué)可能就是這個原因。這個方法不便于連續(xù)發(fā)送,所以采用DataInputStream(InputSream)來處理InputStream,調(diào)用他的readUTF()方法來讀,輸出流也一樣,調(diào)用writerUTF()方法來寫,它就不要求關(guān)閉,可以連續(xù)寫與讀容。另外從任務(wù)四中,在服務(wù)器端又發(fā)現(xiàn)了一個缺

17、點(diǎn),雖然可以與多個客戶端連接,但每次只能接收一句話。如何解決問題,在服務(wù)器端再加線程。F任務(wù)六 目的:客戶端代碼不變,在服務(wù)器端加上線程,要求完成服務(wù)器不僅要與多個客戶端連接,而且可以接受一個客戶的多條語句。教師做服務(wù)器端,并啟動服務(wù)器端,每個同學(xué)與服務(wù)器連接,通過教師與學(xué)生互動測試,教師檢查學(xué)生完成情況并記錄。publicclass Server publicvoid startServer()try serverSocket=new ServerSocket(8000);new Thread(new StartServerThread().start(); catch (IOExcepti

18、on e) e.printStackTrace();/下面這個線程類中類,能夠完成服務(wù)器可以與多個客戶端連接,不用線程的話與一個客戶端連接后就會結(jié)束程序。class StartServerThread implements Runnablepublicvoid run() while(true)try clientSocket=serverSocket.accept();new Thread(new ServerReadThread(clientSocket).start(); catch (IOException e) e.printStackTrace();/下面這個線程類中類,能夠完成服

19、務(wù)器可以可以接受一個客戶的多條信息。不用線程的話,服務(wù)器只能接受一條語句。class ServerReadThread implements RunnableSocket clientSocket;public ServerReadThread(Socket clientSocket) this.clientSocket=clientSocket;FFpublicvoid run() try InputStream ins=clientSocket.getInputStream(); DataInputStream dataIn = new DataInputStream(ins);while

20、(true)String str= dataIn.readUTF();System.out.println(str); catch (IOException e) e.printStackTrace();publicstaticvoid main(String args) new Server();F任務(wù)七 目的:客戶端代碼不變,在服務(wù)器端加上線程,要求完成服務(wù)器不僅要與多個客戶端連接,而且可以接受一個客戶的多條語句。學(xué)生做服務(wù)器端,并啟動服務(wù)器端,教師作為客戶端逐個與每個同學(xué)連接,通過教師與學(xué)生互動測試,教師檢查學(xué)生完成情況并記錄。F任務(wù)八 目的:在src/client,src/server

21、下創(chuàng)建圖形界面文件。其中圖1-6為文件GuiServer.java運(yùn)行結(jié)果,該文件在src/server下, 圖1-7為文件GuiClientMain.java運(yùn)行結(jié)果,圖1-8為文件CuiClient.java運(yùn)行結(jié)果,文件所在目錄結(jié)構(gòu)如圖1-9所示。圖1-6 圖1-7 圖1-8 圖1-9注意:學(xué)生完成了以上圖形界面類的創(chuàng)建,教師作為一次作業(yè)檢查,學(xué)生保存,由于圖形界面不是重點(diǎn),為便于邏輯的實(shí)現(xiàn),統(tǒng)一教學(xué)進(jìn)度,下面的圖形界面統(tǒng)一采用教師提供的文件,學(xué)生將老師提供的圖形界面復(fù)制到自已相應(yīng)的包下(圖形界面文件素材在“ 聊天系統(tǒng)素材”下的“圖形界面”下),如上圖1-9所示。學(xué)生可以在業(yè)余時間,采用

22、自已設(shè)計的圖形界面完成本項目。附三個文件的代碼GuiServer.java:public class GuiServer extends Frame /寫三個組件privateTextArea text = new TextArea("", 10,40);privateButton startButton = new Button("開始");private Button closeButton = new Button("結(jié)束");/寫了三個容器privatePanel p1 = new Panel();private Panel

23、p2 = new Panel();private Panel p3 = new Panel();public GuiServer() super("某某服務(wù)器");p1.add(startButton);p1.add(closeButton);p2.add(text);this.add(p2,BorderLayout.NORTH);this.add(p1,BorderLayout.CENTER);this.pack();this.setVisible(true);public class GuiClient extends Frameprivate Button sendB

24、utton;private Button closeButton;private TextArea receiveMsg ;private TextArea sendMsg ;private GuiClient frm;private Choice clientList;public GuiClient(String str)super(str);frm = this;clientList = new Choice();this.clientList.add("ALL ");receiveMsg = new TextArea(5 , 20);sendMsg = new Te

25、xtArea(5 , 20);this.sendMsg.setText("");this.receiveMsg.setText(""); this.sendButton = new Button("發(fā)送");this.closeButton = new Button("退出");Panel p = new Panel();Panel p1 = new Panel();Panel p2 = new Panel();Panel p3 = new Panel();Panel p4 = new Panel();frm.ad

26、d(p);p.setLayout(new BorderLayout();p.add(p1,"North");p.add(p2,"Center");p2.add(p4);p.add(p3,"South");p1.setLayout(new FlowLayout();p1.add(clientList);p4.setLayout(new GridLayout(2,1);p4.add(receiveMsg);p4.add(sendMsg);p3.setLayout(new FlowLayout();p3.add(this.sendButto

27、n);p3.add(this.closeButton);frm.add(p);/showFrame()方法的作用是將窗口顯示在指定的地方public void showFrame()Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();Dimension frameSize = frm.getSize();frm.setLocation(screenSize.width-frameSize.width-200)/2,(screenSize.height-frameSize.height-100)/2);frm.pa

28、ck();frm.setVisible(true);public void listenSendButton(final Client b) this.sendButton.addActionListener( new ActionListener() public void actionPerformed(ActionEvent e) b.send(); ); public void listenCloseButton(final Client b) this.closeButton.addActionListener( new ActionListener() public void ac

29、tionPerformed(ActionEvent e) b.quit(); ); public class GuiClientMain extends Frameprivate TextField address;private TextField port;private TextField userName;private Button loginButton ;privateButton qutiButton ;private Frame clientmainframe;public GuiClientMain(String str)super(str);clientmainframe

30、 = this;Label label1 = new Label("服務(wù)器 IP:");Label label2 = new Label("服務(wù)器port:");Label label3 = new Label(" 用戶名:");this.loginButton = new Button("登錄");this.qutiButton = new Button("取消");this.address = new TextField(null,20);this.port = new TextField(

31、null,20);this.userName = new TextField(null , 20);Panel p1 = new Panel();Panel p2 = new Panel();Panel p3 = new Panel();Panel p4 = new Panel();clientmainframe.setLayout(new GridLayout(4,1);p1.setLayout(new FlowLayout();p2.setLayout(new FlowLayout();p3.setLayout(new FlowLayout();p4.setLayout(new FlowL

32、ayout();p1.add(label1);p1.add(this.address);p2.add(label2);p2.add(this.port);p3.add(label3);p3.add(this.userName);p4.add(this.loginButton);p4.add(this.qutiButton);clientmainframe.add(p1);clientmainframe.add(p2);clientmainframe.add(p3);clientmainframe.add(p4);clientmainframe.addWindowListener(new Win

33、dowAdapter()public void windowClosing(WindowEvent e)System.exit(0););/showFrame()方法的作用是將窗口顯示在指定的地方public void showFrame()Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();Dimension frameSize = clientmainframe.getSize();clientmainframe.setLocation(screenSize.width-frameSize.width-200

34、)/2,(screenSize.height-frameSize.height-100)/2);clientmainframe.pack();clientmainframe.setVisible(true);public void listenLoginButton(final Client b)this.loginButton.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e) b.login(); );public void listenQuitButton(final Clien

35、t b)this.qutiButton.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e) System.exit(0); );F任務(wù)九 目的:利用圖形界面圖一中“開始”按鈕啟動服務(wù)器,“退出”按鈕關(guān)閉服務(wù)器。具體操作如下:1、 修改GuiServer.java中的開始按鈕監(jiān)聽的方法。代碼如下:publicvoidstartButListenter(final Server a)startButton.addActionListener(new ActionListener()publ

36、icvoid actionPerformed(ActionEvent e) text.setText("服務(wù)器開始工作了。");a.startServer(););這段代碼表達(dá)的是當(dāng)我們點(diǎn)擊了startButton,就跳轉(zhuǎn)到了Server.java中方法startServer()。2、 在Server.java中startServer()方法完成開啟服務(wù)器的工作。代碼如下:publicvoid startServer()try serverSocket=newServerSocket(8000);new Thread(new StartServerThread().star

37、t(); catch (IOException e) e.printStackTrace();3、 線程類StartServerThread的代碼如下:class StartServerThread implements Runnableintcount=0;publicvoid run() while(true)try Socket clientSocket=serverSocket.accept();InputStream ins=clientSocket.getInputStream();DataInputStream dataIn = new DataInputStream(ins);

38、String str= dataIn.readUTF();System.out.println("這是從客戶端傳過來的信息:"+str);count+;System.out.println("這是第"+count+"個連接客戶"); catch (IOException e) e.printStackTrace();4、 如何實(shí)現(xiàn)點(diǎn)擊退出按鈕,就關(guān)閉服務(wù)器?修改GuiServer.java中的退出按鈕監(jiān)聽的方法。publicvoid endButtonListenter(final Server a)closeButton.addAc

39、tionListener(new ActionListener()publicvoid actionPerformed(ActionEvent e) a.endServer(););這段代碼表達(dá)的是當(dāng)我們點(diǎn)擊了closeButton ,就跳轉(zhuǎn)到了Server.java中方法endServer ()。Server.java中方法endServer ()代碼如下:publicvoid endServer()System.exit(0);F任務(wù)十:客戶端利用圖形界面登錄到指定的服務(wù)器,并且發(fā)送消息給服務(wù)器。具體操作如下:1、 運(yùn)行Client.java,先調(diào)用構(gòu)造器,構(gòu)造器中調(diào)用按鈕監(jiān)聽,點(diǎn)擊登錄按

40、鈕,程序跳轉(zhuǎn)到Client.java中的login()方法,在login()中實(shí)現(xiàn)將圖形界面輸入的容打印輸出。2、 在GuiClientMain.java中加入三個方法,用于獲取三個文本框的值。代碼如下:public String getAddress() returnaddress.getText();publicint getPort() String port1=port.getText();int port2=Integer.parseInt(port1);return port2;public String getUserName() returnuserName.getText()

41、;3、Client.java中的login()方法如下:publicvoid login()try/首先要獲取登錄框中的容,即用戶輸入的數(shù)據(jù)String severIP=guiMain.getAddress();int serverPort=guiMain.getPort();userName=guiMain.getUserName();System.out.println("severIP="+severIP);System.out.println("serverPort="+serverPort);System.out.println("u

42、serName="+userName);client = new Socket(severIP,serverPort);System.out.println("與服務(wù)器連接成功。");ous=client.getOutputStream();DataOutputStream dataOut = new DataOutputStream(ous);dataOut.writeUTF("IP為"+severIP+" ; Port為"+serverPort+"的人在聯(lián)系你。");dataOut.writeUTF(

43、"用戶名是:"+userName);/這個IP前面帶一個斜桿/String clientIp=client.getLocalAddress().toString();/下面代碼將斜桿去掉。String clientIp=client.getLocalAddress().toString().substring(1);int clientPort=client.getLocalPort();System.out.println("我的IP是:"+clientIp);System.out.println("我的port是:"+client

44、Port);/登錄界面退出guiMain.dispose();/聊天界面顯示/調(diào)用send按鈕監(jiān)聽方法,那么點(diǎn)擊send按鈕就會跳到send()方法gui.listenSendButton(this);/點(diǎn)擊close按鈕就會跳到quit()方法gui.listenCloseButton(this);gui.setTitle(userName);gui.showFrame();gui.addWindowListener(new WindowAdapter()publicvoid windowClosing(WindowEvent e) quit(); );/new Thread(new Rea

45、dThread().start();catch(Exception e)System.out.println(e);System.out.println("連接有誤,請檢查服務(wù)器是否打開,或者服務(wù)器IP地址與端口號是否有誤!");publicvoid send()System.out.println("send");try String msg=gui.getSendMsg();System.out.println("msg="+msg);ous=client.getOutputStream();ous=client.getOutpu

46、tStream();DataOutputStream dataOut = new DataOutputStream(ous);dataOut.writeUTF(userName+"說:"+msg);gui.setSendMsgClear(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();思考:在 聊天系統(tǒng)中,服務(wù)器是如何知道是哪個客戶發(fā)來的信息呢?Socket中有兩個方法可以將連接服務(wù)器的客戶端的Ip和port獲取,這也是服務(wù)器知道是哪個客戶發(fā)過來信息的原因,也是后面服

47、務(wù)器將消息發(fā)送給某個客戶端的基礎(chǔ)。Socket實(shí)例兩個方法getLocalAddress(),與getLocalPort(),可以獲取客戶端的Ip和port獲取。clientSocket.getLocalAddress();clientSocket.getLocalPort();F任務(wù)十一:客戶端將自身的Ip和port獲取,當(dāng)與服務(wù)器成功連接后,將自身的Ip和port以一定的消息格式傳給服務(wù)器。具體代碼如下:/獲取客戶端的IP與portString clientIp=clientSocket1.getLocalAddress().toString().substring(1);int clie

48、ntPort=clientSocket1.getLocalPort();System.out.println("本客戶端IP為:"+clientIp);System.out.println("本客戶端Port為:"+clientPort);/注意這個clientIp前面有一個斜桿,還要處理下。/按消息規(guī)則發(fā)送消息String str="NEW/"+clientIp+"/"+clientPort+"/"+userName+"/"dos.writeUTF(str);F任務(wù)十二:實(shí)

49、現(xiàn)用聊天對話框的發(fā)送框向服務(wù)器發(fā)送信息。如下圖所示。分析聊天原理:與服務(wù)器連接成功的客戶,服務(wù)器端都能獲取它的IP與Port,每一個客戶的IP與Port都不會一樣,所以我們用Ip與Port可以區(qū)分每個客戶,這也是聊天系統(tǒng)成功的基本原理。所以在登錄的時候,就將這些信息發(fā)送給了服務(wù)器,讓服務(wù)器知道有哪些客戶上線了。服務(wù)器在轉(zhuǎn)發(fā)消息時,通過選擇客戶的IP與Port來發(fā)送給指定的客戶,作為客戶端,一要告訴服務(wù)器你本身的IP與Port,二要告訴服務(wù)器你的信息發(fā)給誰,也是通過IP與port來確定,所以說在login()方法中既要發(fā)送自已的IP與Port給服務(wù)器,在send()方法中,我們還要將接收方的IP

50、與Port發(fā)送服務(wù)器,服務(wù)器將如何辨認(rèn)哪個是你自已的IP與Port,哪個是你將要發(fā)送的客戶的IP與Port,那么你就要做個標(biāo)記。如你在登錄時發(fā)的,說明是你自已的IP與Port,前面加個前綴“NEW”,你在send()方法中發(fā)送的我們就加一個前綴“MSG”,服務(wù)器接收到以后,通過前綴就可以分類處理??偨Y(jié)聊天系統(tǒng)中消息的發(fā)送,都要發(fā)送IP,Port, 用戶名,消息,而且遵循整發(fā)散取的原則,意思是整合發(fā)過去,到了對方,都要打散它,為了便于整合與打散,所以我們定義一下消息格式,便于后面寫方法專門來處理整合、打散字符串。我們最多發(fā)送五個容給服務(wù)器,所以設(shè)計了下面這個格式:五個容之間用斜線分開,每次可以發(fā)

51、送小于或等于五個容的消息。如果小于五個容,在具體地方發(fā)送空格就行。消息格式規(guī)定為:head/+ip+/+port+/+usename+/+sendmsgF任務(wù)十三:客戶端登錄時將自已相關(guān)的信息發(fā)送給服務(wù)器,服務(wù)器將這些信息接到后,將這些信息拆開并輸出。當(dāng)前綴不同時按不同的方式處理。處理方式如下 :對于登錄時,head=”NEW”, ip=本客戶的IP, port=本客戶的Port,username=本客戶的用戶名,sendmsg=”;整發(fā):(客戶端)dos.writeUTF("NEW/"+clientIp+"/"+clientPort+"/&q

52、uot;+userName+"/");散取:(服務(wù)器)String str=dis.readUTF();System.out.println("來自客戶端的消息:"+str);int index1=str.indexOf("/",0);int index2=str.indexOf("/",index1+1);int index3=str.indexOf("/",index2+1);int index4=str.indexOf("/",index3+1);String head=str.substring(0,index1);System.out.println("head="+head);String clientIP=str.substring(index1+1,index2);System.out.println("clientIP="+clientIP);String clientPort=str.substring(index2+1,index3);System.out.println("clientPort="+clientP

溫馨提示

  • 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論