資源描述:
《計(jì)算機(jī)網(wǎng)絡(luò)tcp聊天室實(shí)驗(yàn)報(bào)告》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫(kù)。
1、計(jì)算機(jī)網(wǎng)絡(luò)原理實(shí)驗(yàn)報(bào) 告書(shū) 姓名:XXX 班級(jí):XXXXXX 學(xué)號(hào):XXXXXXXXXXXXXXXXXXXXXX二0一一年五月socket編程一、實(shí)驗(yàn)?zāi)康谋敬螌?shí)驗(yàn)的目的是通過(guò)利用Socket編寫(xiě)簡(jiǎn)單的客戶(hù)/服務(wù)器程序,了解TCP/IP網(wǎng)絡(luò)的基本通信原理和編程方法,初步掌握TCP/IP網(wǎng)絡(luò)的傳輸層上的編程接口(WindowsSocketsAPI),學(xué)會(huì)利用這些接口來(lái)編寫(xiě)簡(jiǎn)單的網(wǎng)絡(luò)通信軟件。二、實(shí)驗(yàn)內(nèi)容利用socket編寫(xiě)簡(jiǎn)單的客戶(hù)/服務(wù)器程序,比如聊天室、文件傳輸?shù)取H?、?shí)驗(yàn)步驟?思路分析2將服務(wù)器
2、與客戶(hù)端分兩部分實(shí)現(xiàn),采用多線程實(shí)現(xiàn)多個(gè)客戶(hù)端與服務(wù)器端連接2客戶(hù)端建立一個(gè)Socket與服務(wù)器端連接,成功后通過(guò)這個(gè)Socket來(lái)發(fā)送和接收數(shù)據(jù)就可以了。2服務(wù)器端通過(guò)創(chuàng)建一個(gè)Socket的Arraylist數(shù)組,與指定的本地端口綁定,就可以在端口進(jìn)行偵聽(tīng)。如果有客戶(hù)端連接請(qǐng)求,則在數(shù)組中選擇一個(gè)空Socket,將客戶(hù)端地址賦給這個(gè)Socket。然后登錄成功的客戶(hù)就可以在服務(wù)器上聊天了。?代碼實(shí)現(xiàn)?packageTCP_chat_room.gui;??importjava.io.IOException;??publiccla
3、ssClientApp{?publicstaticvoidmain(String[]args){?/**?*javax.swing.UIManager$LookAndFeelInfo[Metaljavax.swing.plaf.metal.MetalLookAndFeel]?javax.swing.UIManager$LookAndFeelInfo[Nimbuscom.sun.java.swing.plaf.nimbus.NimbusLookAndFeel]?javax.swing.UIManager$LookAndFeelI
4、nfo[CDE/Motifcom.sun.java.swing.plaf.motif.MotifLookAndFeel]?javax.swing.UIManager$LookAndFeelInfo[Windowscom.sun.java.swing.plaf.windows.WindowsLookAndFeel]?javax.swing.UIManager$LookAndFeelInfo[WindowsClassiccom.sun.java.swing.plaf.windows.WindowsClassicLookAndFee
5、l]?*@authorAdministrator?*?*/?try{?//使用Nimbus皮膚?UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");?//localhost或127.0.0.1?ClientFramefrClientFrame=newClientFrame("localhost",Server.CHAT_ROOM_PORT);?frClientFrame.setConnected(true);?frClient
6、Frame.setVisible(true);?//如果登錄成功?if(frClientFrame.login("222")){?frClientFrame.receiveMessage();?}?}catch(ClassNotFoundExceptione){?e.printStackTrace();?}catch(InstantiationExceptione){?e.printStackTrace();?}catch(IllegalAccessExceptione){?e.printStackTrace();?}catc
7、h(UnsupportedLookAndFeelExceptione){?e.printStackTrace();?}catch(IOExceptione){?e.printStackTrace();?}?}???}packageTCP_chat_room.action;importjava.awt.AWTException;publicclassClientFrameActionextendsWindowAdapter{privateClientFramefrClientFrame;publicClientFrameActi
8、on(ClientFramefrClientFrame){this.frClientFrame=frClientFrame;}publicvoidwindowIconified(WindowEvente){if(SystemTray.isSupported()){finalS