RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:9:30-18:00
你可能遇到了下面的问题
关闭右侧工具栏
c#中异步基于消息通信的完成端口的TCP/IP协议的组
  • 作者:zhaozj
  • 发表时间:2020-12-23 10:40
  • 来源:未知

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

源代码:

客户端:

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();