計(jì)算機(jī)網(wǎng)絡(luò)課程設(shè)計(jì)報(bào)告-聊天程序_第1頁(yè)
計(jì)算機(jī)網(wǎng)絡(luò)課程設(shè)計(jì)報(bào)告-聊天程序_第2頁(yè)
計(jì)算機(jī)網(wǎng)絡(luò)課程設(shè)計(jì)報(bào)告-聊天程序_第3頁(yè)
計(jì)算機(jī)網(wǎng)絡(luò)課程設(shè)計(jì)報(bào)告-聊天程序_第4頁(yè)
計(jì)算機(jī)網(wǎng)絡(luò)課程設(shè)計(jì)報(bào)告-聊天程序_第5頁(yè)
已閱讀5頁(yè),還剩7頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

計(jì)算機(jī)網(wǎng)絡(luò)課程設(shè)計(jì)報(bào)告題目:網(wǎng)絡(luò)聊天程序的設(shè)計(jì)與實(shí)現(xiàn)

姓名:申超明學(xué)號(hào):0705010313班級(jí):計(jì)算機(jī)3班指導(dǎo)老師:文宏湖南科技大學(xué)計(jì)算機(jī)科學(xué)與工程學(xué)院2010年9月

一:實(shí)驗(yàn)名稱網(wǎng)絡(luò)聊天程序的設(shè)計(jì)與實(shí)現(xiàn)二:實(shí)驗(yàn)?zāi)康? 了解Socket通信的原理,并通過(guò)程序?qū)崿F(xiàn)通信的過(guò)程。三:實(shí)驗(yàn)要求熟悉Socket程序通信過(guò)程,熟悉在通信過(guò)程中流之間的轉(zhuǎn)換。程序中熟悉靈活運(yùn)用線程執(zhí)行相關(guān)操作。四:實(shí)驗(yàn)原理Socket通信的原理還是比較簡(jiǎn)單的,它大致分為以下幾個(gè)步驟。服務(wù)器端的步驟如下:建立服務(wù)器端的Socket,開(kāi)始偵聽(tīng)整個(gè)網(wǎng)絡(luò)中的連接請(qǐng)求。當(dāng)檢測(cè)到來(lái)自客戶端的連接請(qǐng)求時(shí),向客戶端發(fā)送收到連接請(qǐng)求的信息,并建立與客戶端之間的連接。當(dāng)完成通信后,服務(wù)器關(guān)閉與客戶端的Socket連接??蛻舳说牟襟E如下:建立客戶端的Socket,確定要連接的服務(wù)器的主機(jī)名和端口。發(fā)送連接請(qǐng)求到服務(wù)器,并等待服務(wù)器的回饋信息。連接成功后,與服務(wù)器進(jìn)行數(shù)據(jù)的交互。數(shù)據(jù)處理完畢后,關(guān)閉自身的Socket連接。

