• 大小: 75KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-16
  • 语言: C#
  • 标签: C#  UDP  客户端  

资源简介

用C#写的基于UDP的聊天系统,能实现多台电脑聊天,没有服务器

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;

namespace UdpChatExample
{
    public partial class FormMain : Form
    {
        /// 
        /// 接收用
        /// 

        private UdpClient receiveUdpClient;
        /// 
        /// 发送用
        /// 

        private UdpClient sendUdpClient;
        /// 
        /// 和本机绑定的端口号
        /// 

        private const int port = 8889;
        /// 
        /// 本机IP
        /// 

        IPAddress ip;
        /// 
        /// 远程主机IP
        /// 

        IPAddress remoteIp;
        public FormMain()
        {
            InitializeComponent();
            //获取本机可用IP地址
            IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName());
            foreach (IPAddress ipa in ips)
            {
                if (ipa.AddressFamily == AddressFamily.InterNetwork)
                {
                    ip = ipa;
                    break;
                }
            }
            //为了在同一台机器调试,此IP也作为默认远程IP
            remoteIp = ip;
            txt_IPAddress.Text = remoteIp.ToString();
            txt_Send.Text = “你好!“;
        }

        private void FormMain_Load(object sender EventArgs e)
        {
            //创建一个县城接收远程主机发来的信息
            Thread myThread = new Thread(ReceiveData);
            myThread.IsBackground = true;
            myThread.Start();
            txt_Send.Focus();
        }

        /// 
        /// 接收数据
        /// 

        private void ReceiveData()
        {
            IPEndPoint local = new IPEndPoint(ip port);
            receiveUdpClient = new UdpClient(local);
            IPEndPoint remote = new IPEndPoint(IPAddress.Any 0);
            while (true)
            {
                try
                {
                    //关闭udpClient 时此句会产生异常
                    byte[] receiveBytes = receiveUdpClient.Receive(ref remote);
                    string receiveMessage = Encoding.Unicode.GetString(
                        receiveBytes 0 receiveBytes.Length);
                    AddItem(lst_Receive string.Format(“来自{0}:{1}“ remote receiveMessage));
                }
                catch
                {
                    break;
                }
            }
        }

        private void button1_Click(object sender EventArgs e)
        {
            remoteIp = IPAddress.Parse(txt_IPAddress.Text);
            Thread myThread = new Thread(SendMessage);
            myThread.IsBackground = true;
            myThread.Start(txt_Send.Text);
        }

        /// 
        /// 发送消息
        /// 

        /// 
        private void

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       4745  2011-08-09 03:08  UdpChatExample\UdpChatExample\Backup\FormMain.cs

     文件       6781  2011-08-09 02:02  UdpChatExample\UdpChatExample\Backup\FormMain.Designer.cs

     文件       5814  2011-08-09 02:02  UdpChatExample\UdpChatExample\Backup\FormMain.resx

     文件        498  2011-08-09 01:49  UdpChatExample\UdpChatExample\Backup\Program.cs

     文件       1378  2011-08-09 01:41  UdpChatExample\UdpChatExample\Backup\Properties\AssemblyInfo.cs

     文件       2878  2011-08-09 01:41  UdpChatExample\UdpChatExample\Backup\Properties\Resources.Designer.cs

     文件       5612  2011-08-09 01:41  UdpChatExample\UdpChatExample\Backup\Properties\Resources.resx

     文件       1099  2011-08-09 01:41  UdpChatExample\UdpChatExample\Backup\Properties\Settings.Designer.cs

     文件        249  2011-08-09 01:41  UdpChatExample\UdpChatExample\Backup\Properties\Settings.settings

     文件       3754  2011-08-09 02:09  UdpChatExample\UdpChatExample\Backup\UdpChatExample.csproj

     文件      12288  2015-06-30 16:06  UdpChatExample\UdpChatExample\bin\Debug\UdpChatExample.exe

     文件      30208  2015-06-30 16:06  UdpChatExample\UdpChatExample\bin\Debug\UdpChatExample.pdb

     文件      11608  2015-06-30 16:06  UdpChatExample\UdpChatExample\bin\Debug\UdpChatExample.vshost.exe

     文件        490  2010-03-17 22:39  UdpChatExample\UdpChatExample\bin\Debug\UdpChatExample.vshost.exe.manifest

     文件      16553  2011-08-09 02:55  UdpChatExample\UdpChatExample\bin\Debug.rar

     文件       4745  2011-08-09 03:08  UdpChatExample\UdpChatExample\FormMain.cs

     文件       6781  2011-08-09 02:02  UdpChatExample\UdpChatExample\FormMain.Designer.cs

     文件       5814  2011-08-09 02:02  UdpChatExample\UdpChatExample\FormMain.resx

     文件       6055  2015-06-30 16:08  UdpChatExample\UdpChatExample\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件        722  2015-06-30 16:06  UdpChatExample\UdpChatExample\obj\Debug\GenerateResource-ResGen.read.1.tlog

     文件        658  2015-06-30 16:06  UdpChatExample\UdpChatExample\obj\Debug\GenerateResource-ResGen.write.1.tlog

     文件       4608  2015-06-30 16:06  UdpChatExample\UdpChatExample\obj\Debug\TempPE\Properties.Resources.Designer.cs.dll

     文件       1509  2015-06-30 16:13  UdpChatExample\UdpChatExample\obj\Debug\UdpChatExample.csproj.FileListAbsolute.txt

     文件        850  2011-08-09 02:09  UdpChatExample\UdpChatExample\obj\Debug\UdpChatExample.csproj.GenerateResource.Cache

     文件      12288  2015-06-30 16:06  UdpChatExample\UdpChatExample\obj\Debug\UdpChatExample.exe

     文件        180  2015-06-30 16:06  UdpChatExample\UdpChatExample\obj\Debug\UdpChatExample.FormMain.resources

     文件      30208  2015-06-30 16:06  UdpChatExample\UdpChatExample\obj\Debug\UdpChatExample.pdb

     文件        180  2015-06-30 16:06  UdpChatExample\UdpChatExample\obj\Debug\UdpChatExample.Properties.Resources.resources

     文件        498  2011-08-09 01:49  UdpChatExample\UdpChatExample\Program.cs

     文件       1378  2011-08-09 01:41  UdpChatExample\UdpChatExample\Properties\AssemblyInfo.cs

............此处省略26个文件信息

评论

共有 条评论