欢迎来到天天文库
浏览记录
ID:45754912
大小:64.70 KB
页数:29页
时间:2019-11-17
《c#中异步基于消息通信的完成端口的TCPIP协议的组件实》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、源代码:客户端:usingSystem;usingSystem.IO;usingSystem.ComponentModel;usingSystem.Collections;usingSystem.Diagnostics;usingSystem.Net;usingSystem.Net.Sockets;usingSystem.Threading;namespaceMyKJ{?///?///MyTcpIpClient提供在NetTCP_IP协议上基于消息的客户端?///?publicclassMyTcpIp
2、Client:System.ComponentModel.Component?{??privateintbufferSize=2048;??privatcstringtcpIpScrvcrIP="127.0.0.1??privateinttcplpServerPort=11000;??privateSocketClientSocket=null;??privateManualResetEventconnectDone=newManualResetEvent(false);??privateManual
3、ResetEventsendDone=newManualResetEvent(false);99••??privatevoidConnectCallback(IAsyncResultar)??{???try???{????Socketclient=(Socket)ar.AsyncState;????client.EndConnect(ar);9999••••???calch(Exceptione)???{????OnErrorEvent(newErrorEventArgs(e));???}???fin
4、ally???{????connectDone.Set();???}??)??privatevoidSendCallback(IAsyncResultar)??{???try???{????Socketclient=(Socket)ar.AsyncState;????intbytesSent=client.EndSend(ar);????//Console.WriteLine(bytesSent);???}???catch(Exceptione)????OnErrorEvent(newErrorEve
5、ntArgs(e));???finally???{????sendDone.Set();???}??}??privatevoidReceiveCallback(lAsyncResultar)??{???Sockethandler=null;???try???{????lock(ar)9999(?????StateObjectstate=(StateObject)ar.AsyncState;?????handler=state.workSocket;??????????intbytesRead=hand
6、ler.EndReceive(ar);99999?????讦(bytesRead>0)??????intReadPiont=0;???????while(ReadPiont??????{????????if(state.Cortrol==()&&ReadPiont???????{????????longbi1=state.buffer[ReadPiont];????????bi1=(bil«24)&0xff000000;????????statc.packSizc=bi1;????????ReadPi
7、ont++;????????state.Cortrol=l;???????}?????????????if(state.Cortrol==l&&ReadPiont???????{????????longbi1=state.buffer[ReadPiont];????????bi1=(bil«16)&OxOOffOOOO;????????state.packSize=state.packSize+bil;????????ReadPiont++;????????state.Cortrol=2;??????
8、?}999999???????if(state.Cortrol==2&&ReadPiont???????{????????longbil=state.buffer[ReadPiont];????????bil=(bil«8)&OxOOOOffOO;????????state.packSize=state.packSize+bi1:????????ReadPiont++;????????state.Cortrol=3;???????}9999999????
此文档下载收益归作者所有