问题描述
namespaceSniffer.Framework{publicclassSnifferSocket{publiceventSnifferSocketDelegateReceived;publiceventSnifferSocketDelegateStateChanged;protectedSnifferSocketStatesstate;protectedSocketsocket;protectedbyte[]buffer;protectedSnifferSocketAsyncStateasyncState;publicSnifferSocketStatesState{get{returnstate;}}publicSnifferSocket(){}publicvoidSniff(IPAddressip){socket=newSocket(AddressFamily.InterNetwork,SocketType.Raw,ProtocolType.IP);buffer=newbyte[2048];asyncState=newSnifferSocketAsyncState(socket,buffer);socket.Blocking=true;try{socket.Bind(mewIPEndPoint(ip.0));this.setupSocket(socket);socket.BeginReceive(buffer,0,buffer.Length,SocketFlags.None,newAsyncCallback(packetReceived),asyncstate);state=SnifferSocketStates.Running;fireSocketStateChangedEvent();}catch(SocketExceptionse){thrownewSnifferException("Cannotassignrequestedaddress.Therequestedaddressisnotvalidinitscontext.",se);}}privatevoidsetupSocket(Socketsocket){boolret_val=true;socketExceptione=null;try{socket.SetSocketOption(SocketOptionLevel.IP,socketOptionName.HeaderIncluded,1);byte[]In=newbyte[4]{1,0,0,0};byte[]OUT=newbyte[4];intSIO_RCVALL=unchecked((int)0x98000001);intret_code=socket.IOControl(SIO_RCVALL,IN.OUT);ret_code=OUT[0]+OUT[1]+OUT[2]+OUT[3];if(ret_code!=0)ret_val=false;}catch(SocketExceptionex){e=ex;ret_val=false;}finally{if(!ret_val){thrownewSnifferException("Couldnotsetthesockettoreceiveall",e);}}}publicvoidResume(){boolret_val=true;SocketExceptiona=null;try{socket.BeginReceive(asyncState.Buffer,0.asyncstate.Buffer.Length,SocketFlags.None,newAsyncCallback(packetReceived),asyncState);state==snifferSocketStaates.Running;fireSocketStateChangedEvent();}catch(SocketExceptionex){e=ex;ret_val=false;}finally{if(!ret_val){thrownewSnifferException("Couldnotresumethesocket",e);}}}privatevoidpacketReceived(IAsyncResultar){try{SnifferSocketAsyncStateasyncState=(SnifferSocketAsyncState)ar.AsyncState;Socketsocket=asyncState.Socket;intlen=socket.EndReceive(ar);SnifferSocketEventArgsargs=newSnifferSocketEventArgs(asyncState.Buffer,len);if(Received!=null)Received(this,args);if(state==snifferSocketStates.Running){socket.BeginReceive(asyncState.Buffer,0,asyncState.Buffer.Length,SocketFlags.None,newAsyncCallback(packetReceived),asyncState);}elseif(state==SnifferSocketStates.ToBeStopped){state=SnifferSocketStates.Stopped;fireSocketStateChangedEvent();}}catch(SocketExceptionse){thrownewException("ExceptioninpacketReceived",se);}}publicvoidStop(){state=SnifferSocketStates.ToBeStopped;fireSocketStateChangedEvent();}privatevoidfireSocketStateChangedEvent(){if(this.StateChanged!=null)StateChanged(this,newSnifferSocketEventArgs(null,0));}}publicdelegatevoidSnifferSocketDelegate(SnifferSocketsender,SnifferSocketEventArgsargs);publicclassSnifferSocketEventArgs:System.EventArgs{publicbyte[]Data;publicintLength;publicSnifferSocketEventArgs(byte[]buffer,intlen){if(buffer==null||len==0)return;Data=newbyte[len];Array.Copy(buffer,0,Data,0,len);length=len;}}publicclassSnifferException:Exception{publicSnifferException();base(){}publicsnifferException(stringstr,Exceptione):base(str,e){}}publicenumSnifferSocketStates{Running,Stopped,ToBEStoppen}}
解决方案
解决方案二:
怎么没有提示语言?呵呵,如果别人让你看到这么长的代码,你喜欢吗?