資源簡介
C# socket聊天 服務器轉發 由client 和server client直接能相互發送消息由服務器轉發

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Text;
using?System.Windows.Forms;
using?System.Net;
using?System.Net.Sockets;
using?System.IO;
using?System.Threading;
namespace?AsyncTcpClient
{
????public?partial?class?FormClient?:?Form
????{
????????//聲明公共變量
????????private?bool?isExit?=?false;
????????private?delegate?void?SetListBoxCallback(string?str);
????????private?SetListBoxCallback?setListBoxCallback;
????????private?delegate?void?SetRichTextBoxReceiveCallback(string?str);
????????private?SetRichTextBoxReceiveCallback?setRichTextBoxReceiveCallback;
????????private?delegate?void?SetcomboBoxCallback(string?str);
????????private?SetcomboBoxCallback?setComboBoxCallback;
????????private?TcpClient?client;
????????private?NetworkStream?networkStream;
????????private?EventWaitHandle?allDone?=?new?EventWaitHandle(false?EventResetMode.ManualReset);
????????public?FormClient()
????????{
????????????InitializeComponent();
????????}
????????//窗體加載
????????private?void?FormClient_Load(object?sender?EventArgs?e)
????????{
????????????listBoxStatus.HorizontalScrollbar?=?true;
????????????setListBoxCallback?=?new?SetListBoxCallback(SetListBox);
????????????setRichTextBoxReceiveCallback?=?new?SetRichTextBoxReceiveCallback(SetRichTextBoxReceive);
????????????setComboBoxCallback?=?new?SetcomboBoxCallback(SetComboBox);
????????}
????????//連接事件
????????private?void?buttonConnect_Click(object?sender?EventArgs?e)
????????{
????????????//使用IPv4,實現應用時要將Dns.GetHostName()改為服務器名
????????????client?=?new?TcpClient(AddressFamily.InterNetwork);
????????????IPAddress?serverIP?=?IPAddress.Parse(tbxIP.Text);
????????????AsyncCallback?requestCallback?=?new?AsyncCallback(RequestCallback);
????????????allDone.Reset();
????????????//開始對遠程主機的異步請求
????????????client.BeginConnect(serverIP?51888?requestCallback?client);
????????????listBoxStatus.Invoke(setListBoxCallback?string.Format(“本機EndPoint:{0}“?client.Client.LocalEndPoint));
????????????listBoxStatus.Invoke(setListBoxCallback?“開始與服務器建立連接“);
????????????//阻塞線程直到BeginConnect完成,連接完成自動調用RequestCallback中的Set方法解除阻塞
????????????allDone.WaitOne();
????????}
????????//發送事件
????????private?void?buttonSend_Click(object?sender?EventArgs?e)
????????{
????????????if?(comboBox1.SelectedItem?!=?null)
????????????{
????????????????SendString(richTextBoxSend.Text?+?“;“?+?comboBox1.SelectedItem.ToString()?+?“;“);
????????????????richTextBoxSend.Clear();
????????????}
????????????else
????????????{
????????????????MessageBox.Show(“請選擇一個接收者“);
????????????}
????????}
????????private?void?RequestCallback(IAsyncResult?ar)
????????{
????????????//異步執行到此,說明BeginConnect已完成,但未結束,需要解除阻塞,并調用EndConnect
????????????allDone.Set();
????????????try
????????????{
????????????????client?=?(TcpClient)ar.AsyncState;
????????????????client.EndConnect(ar);
????????????????listBoxStatus.Invo
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????3608??2012-11-21?14:07??client\AsyncTcpClient\AsyncTcpClient.csproj
?????文件????????452??2012-11-21?14:07??client\AsyncTcpClient\AsyncTcpClient.csproj.user
?????文件??????15360??2012-11-21?16:44??client\AsyncTcpClient\bin\Debug\AsyncTcpClient.exe
?????文件??????40448??2012-11-21?16:44??client\AsyncTcpClient\bin\Debug\AsyncTcpClient.pdb
?????文件??????21464??2012-11-21?16:46??client\AsyncTcpClient\bin\Debug\AsyncTcpClient.vshost.exe
?????文件????????490??2010-03-17?22:39??client\AsyncTcpClient\bin\Debug\AsyncTcpClient.vshost.exe.manifest
?????文件???????7329??2012-11-21?16:43??client\AsyncTcpClient\FormClient.cs
?????文件???????9500??2012-11-21?15:44??client\AsyncTcpClient\FormClient.Designer.cs
?????文件???????5814??2012-11-21?15:44??client\AsyncTcpClient\FormClient.resx
?????文件????????874??2011-02-15?10:43??client\AsyncTcpClient\obj\AsyncTcpClient.csproj.FileListAbsolute.txt
?????文件???????1137??2012-11-21?16:46??client\AsyncTcpClient\obj\Debug\AsyncTcpClient.csproj.FileListAbsolute.txt
?????文件????????852??2012-11-21?15:45??client\AsyncTcpClient\obj\Debug\AsyncTcpClient.csproj.GenerateResource.Cache
?????文件???????1339??2012-11-21?14:37??client\AsyncTcpClient\obj\Debug\AsyncTcpClient.csprojResolveAssemblyReference.cache
?????文件??????15360??2012-11-21?16:44??client\AsyncTcpClient\obj\Debug\AsyncTcpClient.exe
?????文件????????180??2012-11-21?15:45??client\AsyncTcpClient\obj\Debug\AsyncTcpClient.FormClient.resources
?????文件??????40448??2012-11-21?16:44??client\AsyncTcpClient\obj\Debug\AsyncTcpClient.pdb
?????文件????????180??2012-11-21?14:07??client\AsyncTcpClient\obj\Debug\AsyncTcpClient.Properties.Resources.resources
?????文件????????789??2012-11-21?14:15??client\AsyncTcpClient\obj\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件???????6091??2012-11-21?16:45??client\AsyncTcpClient\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件???????4608??2012-11-21?14:15??client\AsyncTcpClient\obj\Debug\TempPE\Properties.Resources.Designer.cs.dll
?????文件????????478??2011-02-10?15:55??client\AsyncTcpClient\Program.cs
?????文件???????1200??2011-02-10?15:55??client\AsyncTcpClient\Properties\AssemblyInfo.cs
?????文件???????2856??2012-11-21?14:07??client\AsyncTcpClient\Properties\Resources.Designer.cs
?????文件???????5612??2011-02-10?15:55??client\AsyncTcpClient\Properties\Resources.resx
?????文件???????1097??2012-11-21?14:07??client\AsyncTcpClient\Properties\Settings.Designer.cs
?????文件????????249??2011-02-10?15:55??client\AsyncTcpClient\Properties\Settings.settings
?????文件????????512??2011-02-10?16:00??client\AsyncTcpClient\Readob
?????文件????????932??2012-11-21?14:07??client\AsyncTcpClient.sln
????..A..H.?????14336??2011-02-15?10:43??client\AsyncTcpClient.suo
????..A..H.?????29184??2012-11-21?16:48??client\AsyncTcpClient.v11.suo
............此處省略53個文件信息
- 上一篇:新浪新聞RSS閱讀器C#版
- 下一篇:C#凱撒密碼的原理與實現
評論
共有 條評論