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.To

相关文章】

  • TCP/IP攻击原理分析总结
  • TCP/IP协议数据报结构详解
  • 通过连接实例解读TCP/IP协议

相关内容