自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

代碼分享:UDP協(xié)議聊天工具的編寫

網(wǎng)絡(luò) 網(wǎng)絡(luò)管理
文章中,我們分享了一個UDP協(xié)議的聊天器編寫代碼,希望對大家有所幫助。那么具體的源碼內(nèi)容請參考下文。

UDP協(xié)議我們在一些通訊軟件中經(jīng)常見到,而且也有不少朋友對這方面的編程感興趣。那么這里我們就來介紹一下UDP協(xié)議的聊天器的編寫過程。希望對大家有所幫助。代碼:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. using System.Net;  
  9. using System.Net.Sockets;  
  10. using System.Threading;  
  11. namespace MulticastExample  
  12. {  
  13.     public partial class Form1 : Form  
  14.     {  
  15.         delegate void AppendStringCallback(string text);  
  16.         AppendStringCallback appendStringCallback;  
  17.         //使用的接收端口號  
  18.         private int port = 8001;  
  19.         private UdpClient udpClient;  
  20.         public Form1()  
  21.         {  
  22.             InitializeComponent();  
  23.             appendStringCallback = new AppendStringCallback(AppendString);  
  24.         }  
  25.         private void AppendString(string text)  
  26.         {  
  27.             if (richTextBox1.InvokeRequired)  
  28.             {  
  29.                 richTextBox1.Invoke(appendStringCallback, text);  
  30.             }  
  31.             else 
  32.             {  
  33.                 richTextBox1.AppendText(text + "\r\n");  
  34.             }  
  35.         }  
  36.         private void ReceiveData()  
  37.         {  
  38.             udpClient = new UdpClient(port);  
  39.             //必須使用UDP協(xié)議組播的地址范圍內(nèi)的地址  
  40.             udpClient.JoinMulticastGroup(IPAddress.Parse("224.100.0.1"), 50);  
  41.             IPEndPoint remote = null;  
  42.             //接收從遠(yuǎn)程主機發(fā)送過來的信息  
  43.             while (true)  
  44.             {  
  45.                 try 
  46.                 {  
  47.                     //關(guān)閉udpClient時此句會產(chǎn)生異常  
  48.                     byte[] bytes = udpClient.Receive(ref remote);  
  49.                     string str = Encoding.UTF8.GetString(bytes, 0, bytes.Length);  
  50.                     AppendString(string.Format("來自{0}:{1}", remote, str));  
  51.                 }  
  52.                 catch 
  53.                 {  
  54.                     //退出循環(huán),結(jié)束線程  
  55.                     break;  
  56.                 }  
  57.             }  
  58.         }  
  59.         private void btnSend_Click(object sender, EventArgs e)  
  60.         {  
  61.             UdpClient myUdpClient = new UdpClient();  
  62.             try 
  63.             {  
  64.                 //允許發(fā)送和接收廣播數(shù)據(jù)報  
  65.                 myUdpClient.EnableBroadcast = true;  
  66.                 //必須使用組播地址范圍內(nèi)的地址  
  67.                 IPEndPoint iep = new IPEndPoint(IPAddress.Parse("224.100.0.1"), port);  
  68.                 //將發(fā)送內(nèi)容轉(zhuǎn)換為字節(jié)數(shù)組  
  69.                 byte[] bytes = Encoding.UTF8.GetBytes(txbSend.Text);  
  70.                 //向子網(wǎng)發(fā)送信息  
  71.                 myUdpClient.Send(bytes, bytes.Length, iep);  
  72.                 txbSend.Clear();  
  73.                 txbSend.Focus();  
  74.             }  
  75.             catch (Exception err)  
  76.             {  
  77.                 MessageBox.Show(err.Message, "發(fā)送失敗");  
  78.             }  
  79.             finally  
  80.             {  
  81.                 myUdpClient.Close();  
  82.             }  
  83.         }  
  84.         private void Form1_Load(object sender, EventArgs e)  
  85.         {  
  86.             Thread receiveThread = new Thread(new ThreadStart(ReceiveData));  
  87.             //將線程設(shè)為后臺運行  
  88.             receiveThread.IsBackground = true;  
  89.             receiveThread.Start();  
  90.         }  
  91.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)  
  92.         {  
  93.             udpClient.Close();  
  94.         }  
  95.     }  

以上就是全部的UDP協(xié)議聊天器的編寫代碼了。

本文出自 “gauyanm” 博客,請務(wù)必保留此出處http://gauyanm.blog.51cto.com/629619/340047

責(zé)任編輯:佟健 來源: TT網(wǎng)絡(luò)
相關(guān)推薦

2015-04-27 14:29:53

C#UDP實現(xiàn)P2P語音聊天工具

2010-07-13 08:19:10

Linux聊天工具

2011-11-30 10:48:21

2019-03-07 14:45:07

聊天工具富文本輸入框前端

2022-02-12 12:18:59

Delta Chat聊天應(yīng)用開源

2011-12-21 17:39:03

imo即時通訊

2011-06-27 10:58:31

Qt 局域網(wǎng) 聊天

2017-05-10 11:10:15

LinuxUbuntuDiscord

2014-09-01 10:33:34

2009-10-26 11:04:36

VB.NET UDP協(xié)

2012-02-20 09:57:12

2011-12-15 10:30:51

即時通訊imo

2022-02-12 10:39:59

FBI網(wǎng)絡(luò)犯罪加密

2023-02-15 14:07:03

2016-04-29 17:41:53

北信源/企業(yè)IM

2010-07-08 13:19:34

UDP協(xié)議

2011-03-30 20:44:46

上網(wǎng)行為管理管理策略網(wǎng)康科技

2010-06-29 12:42:05

UDP協(xié)議Java

2009-04-17 09:30:33

Firefox插件瀏覽器

2010-10-26 14:41:18

點贊
收藏

51CTO技術(shù)棧公眾號