代码分享:UDP协议聊天工具的编写

网络 网络管理
文章中,我们分享了一个UDP协议的聊天器编写代码,希望对大家有所帮助。那么具体的源码内容请参考下文。

UDP协议我们在一些通讯软件中经常见到,而且也有不少朋友对这方面的编程感兴趣。那么这里我们就来介绍一下UDP协议的聊天器的编写过程。希望对大家有所帮助。代码:

using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Text;  
using System.Windows.Forms;  
using System.Net;  
using System.Net.Sockets;  
using System.Threading;  
namespace MulticastExample  
{  
    public partial class Form1 : Form  
    {  
        delegate void AppendStringCallback(string text);  
        AppendStringCallback appendStringCallback;  
        //使用的接收端口号  
        private int port = 8001;  
        private UdpClient udpClient;  
        public Form1()  
        {  
            InitializeComponent();  
            appendStringCallback = new AppendStringCallback(AppendString);  
        }  
        private void AppendString(string text)  
        {  
            if (richTextBox1.InvokeRequired)  
            {  
                richTextBox1.Invoke(appendStringCallback, text);  
            }  
            else 
            {  
                richTextBox1.AppendText(text + "\r\n");  
            }  
        }  
        private void ReceiveData()  
        {  
            udpClient = new UdpClient(port);  
            //必须使用UDP协议组播的地址范围内的地址  
            udpClient.JoinMulticastGroup(IPAddress.Parse("224.100.0.1"), 50);  
            IPEndPoint remote = null;  
            //接收从远程主机发送过来的信息  
            while (true)  
            {  
                try 
                {  
                    //关闭udpClient时此句会产生异常  
                    byte[] bytes = udpClient.Receive(ref remote);  
                    string str = Encoding.UTF8.GetString(bytes, 0, bytes.Length);  
                    AppendString(string.Format("来自{0}:{1}", remote, str));  
                }  
                catch 
                {  
                    //退出循环,结束线程  
                    break;  
                }  
            }  
        }  
        private void btnSend_Click(object sender, EventArgs e)  
        {  
            UdpClient myUdpClient = new UdpClient();  
            try 
            {  
                //允许发送和接收广播数据报  
                myUdpClient.EnableBroadcast = true;  
                //必须使用组播地址范围内的地址  
                IPEndPoint iep = new IPEndPoint(IPAddress.Parse("224.100.0.1"), port);  
                //将发送内容转换为字节数组  
                byte[] bytes = Encoding.UTF8.GetBytes(txbSend.Text);  
                //向子网发送信息  
                myUdpClient.Send(bytes, bytes.Length, iep);  
                txbSend.Clear();  
                txbSend.Focus();  
            }  
            catch (Exception err)  
            {  
                MessageBox.Show(err.Message, "发送失败");  
            }  
            finally  
            {  
                myUdpClient.Close();  
            }  
        }  
        private void Form1_Load(object sender, EventArgs e)  
        {  
            Thread receiveThread = new Thread(new ThreadStart(ReceiveData));  
            //将线程设为后台运行  
            receiveThread.IsBackground = true;  
            receiveThread.Start();  
        }  
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)  
        {  
            udpClient.Close();  
        }  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.

以上就是全部的UDP协议聊天器的编写代码了。

本文出自 “gauyanm” 博客,请务必保留此出处http://gauyanm.blog.51cto.com/629619/340047

责任编辑:佟健 来源: TT网络
相关推荐

2015-04-27 14:29:53

C#UDP实现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聊天应用开源

2011-12-21 17:39:03

imo即时通讯

2011-06-27 10:58:31

Qt 局域网 聊天

2017-05-10 11:10:15

LinuxUbuntuDiscord

2014-09-01 10:33:34

2009-10-26 11:04:36

VB.NET UDP协

2012-02-20 09:57:12

2011-12-15 10:30:51

即时通讯imo

2022-02-12 10:39:59

FBI网络犯罪加密

2023-02-15 14:07:03

2016-04-29 17:41:53

北信源/企业IM

2010-07-08 13:19:34

UDP协议

2011-03-30 20:44:46

上网行为管理管理策略网康科技

2010-06-29 12:42:05

UDP协议Java

2009-04-17 09:30:33

Firefox插件浏览器

2010-10-26 14:41:18

点赞
收藏

51CTO技术栈公众号