• 大小: 91KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-12
  • 语言: 其他
  • 标签:

资源简介

基于UDP,P2P的通信示例,通过UDP打洞技术实现,可获取到局域网内机器的对外IP和路由映射IP和端口号,实现类似QQ的两个局域网内机器的通信

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using P2P.WellKnown;


namespace P2P.P2PClient
{
    /// 
    /// AppClass 的摘要说明。
    /// 

    public class AppClass
    {
        public static void Main()
        {
            //System.Net.WebClient wc = new System.Net.WebClient();
            //string ipAdress = wc.DownloadString(“http://www.zu14.cn/ip/“);
            IPHostEntry ihe = Dns.GetHostByName(Dns.GetHostName());
            IPAddress myIp = ihe.AddressList[0];
            Client client = new Client(myIp.ToString());
            Console.WriteLine(“输入用户名:“);
            string myname = Console.ReadLine();
            Console.WriteLine(“输入密码:“);
            string mypassword = Console.ReadLine();
            client.ConnectToServer(myname mypassword);
            client.Start();
            Console.WriteLine(“test arguments“);
            while (true)
            {
                string str = Console.ReadLine();
                client.PaserCommand(str);
            }
        }
    }
    /// 
    /// Client 的摘要说明。
    /// 

    public class Client : IDisposable
    {
        #region 定义各变量
        
        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;

        #endregion

        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(“输入以下命令操作符:\n“);
                Console.WriteLine(“格式为:\“send\“\“autosend\“\“exit\“\“getu\““);
                Console.WriteLine(“Example : send Username Message“);
                Console.WriteLine(“          autosend“);
                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);
            // 接受服

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

     文件       9728  2011-03-14 09:37  P2P.WellKnown\P2P.P2PClient\bin\Debug\P2P.P2PClient.exe

     文件      17920  2011-03-14 09:37  P2P.WellKnown\P2P.P2PClient\bin\Debug\P2P.P2PClient.pdb

     文件      11600  2011-03-14 09:38  P2P.WellKnown\P2P.P2PClient\bin\Debug\P2P.P2PClient.vshost.exe

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

     文件       7168  2011-03-14 09:22  P2P.WellKnown\P2P.P2PClient\bin\Debug\P2P.WellKnown.dll

     文件      24064  2011-03-14 09:22  P2P.WellKnown\P2P.P2PClient\bin\Debug\P2P.WellKnown.pdb

     文件       5874  2011-03-14 09:37  P2P.WellKnown\P2P.P2PClient\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件        543  2011-03-14 09:38  P2P.WellKnown\P2P.P2PClient\obj\x86\Debug\P2P.P2PClient.csproj.FileListAbsolute.txt

     文件       9728  2011-03-14 09:37  P2P.WellKnown\P2P.P2PClient\obj\x86\Debug\P2P.P2PClient.exe

     文件      17920  2011-03-14 09:37  P2P.WellKnown\P2P.P2PClient\obj\x86\Debug\P2P.P2PClient.pdb

     文件       2579  2011-03-13 13:28  P2P.WellKnown\P2P.P2PClient\P2P.P2PClient.csproj

     文件      11430  2011-03-14 09:37  P2P.WellKnown\P2P.P2PClient\Program.cs

     文件       1358  2011-03-13 13:27  P2P.WellKnown\P2P.P2PClient\Properties\AssemblyInfo.cs

     文件       8192  2011-03-14 09:22  P2P.WellKnown\P2P.P2PServer\bin\Debug\P2P.P2PServer.exe

     文件      15872  2011-03-14 09:22  P2P.WellKnown\P2P.P2PServer\bin\Debug\P2P.P2PServer.pdb

     文件      11600  2011-03-14 09:23  P2P.WellKnown\P2P.P2PServer\bin\Debug\P2P.P2PServer.vshost.exe

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

     文件       7168  2011-03-14 09:22  P2P.WellKnown\P2P.P2PServer\bin\Debug\P2P.WellKnown.dll

     文件      24064  2011-03-14 09:22  P2P.WellKnown\P2P.P2PServer\bin\Debug\P2P.WellKnown.pdb

     文件       5874  2011-03-14 09:22  P2P.WellKnown\P2P.P2PServer\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件        543  2011-03-14 09:23  P2P.WellKnown\P2P.P2PServer\obj\x86\Debug\P2P.P2PServer.csproj.FileListAbsolute.txt

     文件       8192  2011-03-14 09:22  P2P.WellKnown\P2P.P2PServer\obj\x86\Debug\P2P.P2PServer.exe

     文件      15872  2011-03-14 09:22  P2P.WellKnown\P2P.P2PServer\obj\x86\Debug\P2P.P2PServer.pdb

     文件       9107  2011-03-14 09:22  P2P.WellKnown\P2P.P2PServer\obj\x86\Debug\ResolveAssemblyReference.cache

     文件       2579  2011-03-13 13:27  P2P.WellKnown\P2P.P2PServer\P2P.P2PServer.csproj

     文件       6952  2011-03-14 09:16  P2P.WellKnown\P2P.P2PServer\Program.cs

     文件       1358  2011-03-13 13:24  P2P.WellKnown\P2P.P2PServer\Properties\AssemblyInfo.cs

     文件       7168  2011-03-14 09:22  P2P.WellKnown\P2P.WellKnown\bin\Debug\P2P.WellKnown.dll

     文件      24064  2011-03-14 09:22  P2P.WellKnown\P2P.WellKnown\bin\Debug\P2P.WellKnown.pdb

     文件       7744  2011-03-14 09:19  P2P.WellKnown\P2P.WellKnown\Class1.cs

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

评论

共有 条评论

相关资源