• 大小: 90KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-07
  • 语言: C#
  • 标签: P2PNAT  

资源简介

P2P之UDP穿透NAT的原理与C#实现

资源截图

代码片段和文件信息

namespace P2P.P2PClient
{

    using System;

    using System.Net;

    using System.Net.Sockets;

    using System.Threading;

    using P2P.WellKnown;

    /// 

    /// AppClass 的摘要说明。

    /// 


    public class AppClass
    {

        public static void Main()
        {

            Client client = new Client(“192.168.1.25“);

            client.ConnectToServer(“myname1“ “mypassword1“);

            client.Start();

            Console.WriteLine(“test arguments“);

            while (true)
            {

                string str = Console.ReadLine();

                client.PaserCommand(str);

            }

        }

    }

    /// 

    /// Client 的摘要说明。

    /// 


    public class Client : IDisposable
    {

        private const int MAXRETRY = 10;

        private UdpClient client;

        private IPEndPoint hostPoint;

        private IPEndPoint remotePoint;

        private UserCollection userList;

        private string myName;

        private bool ReceivedACK;

        private Thread listenThread;



        public Client(string serverIP)
        {

            ReceivedACK = false;

            remotePoint = new IPEndPoint(IPAddress.Any 0);

            hostPoint = new IPEndPoint(IPAddress.Parse(serverIP) P2PConsts.SRV_PORT);

            client = new UdpClient();

            userList = new UserCollection();

            listenThread = new Thread(new ThreadStart(Run));

        }



        public void Start()
        {

            if (this.listenThread.ThreadState == ThreadState.Unstarted)
            {

                this.listenThread.Start();

                Console.WriteLine(“You can input you command:\n“);

                Console.WriteLine(“Command Type:\“send\“\“exit\“\“getu\““);

                Console.WriteLine(“Example : send Username Message“);

                Console.WriteLine(“          exit“);

                Console.WriteLine(“          getu“);

            }

        }



        public void ConnectToServer(string userName string password)
        {

            myName = userName;

            // 发送登录消息到服务器

            P2P.WellKnown.C2S.LoginMessage lginMsg = new P2P.WellKnown.C2S.LoginMessage(userName password);

            byte[] buffer = FormatterHelper.Serialize(lginMsg);

            client.Send(buffer buffer.Length hostPoint);

            // 接受服务器的登录应答消息

            buffer = client.Receive(ref remotePoint);

            P2P.WellKnown.S2C.GetUsersResponseMessage srvResMsg = (P2P.WellKnown.S2C.GetUsersResponseMessage)FormatterHelper.Deserialize(buffer);

            // 更新用户列表

            userList.Clear();

            foreach (User user in srvResMsg.UserList)
            {

                userList.Add(user);

            }

            this.DisplayUsers(userList);

        }



        /// 

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

     文件       1177  2009-01-06 15:22  P2P\P2P\Properties\AssemblyInfo.cs

     文件       1940  2009-01-06 15:22  P2P\P2P\P2P.csproj

     文件       5632  2005-11-11 22:25  P2P\P2P\bin\Debug\P2P.vshost.exe

     文件      16384  2009-01-06 15:23  P2P\P2P\bin\Debug\P2P.exe

     文件      11776  2009-01-06 15:23  P2P\P2P\bin\Debug\P2P.pdb

     文件      11776  2009-01-06 15:23  P2P\P2P\obj\Debug\P2P.pdb

     文件      16384  2009-01-06 15:23  P2P\P2P\obj\Debug\P2P.exe

     文件        188  2009-01-06 15:23  P2P\P2P\obj\P2P.csproj.FileListAbsolute.txt

     文件        192  2009-01-06 15:22  P2P\P2P\Program.cs

     文件       1911  2009-01-06 15:24  P2P\P2P.sln

    ..A..H.     24576  2009-01-07 11:46  P2P\P2P.suo

     文件       1195  2009-01-06 15:22  P2P\2P.P2PClient\Properties\AssemblyInfo.cs

     文件       5632  2005-11-11 22:25  P2P\2P.P2PClient\bin\Debug\2P.P2PClient.vshost.exe

     文件      20480  2009-01-07 11:51  P2P\2P.P2PClient\bin\Debug\P2P.WellKnown.dll

     文件      24064  2009-01-07 11:51  P2P\2P.P2PClient\bin\Debug\P2P.WellKnown.pdb

     文件      20480  2009-01-07 11:51  P2P\2P.P2PClient\bin\Debug\2P.P2PClient.exe

     文件      19968  2009-01-07 11:51  P2P\2P.P2PClient\bin\Debug\2P.P2PClient.pdb

     文件       2185  2009-01-06 15:24  P2P\2P.P2PClient\2P.P2PClient.csproj

     文件      19968  2009-01-07 11:51  P2P\2P.P2PClient\obj\Debug\2P.P2PClient.pdb

     文件      20480  2009-01-07 11:51  P2P\2P.P2PClient\obj\Debug\2P.P2PClient.exe

     文件       3150  2009-01-07 11:51  P2P\2P.P2PClient\obj\Debug\ResolveAssemblyReference.cache

     文件        373  2009-01-07 11:51  P2P\2P.P2PClient\obj\2P.P2PClient.csproj.FileListAbsolute.txt

     文件      10702  2009-01-06 17:06  P2P\2P.P2PClient\Program.cs

     文件       1197  2009-01-06 15:23  P2P\P2P.P2PServer\Properties\AssemblyInfo.cs

     文件       2651  2009-01-06 16:29  P2P\P2P.P2PServer\P2P.P2PServer.csproj

     文件      20480  2009-01-07 11:51  P2P\P2P.P2PServer\bin\Debug\P2P.WellKnown.dll

     文件      24064  2009-01-07 11:51  P2P\P2P.P2PServer\bin\Debug\P2P.WellKnown.pdb

     文件      20480  2009-01-07 11:51  P2P\P2P.P2PServer\bin\Debug\P2P.P2PServer.exe

     文件      19968  2009-01-07 11:51  P2P\P2P.P2PServer\bin\Debug\P2P.P2PServer.pdb

     文件       5632  2005-11-11 22:25  P2P\P2P.P2PServer\bin\Debug\P2P.P2PServer.vshost.exe

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

评论

共有 条评论

相关资源