程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> c#中異步基於消息通信的完成端口的TCP/IP協議的組件實現(源代碼)

c#中異步基於消息通信的完成端口的TCP/IP協議的組件實現(源代碼)

編輯:關於C語言
源代碼:

客戶端:

using System;
using System.IO;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace MyKJ
{
?///
?/// MyTcpIpClIEnt 提供在Net TCP_IP 協議上基於消息的客戶端
?///
?public class MyTcpIpClIEnt : System.ComponentModel.Component
?{
??private int bufferSize=2048;
??private string tcpIpServerIP="127.0.0.1";
??private int tcpIpServerPort=11000;
??private Socket ClIEntSocket=null;
??private ManualResetEvent connectDone = new ManualResetEvent(false);
??private ManualResetEvent sendDone = new ManualResetEvent(false);
??
??private void ConnectCallback(IAsyncResult ar)
??{
???try
???{
????Socket clIEnt = (Socket) ar.AsyncState;
????clIEnt.EndConnect(ar);
????
???}
???catch (Exception e)
???{
????OnErrorEvent(new ErrorEventArgs(e));
???}
???finally
???{
????connectDone.Set();
???}
??}
??private void SendCallback(IAsyncResult ar)
??{
???try
???{
????Socket clIEnt = (Socket) ar.AsyncState;
????int bytesSent = clIEnt.EndSend(ar);
????//Console.WriteLine(bytesSent);
???}
???catch (Exception e)
???{
????OnErrorEvent(new ErrorEventArgs(e));
???}
???finally
???{
????sendDone.Set();
???}
??}
??private void ReceiveCallback(IAsyncResult ar)
??{
???Socket handler=null;
???try
???{
????lock(ar)
????{
?????StateObject state = (StateObject) ar.AsyncState;
?????handler = state.workSocket;
?????
?????int bytesRead = handler.EndReceive(ar);
?????
?????if (bytesRead > 0)
?????{
??????int ReadPiont=0;?
??????while(ReadPiont??????{?
???????if(state.Cortrol==0 && ReadPiont???????{
????????long bi1=state.buffer[ReadPiont];
????????bi1=(bi1<<24)&0xff000000;
????????state.packSize=bi1;
????????ReadPiont++;
????????state.Cortrol=1;
???????}
??????
???????if(state.Cortrol==1 && ReadPiont???????{
????????long bi1=state.buffer[ReadPiont];
????????bi1=(bi1<<16)&0x00ff0000;
????????state.packSize=state.packSize+bi1;
????????ReadPiont++;
????????state.Cortrol=2;
???????}
??????
???????if(state.Cortrol==2 && ReadPiont???????{
????????long bi1=state.buffer[ReadPiont];
????????bi1=(bi1<<8)&0x0000ff00;
????????state.packSize=state.packSize+bi1;
????????ReadPiont++;
????????state.Cortrol=3;
???????}
???????
???????if(state.Cortrol==3 && ReadPiont???????{
????????long bi1=state.buffer[ReadPiont];
????????bi1=bi1&0xff;
????????state.packSize=state.packSize+bi1-4;
????????ReadPiont++;
????????state.Cortrol=4;
???????}
???????
???????if(state.Cortrol==4 && ReadPiont???????{
????????long bi1=state.buffer[ReadPiont];
????????bi1=(bi1<<24)&0xff000000;
????????state.residualSize=bi1;
????????ReadPiont++;
????????state.Cortrol=5;
????????state.packSize-=1;
???????}
???????
???????if(state.Cortrol==5 && ReadPiont???????{
????????long bi1=state.buffer[ReadPiont];
????????bi1=(bi1<<16)&0x00ff0000;
????????state.residualSize=state.residualSize+bi1;
????????ReadPiont++;
????????state.Cortrol=6;
????????state.packSize-=1;
???????}
???????
???????if(state.Cortrol==6 && ReadPiont???????{
????????long bi1=state.buffer[ReadPiont];
????????bi1=(bi1<<8)&0x0000ff00;
????????state.residualSize=state.residualSize+bi1;
????????ReadPiont++;
????????state.Cortrol=7;
????????state.packSize-=1;
???????}
???????if(state.Cortrol==7 && ReadPiont???????{
????????long bi1=state.buffer[ReadPiont];
????????bi1=bi1&0xff;
????????state.residualSize=state.residualSize+bi1;
????????state.Datastream.SetLength(0);
????????state.Datastream.Position=0;
????????
????????ReadPiont++;
????????state.Cortrol=8;
????????state.packSize-=1;
???????}
???????
???????if(state.Cortrol==8 && ReadPiont???????{
????????int bi1=bytesRead-ReadPiont;
????????int bi2=(int)(state.residualSize-state.Datastream.Length);
????????if(bi1>=bi2)
????????{
?????????state.Datastream.Write(state.buffer,ReadPiont,bi2);
?????????ReadPiont+=bi2;
?????????OnInceptEvent(new InceptEventArgs(state.Datastream,handler));
?????????state.Cortrol=9;
?????????state.packSize-=bi2;
?????????}
????????else
????????{
?????????state.Datastream.Write(state.buffer,ReadPiont,bi1);
?????????ReadPiont+=bi1;
?????????state.packSize-=bi1;
????????}
???????}
???????if(state.Cortrol==9 && ReadPiont???????{
????????int bi1=bytesRead-ReadPiont;
????????if(bi1????????{
?????????state.packSize=state.packSize-bi1;
?????????ReadPiont+=bi1;
????????}?
????????else
????????{
?????????state.Cortrol=0;
?????????ReadPiont+=(int)state.packSize;
????????}
???????}
??????}
?????}
?????else
?????{
??????throw(new Exception("讀入的數據小於1bit"));
?????}
?????if(handler.Connected==true)
?????{
??????handler.BeginReceive(state.buffer,0,bufferSize,0,
???????new AsyncCallback(ReceiveCallback), state);
?????}
????}
???}
???catch (Exception e)
???{
????OnErrorEvent(new ErrorEventArgs(e));
????
???}
??}
??
??///
??/// 連接服務器
??///
??public void Conn()
??{
???try
???{
????ClIEntSocket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);?
????IPAddress ipAddress = IPAddress.Parse(tcpIpServerIP);
????IPEndPoint remoteEP = new IPEndPoint(ipAddress, tcpIpServerPort);
????connectDone.Reset();
????ClientSocket.BeginConnect(remoteEP,new AsyncCallback(ConnectCallback),ClIEntSocket);
????connectDone.WaitOne();
????StateObject state = new StateObject(bufferSize,ClIEntSocket);
????ClIEntSocket.BeginReceive(state.buffer,0,bufferSize,0,
?????new AsyncCallback(ReceiveCallback), state);?
???}
???catch(Exception e)
???{
????OnErrorEvent(new ErrorEventArgs(e));
???}
???
??}
??///
??/// 斷開連接
??///
??public void Close()
??{
???try
???{
????ClIEntSocket.Shutdown(SocketShutdown.Both);
????ClIEntSocket.Close();
???}
???catch(Exception e)
???{
????OnErrorEvent(new ErrorEventArgs(e));
???}
???
??}
??///
??/// 發送一個流數據
??///
??/// 數據流
??public void Send(Stream Astream)
??{
???try
???{
????if(ClIEntSocket.Connected==false)
????{
?????throw(new Exception("沒有連接服務器不可以發送信息!"));
????}
????Astream.Position=0;
????byte[] byteData=new byte[bufferSize];
????int bi1=(int)((Astream.Length+8)/bufferSize);
????int bi2=(int)Astream.Length;
????if(((Astream.Length+8)%bufferSize)>0)
????{
?????bi1=bi1+1;
????}
????bi1=bi1*bufferSize;
????
????byteData[0]=System.Convert.ToByte(bi1>>24);
????byteData[1]=System.Convert.ToByte((bi1&0x00ff0000)>>16);
????byteData[2]=System.Convert.ToByte((bi1&0x0000ff00)>>8);
????byteData[3]=System.Convert.ToByte((bi1&0x000000ff));
????
????byteData[4]=System.Convert.ToByte(bi2>>24);
????byteData[5]=System.Convert.ToByte((bi2&0x00ff0000)>>16);
????byteData[6]=System.Convert.ToByte((bi2&0x0000ff00)>>8);
????byteData[7]=System.Convert.ToByte((bi2&0x000000ff));
????
????int n = Astream.Read(byteData, 8, byteData.Length-8);
????
????while (n>0)
????{
?????ClientSocket.BeginSend(byteData, 0, byteData.Length, 0,?new AsyncCallback(SendCallback), ClIEntSocket);
?????sendDone.WaitOne();
?????byteData=new byte[bufferSize];
?????n = Astream.Read(byteData,0,byteData.Length);
????}
???}
???catch (Exception e)
???{
????OnErrorEvent(new ErrorEventArgs(e));
???}
??}
??
??///
??/// 構造
??///
??/// 父控件
??public MyTcpIpClIEnt(System.ComponentModel.IContainer container)
??{
???container.Add(this);
???InitializeComponent();

???//
???// TODO: 在 InitializeComponent 調用後添加任何構造函數代碼
???//
??}
??///
??/// 構造
??///
??public MyTcpIpClIEnt()
??{
???InitializeComponent();

???//
???// TODO: 在 InitializeComponent 調用後添加任何構造函數代碼
???//
??}

??#region Component Designer generated code
??///
??/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
??/// 此方法的內容。
??///
??private void InitializeComponent()
??{

??}
??#endregion

??///
??/// 要連接的服務器IP地址
??///
??public string TcpIpServerIP
??{
???get
???{
????return tcpIpServerIP;
???}
???set
???{
????tcpIpServerIP=value;
???}
??}

??///
??/// 要連接的服務器所使用的端口
??///
??public int TcpIpServerPort
??{
???get
???{
????return tcpIpServerPort;
???}
???set
???{
????tcpIpServerPort=value;
???}
??}

??///
??/// 緩沖器大小
??///
??public int BufferSize
??{
???get
???{
????return bufferSize;
???}
???set
???{
????bufferSize=value;
???}
??}
??
??///
??/// 連接的活動狀態
??///
??public bool Activ
??{
???get
???{
????if(ClIEntSocket==null)
????{
?????return false;
????}
????return ClIEntSocket.Connected;
???}
??}
??///
??/// 接收到數據引發的事件
??///
??public event InceptEvent Incept;
??///
??/// 引發接收數據事件
??///
??/// 接收數據
??protected virtual void OnInceptEvent(InceptEventArgs e)
??{
???if (Incept != null)
???{
????Incept(this, e);
???}
??}
??///
??/// 發生錯誤引發的事件
??///
??public event ErrorEvent Error;
??///
??/// 引發錯誤事件
??///
??/// 錯誤數據
??protected virtual void OnErrorEvent(ErrorEventArgs e)
??{
???if (Error != null)
???{
????Error(this, e);
???}
??}
??
?}
?
?///
?/// 接收數據事件
?///
?public class InceptEventArgs : EventArgs
?{?
??private readonly Stream datastream;
??private readonly Socket clIEntSocket;
??///
??/// 構造
??///
??/// 接收到的數據
??/// 接收的插座
??public InceptEventArgs(Stream Astream,Socket ClIEntSocket)
??{
???datastream=Astream;
???clientSocket=ClIEntSocket;
??}
??///
??/// 接受的數據流
??///
??public Stream Astream
??{????
???get { return datastream;}?????
??}
??///
??/// 接收的插座
??///
??public Socket ClIEntSocket
??{????
???get { return clIEntSocket;}?????
??}
?}?
?///
?/// 定義接收委托
?///
?public delegate void InceptEvent(object sender, InceptEventArgs e);
?///
?/// 錯處事件
?///
?public class ErrorEventArgs : EventArgs
?{?
??private readonly Exception error;
??///
??/// 構造
??///
??/// 錯誤信息對象
??public ErrorEventArgs(Exception Error)
??{
???error=Error;
??}
??///
??/// 錯誤信息對象
??///
??public Exception Error
??{????
???get { return error;}?????
??}
?}
?///
?/// 錯誤委托
?///
?public delegate void ErrorEvent(object sender, ErrorEventArgs e);
?
?
?
}


服務器端:

using System;
using System.IO;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace MyKJ
{
?///
?/// MyTcpIpClIEnt 提供在Net TCP_IP 協議上基於消息的服務端
?///
?public class MyTcpIpServer : System.ComponentModel.Component
?{
??private int bufferSize=2048;
??private string tcpIpServerIP="";
??private int tcpIpServerPort=11000;
??private Socket listener=null;
??private ManualResetEvent allDone = new ManualResetEvent(false);
??private ManualResetEvent sendDone = new ManualResetEvent(false);
??private Thread thread=null;
??
??private void StartListening()
??{
???try
???{
????listener = new Socket(AddressFamily.InterNetwork,
?????SocketType.Stream, ProtocolType.Tcp);
?????
????IPAddress ipAddress;
????if(tcpIpServerIP.Trim()=="")
????{
?????ipAddress=IPAddress.Any;?
????}
????else
????{
?????ipAddress=IPAddress.Parse(tcpIpServerIP);
????}
????IPEndPoint localEndPoint = new IPEndPoint(ipAddress, tcpIpServerPort);
????
????listener.Bind(localEndPoint);
????listener.Listen(10);
????while (true)
????{
?????allDone.Reset();
?????listener.BeginAccept(new AsyncCallback(AcceptCallback),listener);
?????allDone.WaitOne();
????}
???}
???catch (Exception e)
???{
????OnErrorServerEvent(new ErrorServerEventArgs(e,listener));?
???}
??}
??
??private void ReadCallback(IAsyncResult ar)
??{
???Socket handler=null;
???try
???{
????lock(ar)
????{
?????StateObject state = (StateObject) ar.AsyncState;
?????handler = state.workSocket;
?????
?????int bytesRead = handler.EndReceive(ar);
?????
?????if (bytesRead > 0)
?????{
??????int ReadPiont=0;?
??????while(ReadPiont??????{?
???????if(state.Cortrol==0 && ReadPiont???????{
????????long bi1=state.buffer[ReadPiont];
????????bi1=(bi1<<24)&0xff000000;
????????state.packSize=bi1;
????????ReadPiont++;
????????state.Cortrol=1;
???????}
??????
???????if(state.Cortrol==1 && ReadPiont???????{
????????long bi1=state.buffer[ReadPiont];
????????bi1=(bi1<<16)&0x00ff0000;
????????state.packSize=state.packSize+bi1;
????????ReadPiont++;
????????state.Cortrol=2;
???????}
??????
???????if(state.Cortrol==2 && ReadPiont???????{
????????long bi1=state.buffer[ReadPiont];
????????bi1=(bi1<<8)&0x0000ff00;
????????state.packSize=state.packSize+bi1;
????????ReadPiont++;
????????state.Cortrol=3;
???????}
???????
???????if(state.Cortrol==3 && ReadPiont???????{
????????long bi1=state.buffer[ReadPiont];
????????bi1=bi1&0xff;
????????state.packSize=state.packSize+bi1-4;
????????ReadPiont++;
????????state.Cortrol=4;
???????}
???????
???????if(state.Cortrol==4 && ReadPiont???????{
????????long bi1=state.buffer[ReadPiont];
????????bi1=(bi1<<24)&0xff000000;
????????state.residualSize=bi1;
????????ReadPiont++;
????????state.Cortrol=5;
????????state.packSize-=1;
???????}
???????
???????if(state.Cortrol==5 && ReadPiont???????{
????????long bi1=state.buffer[ReadPiont];
????????bi1=(bi1<<16)&0x00ff0000;
????????state.residualSize=state.residualSize+bi1;
????????ReadPiont++;
????????state.Cortrol=6;
????????state.packSize-=1;
???????}
???????
???????if(state.Cortrol==6 && ReadPiont???????{
????????long bi1=state.buffer[ReadPiont];
????????bi1=(bi1<<8)&0x0000ff00;
????????state.residualSize=state.residualSize+bi1;
????????ReadPiont++;
????????state.Cortrol=7;
????????state.packSize-=1;
???????}
???????if(state.Cortrol==7 && ReadPiont???????{
????????long bi1=state.buffer[ReadPiont];
????????bi1=bi1&0xff;
????????state.residualSize=state.residualSize+bi1;
????????state.Datastream.SetLength(0);
????????state.Datastream.Position=0;
????????
????????ReadPiont++;
????????state.Cortrol=8;
????????state.packSize-=1;
???????}
???????
???????if(state.Cortrol==8 && ReadPiont???????{
????????int bi1=bytesRead-ReadPiont;
????????int bi2=(int)(state.residualSize-state.Datastream.Length);
????????if(bi1>=bi2)
????????{
?????????state.Datastream.Write(state.buffer,ReadPiont,bi2);
?????????ReadPiont+=bi2;
?????????OnInceptServerEvent(new InceptServerEventArgs(state.Datastream,state.workSocket,this));
?????????state.Cortrol=9;
?????????state.packSize-=bi2;
?????????
?????????
????????}
????????else
????????{
?????????state.Datastream.Write(state.buffer,ReadPiont,bi1);
?????????ReadPiont+=bi1;
?????????state.packSize-=bi1;
????????}
???????}
???????if(state.Cortrol==9 && ReadPiont???????{
????????int bi1=bytesRead-ReadPiont;
????????if(bi1????????{
?????????state.packSize=state.packSize-bi1;
?????????ReadPiont+=bi1;
????????}?
????????else
????????{
?????????state.Cortrol=0;
?????????ReadPiont+=(int)state.packSize;
????????}
???????}
??????}
??????if(handler.Connected==true)
??????{
???????handler.BeginReceive(state.buffer,0,bufferSize,0,
????????new AsyncCallback(ReadCallback), state);
??????}
?????}
?????else
?????{
??????handler.Shutdown(SocketShutdown.Both);
??????handler.Close();
??????//throw(new Exception("讀入的數據小於1bit"));
?????}
????}
???}
???catch (Exception e)
???{
????OnErrorServerEvent(new ErrorServerEventArgs(e,handler));
????
???}
??}
??
??private void SendCallback(IAsyncResult ar)
??{
???Socket clIEnt = (Socket) ar.AsyncState;
???try
???{
????int bytesSent = clIEnt.EndSend(ar);
???}
???catch (Exception e)
???{
????OnErrorServerEvent(new ErrorServerEventArgs(e,clIEnt));
???}
???finally
???{
????sendDone.Set();
???}
??}
??
??private void AcceptCallback(IAsyncResult ar)
??{
???Socket handler=null;
???try
???{
????Socket listener = (Socket) ar.AsyncState;
????handler= listener.EndAccept(ar);
????StateObject state = new StateObject(bufferSize,handler);
????state.workSocket = handler;
????handler.BeginReceive(state.buffer,0,bufferSize,0,
?????new AsyncCallback(ReadCallback), state);
???}
???catch (Exception e)
???{
????OnErrorServerEvent(new ErrorServerEventArgs(e,handler));
???}
???finally
???{
????allDone.Set();
???}
??}
??
??///
??/// 析構
??///
??/// 不知道
??protected override void Dispose(bool disposing)
??{
???Abort();
??}
??
??///
??/// 引發接收事件
??///
??/// 數據
??protected virtual void OnInceptServerEvent(InceptServerEventArgs e)
??{
???if (InceptServer != null)
???{
????InceptServer(this, e);
???}
??}
??///
??/// 引發錯誤事件
??///
??/// 數據
??protected virtual void OnErrorServerEvent(ErrorServerEventArgs e)
??{
???if (ErrorServer != null)
???{
????ErrorServer(this, e);
???}
??}
??
??///
??/// 開始監聽訪問
??///
??public void Listening()
??{
???//StartListening();
???thread=new Thread(new ThreadStart(StartListening));
???thread.Name="MyTcpIpServer.Listening";
???thread.Start();
??}
??///
??/// 異常中止服務
??///
??public void Abort()
??{
???if(thread!=null)
???{
????thread.Abort();
????listener.Close();
???}
??}
??
??///
??///構造
??///
??/// 父控件
??public MyTcpIpServer(System.ComponentModel.IContainer container)
??{
???container.Add(this);
???InitializeComponent();

???//
???// TODO: 在 InitializeComponent 調用後添加任何構造函數代碼
???//
??}

??///
??/// 構造
??///
??public MyTcpIpServer()
??{
???InitializeComponent();

???//
???// TODO: 在 InitializeComponent 調用後添加任何構造函數代碼
???//
??}

??#region Component Designer generated code
??///
??/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
??/// 此方法的內容。
??///
??private void InitializeComponent()
??{

??}
??#endregion

??///
??/// 要連接的服務器IP地址
??///
??public string TcpIpServerIP
??{
???get
???{
????return tcpIpServerIP;
???}
???set
???{
????tcpIpServerIP=value;
???}
??}

??///
??/// 要連接的服務器所使用的端口
??///
??public int TcpIpServerPort
??{
???get
???{
????return tcpIpServerPort;
???}
???set
???{
????tcpIpServerPort=value;
???}
??}

??///
??/// 緩沖器大小
??///
??public int BufferSize
??{
???get
???{
????return bufferSize;
???}
???set
???{
????bufferSize=value;
???}
??}
??
??///
??/// 連接的活動狀態
??///
??public bool Activ
??{
???get
???{
????return listener.Connected;
???}
???//set
???//{
???//?activ=value;
???//}
??}

??///
??/// 發送一個流數據
??///
??public void Send(Socket ClIEntSocket,Stream Astream)
??{
???try
???{
????if(ClIEntSocket.Connected==false)
????{
?????throw(new Exception("沒有連接客戶端不可以發送信息!"));
????}
????Astream.Position=0;
????byte[] byteData=new byte[bufferSize];
????int bi1=(int)((Astream.Length+8)/bufferSize);
????int bi2=(int)Astream.Length;
????if(((Astream.Length+8)%bufferSize)>0)
????{
?????bi1=bi1+1;
????}
????bi1=bi1*bufferSize;
????
????byteData[0]=System.Convert.ToByte(bi1>>24);
????byteData[1]=System.Convert.ToByte((bi1&0x00ff0000)>>16);
????byteData[2]=System.Convert.ToByte((bi1&0x0000ff00)>>8);
????byteData[3]=System.Convert.ToByte((bi1&0x000000ff));
????
????byteData[4]=System.Convert.ToByte(bi2>>24);
????byteData[5]=System.Convert.ToByte((bi2&0x00ff0000)>>16);
????byteData[6]=System.Convert.ToByte((bi2&0x0000ff00)>>8);
????byteData[7]=System.Convert.ToByte((bi2&0x000000ff));
????
????int n = Astream.Read(byteData, 8, byteData.Length-8);
????
????while (n>0)
????{
?????ClientSocket.BeginSend(byteData, 0, byteData.Length, 0,?new AsyncCallback(SendCallback), ClIEntSocket);
?????sendDone.WaitOne();
?????byteData=new byte[bufferSize];
?????n = Astream.Read(byteData,0,byteData.Length);
????}
???}
???catch (Exception e)
???{
????OnErrorServerEvent(new ErrorServerEventArgs(e,ClIEntSocket));
???}
??}
??
??///
??/// 接收到數據事件
??///
??public event InceptServerEvent InceptServer;
??///
??/// 發生錯誤事件
??///
??public event ErrorServerEvent ErrorServer;
?}
?///
?/// 狀態對象
?///
?public class StateObject
?{
??///
??/// 構造
??///
??/// 緩存
??/// 工作的插座
??public StateObject(int bufferSize,Socket WorkSocket)
??{
???buffer = new byte[bufferSize];
???workSocket=WorkSocket;
??}
??///
??/// 緩存
??///
??public byte[] buffer = null;
??///
??/// 工作插座
??///
??public Socket workSocket = null;?????????????
??///
??/// 數據流
??///
??public Stream Datastream=new MemoryStream();
??///
??/// 剩余大小
??///
??public long residualSize=0;
??///
??/// 數據包大小
??///
??public long packSize=0;
??///
??/// 計數器
??///
??public int Cortrol=0;
?}
?
?///
?/// 接收事件
?///
?public class InceptServerEventArgs : EventArgs
?{?
??private readonly Stream datastream;
??private readonly Socket serverSocket;
??private readonly MyTcpIpServer tcpIpServer;
??///
??/// 構造
??///
??/// 數據
??/// 工作插座
??/// 提供服務的TCP/IP對象
??public InceptServerEventArgs(Stream Astream,Socket ServerSocket,MyTcpIpServer TcpIpServer)
??{
???datastream=Astream;
???serverSocket=ServerSocket;
???tcpIpServer=TcpIpServer;
??}
??? ///
??? /// 數據
??? ///
??public Stream Astream
??{????
???get { return datastream;}?????
??}
??///
??/// 工作插座
??///
??public Socket ServerSocket
??{????
???get { return serverSocket;}?????
??}
??///
??/// 提供TCP/IP服務的服務器對象.
??///
??public MyTcpIpServer TcpIpServer
??{????
???get { return tcpIpServer;}?????
??}
?}
?///
?/// 接收數據委托
?///
? public delegate void InceptServerEvent(object sender, InceptServerEventArgs e);
?///
?/// 錯誤事件委托
?///
?public class ErrorServerEventArgs : EventArgs
?{?
??private readonly Exception error;
??private readonly Socket serverSocket;
??///
??/// 構造
??///
??/// 數據
??/// 問題插座
??public ErrorServerEventArgs(Exception Error,Socket ServerSocket)
??{
???error=Error;
???serverSocket=ServerSocket;
??}
??? ///
??? /// 數據
??? ///
??public Exception Error
??{????
???get { return error;}?????
??}
??///
??/// 問題插座
??///
??public Socket ServerSocket
??{????
???get { return serverSocket;}?????
??}
?}
?///
?///錯誤事件委托
?///
? public delegate void ErrorServerEvent(object sender, ErrorServerEventArgs e);
}

完成端口方式是NT目前最好的一種通信方式,它在大吞吐,大數量連接下,比其它方式有無法比擬的效率以及性能,因C#在的Socket在內部使用的完成端口方式,在程序實現上異常簡單,請大家體會.


?

?

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved