日期:2009-10-30  浏览次数:20407 次

源代码:

客户端:

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;
????????