資源簡介
本文旨在用C#面向對象的方法實現P2P(Peer To Peer)(類QQ)會話系統。本文可作為學習C# P2P通信與面向對象程序設計的用例(包括所有源碼)。
一、 通信組件:System.Net.Sockets.System.Net.Sockets.UdpClient。
二、 通信原理: 利用P2P之UDP穿透Cone(錐型)NAPT原理。
三、 系統特點:
1、所有會話命令均封裝成對象。
2、每個命令對象都有Send(發送)與Exec();執行方法。Send方法用于把對象(自己)發到對方,Exec()方法完成對象的功能。
3、主要命令:Login(用戶登錄(C->S))、OnlineList(回復當前在線用戶列表(S->C))、Upline(用戶上線(S->C))、Logout(用戶登出(C->S))、Leaveline(用戶離線(C->C))、
Chat(會話(交談、閑聊)(C->C || C->S->C))Relay(服務器轉發)(C->S)、Abort(終止會話)(C->C || C->S->C)、Shutdown(停止服務)(S->S) 等。

代碼片段和文件信息
using?System;
using?System.Net;
using?System.Threading;
using?System.Windows.Forms;
using?System.Threading.Tasks;
using?ChatLibrary;
namespace?ChatClient
{
????public?partial?class?ChatClient?:?Form
????{
????????private?SynchronizationContext?context;?//保存主線程(UI)上下文
????????private?ChatReceiver?receiver;?//P2P核心客戶端類
????????private?IPEndPoint?server;?//服務器端點
????????private?string?title;?//緩存窗口標題
????????private?User?user;?//我方用戶
????????public?ChatClient()
????????{
????????????InitializeComponent();
????????????context?=?SynchronizationContext.Current;
????????}
????????private?void?ChatClient_Load(object?sender?EventArgs?e)
????????{
????????????tbUser.Enabled?=?true;
????????????tbServer.Enabled?=?tbUser.Enabled;
????????????tbPort.Enabled?=?tbUser.Enabled;
????????????btnLogin.Enabled?=?tbUser.Enabled;
????????????btnLogout.Enabled?=?!btnLogin.Enabled;
????????????title?=?Text;
????????????tlState.Text?=?“Ready.“;
????????}
????????public?void?Calling(object?sender?SessionEventArgs?e)?//呼叫發起事件處理程序
????????{
????????????int?index?=?lbUsers.Items.IndexOf(e.messenger.user);
????????????if?(index?>?-1)
????????????{
????????????????ChatForm?form?=?(ChatForm)((Friend)lbUsers.Items[index]).form;
????????????????if?(form?==?null?||?form.IsDisposed)
????????????????{
????????????????????form?=?new?ChatForm(receiver?user?e.messenger.user?false);
????????????????????((Friend)lbUsers.Items[index]).form?=?form;
????????????????}
????????????????context.Post((state)?=>?{?form.tbReceive.AppendText(e.messenger.message?+?“\r\n“);?form.Show();?}?null);?//刷新窗口及其控件必須用主(UI)線程
????????????????/*
????????????????this.Invoke(new?Action(()?=>?
????????????????{
????????????????????form.tbReceive.AppendText(e.messenger.message?+?“\r\n“);
????????????????????form.Show();
????????????????}));
????????????????*/
????????????}
????????}
????????private?ChatForm?SessionForm(SessionEventArgs?e)
????????{
????????????int?index?=?lbUsers.Items.IndexOf(e.messenger.user);
????????????if?(index?>?-1)
????????????{
????????????????ChatForm?form?=?(ChatForm)((Friend)lbUsers.Items[index]).form;
????????????????if?(form?==?null?||?form.IsDisposed)
????????????????{
????????????????????form?=?new?ChatForm(receiver?user?e.messenger.user?false);
????????????????????((Friend)lbUsers.Items[index]).form?=?form;
????????????????}
????????????????this.Invoke(new?Action(()?=>
????????????????{
????????????????????form.tbReceive.AppendText(e.messenger.message?+?“\r\n“);
????????????????}));
????????????????return?form;
????????????}
????????????return?null;
????????}
????????public?void?CallReply(object?sender?SessionEventArgs?e)?//呼叫回復事件處理程序
????????{
????????????SessionForm(e);
????????}
????????public?void?TypeNotify(object?sender?TypeNotifyEventArgs?e)?//類型通知事件處理程序
????????{
????????????ChatForm?form?=?SessionForm(new?SessionEventArgs(e.messenger));
????????????if?(form?!=?null)?
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????189??2015-05-19?10:11??P2P\ChatClient\App.config
?????文件??????26112??2015-06-02?23:20??P2P\ChatClient\bin\Debug\ChatClient.exe
?????文件????????189??2015-05-19?10:11??P2P\ChatClient\bin\Debug\ChatClient.exe.config
?????文件??????38400??2015-06-02?23:20??P2P\ChatClient\bin\Debug\ChatClient.pdb
?????文件??????23168??2015-06-02?20:30??P2P\ChatClient\bin\Debug\ChatClient.vshost.exe
?????文件????????189??2015-05-19?10:11??P2P\ChatClient\bin\Debug\ChatClient.vshost.exe.config
?????文件????????490??2013-03-18?17:00??P2P\ChatClient\bin\Debug\ChatClient.vshost.exe.manifest
?????文件??????23552??2015-06-02?23:20??P2P\ChatClient\bin\Debug\ChatLibrary.dll
?????文件??????62976??2015-06-02?23:20??P2P\ChatClient\bin\Debug\ChatLibrary.pdb
?????文件???????5120??2015-06-02?23:20??P2P\ChatClient\bin\Debug\Win32API.dll
?????文件??????13824??2015-06-02?23:20??P2P\ChatClient\bin\Debug\Win32API.pdb
?????文件???????6742??2015-06-02?21:41??P2P\ChatClient\ChatClient.cs
?????文件???????4438??2015-05-28?08:24??P2P\ChatClient\ChatClient.csproj
?????文件???????9232??2015-05-30?16:48??P2P\ChatClient\ChatClient.Designer.cs
?????文件??????11185??2015-05-30?16:48??P2P\ChatClient\ChatClient.resx
?????文件???????2588??2015-06-02?22:44??P2P\ChatClient\ChatForm.cs
?????文件???????6320??2015-06-02?09:54??P2P\ChatClient\ChatForm.Designer.cs
?????文件???????7638??2015-06-02?09:54??P2P\ChatClient\ChatForm.resx
?????文件???????3922??2015-06-02?23:20??P2P\ChatClient\obj\Debug\ChatClient.ChatClient.resources
?????文件???????1332??2015-06-02?23:20??P2P\ChatClient\obj\Debug\ChatClient.ChatForm.resources
?????文件???????3151??2015-06-02?23:20??P2P\ChatClient\obj\Debug\ChatClient.csproj.FileListAbsolute.txt
?????文件???????1043??2015-06-02?23:20??P2P\ChatClient\obj\Debug\ChatClient.csproj.GenerateResource.Cache
?????文件??????10518??2015-06-02?23:20??P2P\ChatClient\obj\Debug\ChatClient.csprojResolveAssemblyReference.cache
?????文件??????26112??2015-06-02?23:20??P2P\ChatClient\obj\Debug\ChatClient.exe
?????文件??????38400??2015-06-02?23:20??P2P\ChatClient\obj\Debug\ChatClient.pdb
?????文件????????180??2015-06-02?23:20??P2P\ChatClient\obj\Debug\ChatClient.Properties.Resources.resources
?????文件???????1465??2015-05-20?21:57??P2P\ChatClient\obj\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件???????7259??2015-05-28?08:23??P2P\ChatClient\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件??????????0??2015-05-19?10:11??P2P\ChatClient\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
?????文件??????????0??2015-05-19?10:11??P2P\ChatClient\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
............此處省略153個文件信息
- 上一篇:曲線擬合&&線性回歸C#)
- 下一篇:漢諾塔動態演示程序C#完整項目
評論
共有 條評論