• 大小: 24KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-14
  • 语言: C#
  • 标签: UDP  广播  异步  局域网  C#  

资源简介

UDP 局域网异步通讯实现广播,接收发送string数据 用户就可以在一个局域网中聊天了。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace Broadcast
{
    public class Broadcast
    {
        static Socket sockReceive;
        static byte[] buff;
        static EndPoint ep;
        public Broadcast() {
            buff=new byte[8192];
            sockReceive = new Socket(AddressFamily.InterNetwork
            SocketType.Dgram ProtocolType.Udp);
            IPEndPoint iep = new IPEndPoint(IPAddress.Any 9050);
            sockReceive.Bind(iep);


            ep = (EndPoint)iep;
            sockReceive.BeginReceiveFrom(buff 0 buff.Length SocketFlags.None ref ep new AsyncCallback(CallReceive) sockReceive);
        }

        private void CallReceive(IAsyncResult ar)
        {
            Socket sock = (Socket)ar.AsyncState;
            
            try
            {
                sock.Blocking = true;
                ar.AsyncWaitHandle.WaitOne();
                if (ar.IsCompleted)
                {
                    int i = sock.EndReceiveFrom(ar ref ep);
                    Console.Write(Encoding.ASCII.GetString(buff 0 i) + “\n“);
                }
                sock.Blocking = false;
            }
            catch (SocketException soketEx)
            {
                throw soketEx;
            }
            finally
            {
                sock.BeginReceiveFrom(buff 0 buff.Length SocketFlags.None ref ep CallReceive sock);
            }
        }

        public void Close() {
            sockReceive.Close();
        }
    }
}

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

     文件       5632  2011-05-30 15:10  Broadcast\Broadcast\bin\Debug\Broadcast.vshost.exe

     文件       1621  2011-05-30 14:56  Broadcast\Broadcast\Broadcast.cs

     文件       3305  2011-05-30 14:44  Broadcast\Broadcast\Broadcast.csproj

     文件       1373  2011-05-30 15:10  Broadcast\Broadcast\Form1.cs

     文件       2097  2011-05-30 15:09  Broadcast\Broadcast\Form1.Designer.cs

     文件       5814  2011-05-30 15:09  Broadcast\Broadcast\Form1.resx

     文件        476  2011-05-30 14:45  Broadcast\Broadcast\Program.cs

     文件       1284  2011-05-30 11:29  Broadcast\Broadcast\Properties\AssemblyInfo.cs

     文件       2847  2011-05-30 11:29  Broadcast\Broadcast\Properties\Resources.Designer.cs

     文件       5612  2011-05-30 11:29  Broadcast\Broadcast\Properties\Resources.resx

     文件       1094  2011-05-30 11:29  Broadcast\Broadcast\Properties\Settings.Designer.cs

     文件        249  2011-05-30 11:29  Broadcast\Broadcast\Properties\Settings.settings

     文件       1688  2011-05-30 14:10  Broadcast\Broadcast.sln

    ..A..H.     17920  2011-05-30 15:10  Broadcast\Broadcast.suo

     文件       3219  2011-05-30 14:10  Broadcast\Client\Client.csproj

     文件       1213  2011-05-30 15:09  Broadcast\Client\Form1.cs

     文件       2562  2011-05-30 15:09  Broadcast\Client\Form1.Designer.cs

     文件       5814  2011-05-30 15:09  Broadcast\Client\Form1.resx

     文件        473  2011-05-30 13:53  Broadcast\Client\Program.cs

     文件       1278  2011-05-30 13:53  Broadcast\Client\Properties\AssemblyInfo.cs

     文件       2841  2011-05-30 13:53  Broadcast\Client\Properties\Resources.Designer.cs

     文件       5612  2011-05-30 13:53  Broadcast\Client\Properties\Resources.resx

     文件       1091  2011-05-30 13:53  Broadcast\Client\Properties\Settings.Designer.cs

     文件        249  2011-05-30 13:53  Broadcast\Client\Properties\Settings.settings

     目录          0  2011-05-30 11:29  Broadcast\Broadcast\obj\Debug\TempPE

     目录          0  2011-05-30 13:53  Broadcast\Client\obj\Debug\TempPE

     目录          0  2011-05-30 15:10  Broadcast\Broadcast\bin\Debug

     目录          0  2011-05-30 15:09  Broadcast\Broadcast\obj\Debug

     目录          0  2011-05-30 15:10  Broadcast\Client\bin\Debug

     目录          0  2011-05-30 15:10  Broadcast\Client\obj\Debug

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

评论

共有 条评论