五:程序截圖:圖1-服務(wù)器端啟動(dòng)服務(wù)器 圖2-客戶端連接數(shù)據(jù)庫(kù)圖3-客戶端發(fā)送消息圖4-服務(wù)器段接收客戶端發(fā)送過(guò)來(lái)的消息六:程序代碼:Qserver.csusingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.IO;usingSystem.Net;usingSystem.Net.Sockets;usingSystem.Threading;namespaceWindowsApplication2{publicpartialclassqserver:Form{publicqserver(){InitializeComponent();}privatestringipv4;privatevoidForm1_Load(objectsender,EventArgse){IPAddress[]ip=Dns.GetHostAddresses(Dns.GetHostName());ipv4=ip[0].ToString();this.label4.Text=ipv4;}TcpListenertcpls;Socketsk;NetworkStreamns;StreamReadersr;StreamWritersw;Threadtred;privatevoidbutton1_Click(objectsender,EventArgse){tcpls=newTcpListener(1235);tcpls.Start();MessageBox.Show("服務(wù)器已啟動(dòng)");this.button1.Enabled=false;sk=tcpls.AcceptSocket();MessageBox.Show("客戶端已連接");ns=newNetworkStream(sk);sr=newStreamReader(ns,Encoding.UTF8);sw=newStreamWriter(ns,Encoding.UTF8);}stringsmessage="";privatevoidbutton2_Click(objectsender,EventArgse){this.textBox1.Focus();tred=newThread(newThreadStart(this.acceptcm));tred.Start();this.textBox2.Focus();}privatevoidbutton3_Click(objectsender,EventArgse){sendtoc();this.textBox2.Focus();}privatevoidtextBox2_KeyPress(objectsender,KeyPressEventArgse){if(e.KeyChar=='\r')sendtoc();}privatevoidacceptcm(){lock(this){try{if(sr.ReadLine()!=null){this.textBox1.Text+=sr.ReadLine();}elsereturn;}catch(System.Exceptionex){}finally{tred.Abort();}}}publicvoidsendtoc(){try{smessage="\r\n"+this.textBox3.Text+"("+DateTime.Now.ToLongTimeString()+")"+""+this.textBox2.Text;this.textBox1.Text+=smessage;this.textBox2.Clear();sw.WriteLine(smessage);sw.Flush();}catch{MessageBox.Show("服務(wù)器端錯(cuò)誤");}}privatevoidqserver_FormClosed(objectsender,FormClosedEventArgse){if(tcpls!=null)tcpls.Stop();//if(tred!=null)//tred.Abort();Application.Exit();}}}

客戶端代碼:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.IO;usingSystem.Net;usingSystem.Net.Sockets;usingSystem.Threading;namespaceClient{publicpartialclassqclient:Form{publicqclient(){InitializeComponent();}TcpClienttcpct;NetworkStreamns;StreamReadersr;StreamWritersw;Threadtrdc;///<summary>///連接服務(wù)器操作,發(fā)送socket請(qǐng)求///</summary>///<paramname="sender"></param>///<paramname="e"></param>privatevoidbutton1_Click(objectsender,EventArgse){try{stringip=this.textBox4.Text+"."+this.textBox5.Text+"."+this.textBox6.Text+"."+this.textBox7.Text;tcpct=newTcpClient(ip,1235);ns=tcpct.GetStream();sr=newStreamReader(ns);sw=newStreamWriter(ns);this.button1.Enabled=false;}catch{MessageBox.Show("請(qǐng)確認(rèn)是否開(kāi)啟服務(wù)器...");}}stringcmessage="";///<summary>///發(fā)送消息事件///</summary>///<paramname="sender"></param>///<paramname="e"></param>privatevoidbutton2_Click(objectsender,EventArgse){try{sendtos();}catch{//MessageBox.Show("出錯(cuò)啦!"+"\r\n"+"1.你是否已經(jīng)連接服務(wù)器?"+"\r\n"+"2.等待服務(wù)器發(fā)回消息.");}finally{this.textBox2.Focus();}}///<summary>///發(fā)送函數(shù)///</summary>publicvoidsendtos(){if(this.textBox2.Text=="")//發(fā)送{MessageBox.Show("內(nèi)容不能為空");return;}else{cmessage="("+DateTime.Now.ToLongTimeString()+")"+"\r\n"+this.textBox3.Text+this.textBox2.Text;this.textBox1.Text+=cmessage;this.textBox2.Clear();//savechathistrory();sw.WriteLine(cmessage);sw.Flush();}}///<summary>///接收函數(shù)///</summary>publicvoidacceptsm(){lock(this){try//接收{(diào)this.textBox1.Text+=sr.ReadLine()+"\r\n";//savechathistrory();}catch{//MessageBox.Show("出錯(cuò)啦!"+"\r\n"+"1.你是否已經(jīng)連接服務(wù)器?"+"\r\n"+"2.沒(méi)有服務(wù)器消息.");}finally{trdc.Abort();}}}///<summary>///接收消息事件///</summary>///<paramname="sender"></param>///<paramname="e"></param>privatevoidbutton3_Click(objectsender,EventArgse){this.textBox1.Focus();trdc=newThread(newThreadStart(acceptsm));trdc.Start();this.textBox2.Focus();}///<summary>///保存歷史記錄,未做.///</summary>privatevoidsavechathistrory(){StreamWriterfsw=newStreamWriter(Application.UserAppDataPath+"chathistory.txt",false,Encoding.UTF8);sw.WriteLine(this.textBox1.Text);fsw.Flush();fsw.Close();}///<summary>///文件關(guān)閉之后,關(guān)閉客戶端,關(guān)閉活動(dòng)的線程///</summary>///<paramname="sender"></param>///<paramname="e"></param>privatevoidqclient_FormClosed(objectsender,FormC

溫馨提示

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

評(píng)論

0/150

提交評(píng)論