• 大小: 0.03M
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2024-05-03
  • 语言: C#
  • 标签: UDP  DP  连接  C#  

资源简介

控制台程序,实现了本机连接

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace UDP_Server
{
    class Program
    {
        static Socket server;
        static void Main(string[] args)
        {
            server = new Socket(AddressFamily.InterNetwork SocketType.Dgram ProtocolType.Udp);
            server.Bind(new IPEndPoint(IPAddress.Parse(“127.0.0.1“) 2000));//绑定端口号和IP
            Console.WriteLine(“服务端已经开启“);
            Thread t = new Thread(ReciveMsg);//开启接收消息线程
            t.Start();
            Thread t2 = new Thread(sendMsg);//开启发送消息线程
            t2.Start();
        }
        /// 
        /// 向特定ip的主机的端口发送数据报
        /// 

        static void sendMsg()
        {
            EndPoint point = new IPEndPoint(IPAddress.Parse(“127.0.0.1“) 2000);
            while (true)
            {
                string msg = Console.ReadLine();
                server.SendTo(Encoding.UTF8.GetBytes(msg) point);
            }
        }
        /// 
        /// 接收发送给本机ip对应端口号的数据报
        /// 

        static void ReciveMsg()
        {
            while (true)
            {
                EndPoint point = new IPEndPoint(IPAddress.Any 0);//用来保存发送方的ip和端口号
                byte[] buffer = new byte[1024];
                int length = server.ReceiveFrom(buffer ref point);//接收数据报
                string message = Encoding.UTF8.GetString(buffer 0 length);
                Console.WriteLine(point.ToString() + message);

            }
        }
    }
}

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

     文件        187  2020-12-11 14:37  ConsoleApplication1\ConsoleApplication1\App.config

     文件       5632  2020-12-11 22:49  ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe

     文件        187  2020-12-11 14:37  ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe.config

     文件      13824  2020-12-11 22:49  ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.pdb

     文件      24224  2020-12-11 22:49  ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.vshost.exe

     文件        187  2020-12-11 14:37  ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.vshost.exe.config

     文件        490  2019-03-19 12:46  ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.vshost.exe.manifest

     文件       2580  2020-12-11 14:37  ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.csproj

     文件        688  2020-12-11 22:49  ConsoleApplication1\ConsoleApplication1\obj\Debug\ConsoleApplication1.csproj.FileListAbsolute.txt

     文件       1755  2020-12-11 21:13  ConsoleApplication1\ConsoleApplication1\obj\Debug\ConsoleApplication1.csprojResolveAssemblyReference.cache

     文件       5632  2020-12-11 22:49  ConsoleApplication1\ConsoleApplication1\obj\Debug\ConsoleApplication1.exe

     文件      13824  2020-12-11 22:49  ConsoleApplication1\ConsoleApplication1\obj\Debug\ConsoleApplication1.pdb

     文件        725  2020-12-11 21:27  ConsoleApplication1\ConsoleApplication1\obj\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       6658  2020-12-11 19:10  ConsoleApplication1\ConsoleApplication1\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件          0  2020-12-11 14:37  ConsoleApplication1\ConsoleApplication1\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs

     文件          0  2020-12-11 14:37  ConsoleApplication1\ConsoleApplication1\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs

     文件          0  2020-12-11 14:37  ConsoleApplication1\ConsoleApplication1\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs

     文件       1820  2020-12-11 22:49  ConsoleApplication1\ConsoleApplication1\Program.cs

     文件       1362  2020-12-11 14:37  ConsoleApplication1\ConsoleApplication1\Properties\AssemblyInfo.cs

     文件       1026  2020-12-11 14:37  ConsoleApplication1\ConsoleApplication1.sln

    ..A..H.     30720  2020-12-11 21:37  ConsoleApplication1\ConsoleApplication1.v12.suo

     目录          0  2020-12-11 14:37  ConsoleApplication1\ConsoleApplication1\obj\Debug\TempPE

     目录          0  2020-12-11 21:13  ConsoleApplication1\ConsoleApplication1\bin\Debug

     目录          0  2020-12-11 22:49  ConsoleApplication1\ConsoleApplication1\obj\Debug

     目录          0  2020-12-11 14:37  ConsoleApplication1\ConsoleApplication1\bin

     目录          0  2020-12-11 14:37  ConsoleApplication1\ConsoleApplication1\obj

     目录          0  2020-12-11 14:37  ConsoleApplication1\ConsoleApplication1\Properties

     目录          0  2020-12-11 22:49  ConsoleApplication1\ConsoleApplication1

     目录          0  2020-12-11 14:37  ConsoleApplication1

----------- ---------  ---------- -----  ----

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

评论

共有 条评论