【正文】
是狼就要練好牙,是羊就要練好腿。其中Server類和Client類都是繼承Thread類,F(xiàn)ace類繼承Frame類,實(shí)現(xiàn)代碼復(fù)用。(1)增加登錄界面,用戶通過(guò)號(hào)碼和口令進(jìn)入系統(tǒng),對(duì)用戶身份進(jìn)行驗(yàn)證增加系統(tǒng)的安全性。在連接成功時(shí),應(yīng)用程序兩端都會(huì)產(chǎn)生一個(gè)Socket實(shí)例,操作這個(gè)實(shí)例,完成會(huì)話。 } private class TFListener implements KeyListener { } }}該部分主要是完成界面的初始化,合理布局組件,方便用戶交互。 } catch (SocketException e) { skt = new Socket(host, port)。 Face chat。 PrintStream theOutputStream。 Socket skt。import .*。 public void dataout(String data) { } public void run() {j++) try { import .*。下面列出主要存儲(chǔ)結(jié)構(gòu)或變量:存儲(chǔ)結(jié)構(gòu)、變量、對(duì)象類型說(shuō)明postInetAddress標(biāo)識(shí)IP地址Portint標(biāo)識(shí)端口Server [ ]ServerThread服務(wù)器端連接數(shù)Client [ ]Socket客戶端連接數(shù)Client(String ip,int p,Face chat)publicClient類成員函數(shù)Public void run()VoidClient、Server類成員函數(shù)Server(int port,Face chat)publicServer類成員函數(shù)Face()PublicFace類成員函數(shù)當(dāng)一個(gè)Socket會(huì)話產(chǎn)生后,將這個(gè)會(huì)話交給線程處理,然后主程序繼續(xù)監(jiān)聽(tīng)。munication一、課題內(nèi)容和要求基于Socket和多線程編程的聊天程序?qū)崿F(xiàn)網(wǎng)絡(luò)聊天程序設(shè)計(jì)非常復(fù)雜,允許多個(gè)人同時(shí)聊天更加需要多線程技術(shù)的支持,請(qǐng)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的多線程網(wǎng)絡(luò)聊天程序模擬。通過(guò)實(shí)例模型探討基于Java 的Socket 通信程序的一般結(jié)構(gòu)。 s end between two procedures that are operated on the network based on TCP/IP. bag includes Sockets, which is used to develop the network munication procedure paper probes into the general structurebased on Socket munication procedure of Java through the instance general munication software can be developed basedon this one,so there is certain reference value.Key words : ava。實(shí)現(xiàn)了好友添加,消息收發(fā)、顯示等基本功能??蛻舳瞬恍枰付ù蜷_(kāi)的端口,通常臨時(shí)的、動(dòng)態(tài)的分配一個(gè)端口。具體代碼如下:package 。 Socket Client[ ]=new Socket[10]。 int i = 0。 int port,k=0,l=0。Face chat。 l=。 //for(int j=0。 } = ++i。 Client = s。 = chat。 } } catch (IOException err) { }建好連接后,用戶通過(guò)得到Socket的輸入輸出流對(duì)象后,利用流的方法實(shí)現(xiàn)數(shù)據(jù)的傳輸。 readin = ()。 (readin + \n)。 } public void dataout(String data) {public class Face extends Frame { setLayout(ne