資源簡介
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個文件信息
- 上一篇:國家商用密碼算法開放動態庫及演示程序C#語言版本
- 下一篇:簡單的電子郵件收發系統
評論
共有 條評論