c中异步基于消息通信的完成端口的TCPIP协议的组件实现(源代码).docVIP

  • 5
  • 0
  • 约2.18万字
  • 约 25页
  • 2017-02-09 发布于重庆
  • 举报

c中异步基于消息通信的完成端口的TCPIP协议的组件实现(源代码).doc

c中异步基于消息通信的完成端口的TCPIP协议的组件实现(源代码)

源代码: 客户端: 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=; ??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=(bi124)0xff000000; ????????state.packSize=bi1; ????????ReadPiont++; ????????state.Cortrol=1; ???????} ?????? ???????if(state.Cortrol==1 ReadPiont???????{ ????????long bi1=state.buffer[ReadPiont]; ????????bi1=(bi116)0x00ff0000; ????????state.packSize=state.packSize+bi1; ????????ReadPiont++; ???????

文档评论(0)

1亿VIP精品文档

相关文档