使用C#實(shí)現(xiàn)基于TCP和UDP協(xié)議的網(wǎng)絡(luò)通信程序的基本示例
C#中使用TCP通信
TCP通信需要通信雙方都在線,所以需要先啟動(dòng)服務(wù)端進(jìn)行監(jiān)聽(tīng),客戶端才能獲得連接,服務(wù)端代碼:
static void Main(string[] args) { TcpClient client = null; NetworkStream stream = null; byte[] buffer = null; string receiveString = null; IPAddress localIP = IPAddress.Parse("127.0.0.1"); int localPort = 11000; TcpListener listener = new TcpListener(localIP, localPort);//用本地IP和端口實(shí)例化Listener listener.Start();//開(kāi)始監(jiān)聽(tīng) while (true) { client = listener.AcceptTcpClient();//接受一個(gè)Client buffer = new byte[client.ReceiveBufferSize]; stream = client.GetStream();//獲取網(wǎng)絡(luò)流 stream.Read(buffer, 0, buffer.Length);//讀取網(wǎng)絡(luò)流中的數(shù)據(jù) stream.Close();//關(guān)閉流 client.Close();//關(guān)閉Client receiveString = Encoding.Default.GetString(buffer).Trim('\0');//轉(zhuǎn)換成字符串 Console.WriteLine(receiveString); } }
只有服務(wù)端開(kāi)啟監(jiān)聽(tīng)后,客戶端才能正確連接,所以服務(wù)端要一直開(kāi)啟監(jiān)聽(tīng),客戶端每次發(fā)送數(shù)據(jù),都要首先與服務(wù)端建立連接,連接建立完成后才進(jìn)行數(shù)據(jù)發(fā)送。客戶端代碼:
static void Main(string[] args) { string sendString = null;//要發(fā)送的字符串 byte[] sendData = null;//要發(fā)送的字節(jié)數(shù)組 TcpClient client = null;//TcpClient實(shí)例 NetworkStream stream = null;//網(wǎng)絡(luò)流 IPAddress remoteIP = IPAddress.Parse("127.0.0.1");//遠(yuǎn)程主機(jī)IP int remotePort = 11000;//遠(yuǎn)程主機(jī)端口 while (true)//死循環(huán) { sendString = Console.ReadLine();//獲取要發(fā)送的字符串 sendData = Encoding.Default.GetBytes(sendString);//獲取要發(fā)送的字節(jié)數(shù)組 client = new TcpClient();//實(shí)例化TcpClient try { client.Connect(remoteIP, remotePort);//連接遠(yuǎn)程主機(jī) } catch (System.Exception ex) { Console.WriteLine("連接超時(shí),服務(wù)器沒(méi)有響應(yīng)!");//連接失敗 Console.ReadKey(); return; } stream = client.GetStream();//獲取網(wǎng)絡(luò)流 stream.Write(sendData, 0, sendData.Length);//將數(shù)據(jù)寫(xiě)入網(wǎng)絡(luò)流 stream.Close();//關(guān)閉網(wǎng)絡(luò)流 client.Close();//關(guān)閉客戶端 } }
C#中使用UDP通信
UDP通信是無(wú)連接通信,客戶端在發(fā)送數(shù)據(jù)前無(wú)需與服務(wù)器端建立連接,即使服務(wù)器端不在線也可以發(fā)送,但是不能保證服務(wù)器端可以收到數(shù)據(jù)。
服務(wù)器端代碼:
static void Main(string[] args) { UdpClient client = null; string receiveString = null; byte[] receiveData = null; //實(shí)例化一個(gè)遠(yuǎn)程端點(diǎn),IP和端口可以隨意指定,等調(diào)用client.Receive(ref remotePoint)時(shí)會(huì)將該端點(diǎn)改成真正發(fā)送端端點(diǎn) IPEndPoint remotePoint = new IPEndPoint(IPAddress.Any, 0); while (true) { client = new UdpClient(11000); receiveData = client.Receive(ref remotePoint);//接收數(shù)據(jù) receiveString = Encoding.Default.GetString(receiveData); Console.WriteLine(receiveString); client.Close();//關(guān)閉連接 } }
客戶端代碼:
static void Main(string[] args) { string sendString = null;//要發(fā)送的字符串 byte[] sendData = null;//要發(fā)送的字節(jié)數(shù)組 UdpClient client = null; IPAddress remoteIP = IPAddress.Parse("127.0.0.1"); int remotePort = 11000; IPEndPoint remotePoint = new IPEndPoint(remoteIP, remotePort);//實(shí)例化一個(gè)遠(yuǎn)程端點(diǎn) while (true) { sendString = Console.ReadLine(); sendData = Encoding.Default.GetBytes(sendString); client = new UdpClient(); client.Send(sendData, sendData.Length, remotePoint);//將數(shù)據(jù)發(fā)送到遠(yuǎn)程端點(diǎn) client.Close();//關(guān)閉連接 } }
上一篇:C#程序中session值的保存方法以及轉(zhuǎn)為字符串的方法總結(jié)
欄 目:C#教程
下一篇:Unity UGUI教程之實(shí)現(xiàn)滑頁(yè)效果
本文標(biāo)題:使用C#實(shí)現(xiàn)基于TCP和UDP協(xié)議的網(wǎng)絡(luò)通信程序的基本示例
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6587.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新聞效果的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法
- 01-10C#實(shí)現(xiàn)Winform中打開(kāi)網(wǎng)頁(yè)頁(yè)面的方法
- 01-10C#實(shí)現(xiàn)遠(yuǎn)程關(guān)閉計(jì)算機(jī)或重啟計(jì)算機(jī)的方法
- 01-10C#自定義簽名章實(shí)現(xiàn)方法
- 01-10C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
- 01-10winform實(shí)現(xiàn)創(chuàng)建最前端窗體的方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)