資源簡介
一個實現一個服務器 多個客戶端間通信的demo,詳情可以見我"C# Socket異步通信demo,一個服務器對應多個客戶端"這篇文章,謝謝

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Linq;
using?System.Net;
using?System.Net.Sockets;
using?System.Text;
using?System.Threading.Tasks;
using?System.Windows.Forms;
namespace?SocketTest
{
????public?partial?class?frmClient?:?Form
????{
????????private?byte[]?result?=?new?byte[1024];
????????private?Socket?clientSocket?=?new?Socket(AddressFamily.InterNetwork?SocketType.Stream?ProtocolType.Tcp);
????????private?int?port?;
????????System.Timers.Timer?timer?=?new?System.Timers.Timer(2000);
????????public?frmClient()
????????{
????????????InitializeComponent();
????????????txtIp.Text?=?“127.0.0.1“;
????????????txtPort.Text?=?“8888“;
????????????Random?r?=?new?Random();
????????????txtSendDetails.Text?=?r.Next(10000).ToString();
?????????
????????}
????????private?void?Timer_Elapsed(object?sender?System.Timers.ElapsedEventArgs?e)
????????{
????????????Send(txtSendDetails.Text.Trim());
????????}
????????private?void?btnConnection_Click(object?sender?EventArgs?e)
????????{
????????????txtClient.Text?+=?“連接“?+?txtIp.Text?+?“:“?+?txtPort.Text?+?“\r\n“;
????????????syConnect();
????????}
????????private?void?btnSend_Click(object?sender?EventArgs?e)
????????{
????????????if?(Send(txtSendDetails.Text.Trim())?&&?txtSendDetails.Text.Trim()?!=?““)
????????????{
????????????????txtClient.Text?+=?“已發送:“?+?txtSendDetails.Text.Trim()?+?“\r\n“;
????????????????timer.Elapsed?+=?Timer_Elapsed;
????????????????timer.Start();
????????????}
????????}
????????public?void?syConnect()
????????{
????????????try
????????????{
????????????????IPAddress?ip?=?IPAddress.Parse(txtIp.Text.ToString());
????????????????port?=?int.Parse(txtPort.Text.Trim());
????????????????IPEndPoint?endPoint?=?new?IPEndPoint(ip?int.Parse(txtPort.Text.Trim()));
????????????????//clientSocket.BeginConnect(ip?port?new?AsyncCallback(ConnectCallback)?clientSocket);
????????????????//開始連接到服務器
????????????????clientSocket.BeginConnect(endPoint?asyncResult?=>
????????????????{
????????????????????clientSocket.EndConnect(asyncResult);
????????????????}?null);
????????????????txtClient.Text?+=?“連接服務器成功“?+?“\r\n“;
????????????}
????????????catch(Exception?ex)
????????????{
????????????????txtClient.Text?+=?“連接服務器失敗“?+?“\r\n“;
????????????}
????????}
????????private?void?ConnectCallback(IAsyncResult?ar)
????????{
????????????try
????????????{
????????????????Socket?handler?=?(Socket)ar.AsyncState;
????????????????handler.EndConnect(ar);
????????????}
????????????catch?(SocketException?ex)
????????????{
????????????????MessageBox.Show(ex.ToString());
????????????}
????????}
????????///發送消息
????????public?Boolean?Send(string?data)
????????{
????????????try
????????????{
????????????????//Send(System.Text.Encoding.UTF8.GetBytes(data));
????????????????byte[]?bydata?=?System.Text.Encoding.ASCII.GetBytes(data);
????????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
????..A..H.?????45568??2016-03-31?17:40??SocketTest\.vs\SocketTest\v14\.suo
????I.A....???????170??2016-03-31?13:45??SocketTest\SocketTest\App.config
?????文件??????16896??2016-04-01?08:59??SocketTest\SocketTest\bin\Debug\SocketTest.exe
?????文件????????170??2016-03-31?13:45??SocketTest\SocketTest\bin\Debug\SocketTest.exe.config
?????文件??????34304??2016-04-01?08:59??SocketTest\SocketTest\bin\Debug\SocketTest.pdb
?????文件??????22688??2016-04-01?09:00??SocketTest\SocketTest\bin\Debug\SocketTest.vshost.exe
?????文件????????170??2016-03-31?13:45??SocketTest\SocketTest\bin\Debug\SocketTest.vshost.exe.config
?????文件????????490??2015-06-04?12:48??SocketTest\SocketTest\bin\Debug\SocketTest.vshost.exe.manifest
????I.A....??????3659??2016-04-01?08:59??SocketTest\SocketTest\frmClient.cs
????I.A....??????7842??2016-03-31?14:52??SocketTest\SocketTest\frmClient.Designer.cs
?????文件???????5817??2016-03-31?14:52??SocketTest\SocketTest\frmClient.resx
????I.A....??????4457??2016-04-01?08:57??SocketTest\SocketTest\frmServer.cs
????I.A....??????6754??2016-03-31?16:09??SocketTest\SocketTest\frmServer.Designer.cs
????I.A....??????5817??2016-03-31?16:09??SocketTest\SocketTest\frmServer.resx
?????文件???????1452??2016-04-01?08:34??SocketTest\SocketTest\obj\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件???????7047??2016-03-31?14:13??SocketTest\SocketTest\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件???????1135??2016-04-01?09:00??SocketTest\SocketTest\obj\Debug\SocketTest.csproj.FileListAbsolute.txt
?????文件???????1079??2016-03-31?16:09??SocketTest\SocketTest\obj\Debug\SocketTest.csproj.GenerateResource.Cache
?????文件???????2210??2016-03-31?14:11??SocketTest\SocketTest\obj\Debug\SocketTest.csprojResolveAssemblyReference.cache
?????文件??????16896??2016-04-01?08:59??SocketTest\SocketTest\obj\Debug\SocketTest.exe
?????文件????????180??2016-03-31?14:52??SocketTest\SocketTest\obj\Debug\SocketTest.frmClient.resources
?????文件????????180??2016-03-31?16:09??SocketTest\SocketTest\obj\Debug\SocketTest.frmServer.resources
?????文件??????34304??2016-04-01?08:59??SocketTest\SocketTest\obj\Debug\SocketTest.pdb
?????文件????????180??2016-03-31?14:23??SocketTest\SocketTest\obj\Debug\SocketTest.Properties.Resources.resources
?????文件???????3584??2016-03-31?13:47??SocketTest\SocketTest\obj\Debug\TempPE\Properties.Resources.Designer.cs.dll
????I.A....???????526??2016-03-31?13:45??SocketTest\SocketTest\Program.cs
????I.A....??????1340??2016-03-31?13:45??SocketTest\SocketTest\Properties\AssemblyInfo.cs
????I.A....??????2860??2016-03-31?13:45??SocketTest\SocketTest\Properties\Resources.Designer.cs
????I.A....??????5612??2016-03-31?13:45??SocketTest\SocketTest\Properties\Resources.resx
????I.A....??????1110??2016-03-31?13:45??SocketTest\SocketTest\Properties\Settings.Designer.cs
............此處省略18個文件信息
評論
共有 條評論