Component implementation of the TCPIP protocol (source code) of the TCPIP protocol based on message communication based on messa

xiaoxiao2021-03-06  105

Component implementation of the TCP / IP protocol (source code) of the TCP / IP protocol based on message communication based on message communication

Source code:

Client:

Using system; using system.componentmodel; using system.diagnostics; using system.net; using system.net.sockets; using system.threading;

Namespace mykj {? ///

? /// myTCPIPCLIENT provides a message-based client on the NET TCP_IP protocol? ///

? public class mytcpipclient: system.componentmodel.component

? {

?? private int buffersize = 2048;

?? private string tcpipserverip = "127.0.0.1";

?? private int tcpipserverport = 11000;

??prate socket clientsocket = null;

?? Private manualReveTevent ConnectDone = New ManualReveTevent (false);

?? Private manualReveTevent Senddone = New ManualReveTevent (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

??? {

???? semenddone.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);

?????

????? ife (bytesread> 0)

????? {

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

???????}

???????

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

???????}

??????? ing (state.cortrol == 7 && Readpiont

??????? {

???????? long bi1 = state.buffer [readpiont];

???????? bi1 = bi1 & 0xFF;

???????? state.residualsize = state.residualsize bi1;

???????? state.datastream.setlength (0);

???????? state.datathastream.position = 0;

????????

???????? readpiont ;

???????? state.cortrol = 8;

???????? state.packsize- = 1;

???????}

???????

??????? if (state.cortrol == 8 && readpiont

??????? {

???????? INT BI1 = bytesread-readpion

???????? INT BI2 = (int) (state.residualsize-state.datathastream.length);

???????? IF (bi1> = bi2)

???????? {

????????? state.DataStream.write (state.buffer, readpiont, bi2);

????????? readpiont = bi2;

????????? OnCePtevent (new inspteventArgs (state.dataththtream, 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-readpion

???????? IF (bi1

???????? {

????????? state.packsize = state.packsize-bi1;

????????? readpiont = bi1;

????????}?

???????? ELSE

???????? {

????????? state.cortrol = 0;

????????? readpiont = (int) state.packsize;

????????}

???????}

??????}

?????}

????? Else ????? {

?????? throw (New Exception ("Reading Data is less than 1bit");

?????}

????? ife (handler.connected == true)

????? {

?????? Handler.BeginReceive (state.buffer, 0, buffersize, 0,

?????? New asyncCallback (receiveCallback), State);

?????}

????}

???}

??? catch (Exception E)

??? {

???? OneRROREVENT (New ErroreventArgs (e));

????

???}

??}

??

?? ///

??/// connect to the server??///

?? public void conn ()

?? {

??? Try

??? {

???? clientsocket = new socket (addressfamily.internetwork, sockettype.stream, protocoltype.tcp);?

???? ipaddress ipaddress = ipaddress.parse (TCPIPServerip);

??????? IpendPoint Remoteep = New IpendPoint (ipaddress, tcpipserport);

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

???}

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

??}

?? ///

??/// Disconnect??///

?? public void close ()

?? {

??? Try

??? {

???? clientsocket.shutdown (socketshutdown.both);

???? clientsocket.close ();

???}

??? catch (Exception E)

??? {

???? OneRROREVENT (New ErroreventArgs (e));

???}

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

??}

?? ///

?? /// Send a stream data ?? ///

?? ///

data flow

?? public void send (stream astream)

?? {

??? Try

??? {

???? if (clientsocket.connected == false)

???? {

????? throw (New Exception ("No connection server can not send information!"));

????}

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

???? semenddone.waitone ();

????? Bytedata = new byte [buffersize];

????? n = astream.read (Bytedata, 0, Bytedata.Length);

????}

???}

??? catch (Exception E)

??? {

???? OneRROREVENT (New ErroreventArgs (e));

???}

??}

??

?? ///

?? /// Construction ?? ///

?? ///

Parent control

?? public mytcpipclient (System.componentmodel.icontainer Container)

?? {

??? container.add (this);

? ??? initializationComponent ();

??? // ??? // Todo: Add any constructor code after the initializationComponent call ??? // ??} ?? ///

?? /// Construction ?? ///

?? public mytcpipclient ()

?? {

? ??? initializationComponent ();

??? // ??? // Todo: Add any constructor code after INITIALIZECOMPONENT calls ??? // ??}

? # Region Component Designer generated code ?? ///

?? /// Designer supports the required method - do not use the code editor to modify ?? // This method. ?? ///

?? private void initializecomponent ()

?? {

??} ?? # endregion

?? ///

?? // The server IP address to be connected ?? ///

?? Public String TcpipserverIP

?? {

??? get

??? {

???? Return TCPIPSERVERIP;

???}

??? set

??? {

???? tcpiPServerip = value;

???}

??}

?? ///? //// The port used by the server is used to connect? ///

?? public int TCPIPSERVERPORT

?? {

??? get

??? {

???? Return TCPIPSERVERPORT

???}

??? set

??? {

???? tcpiPserverport = value;

???}

??}

?? ///

?? // / buffer size ?? ///

?? public int buffersize

?? {

??? get

??? {

???? Return buffersize;

???}

??? set

??? {

???? buffersize = value;

???}

??}

??

?? ///

?? /// Connected activity status ?? ///

?? Public Bool Activ

?? {

??? get

??? {

???? if (clientsocket == null)

???? {

????? Return False;

????}

???? Return clientSocket.connected;

???}

??}

?? ///

?? // / The event that receives data is incident ?? ///

?? public evenet INCEPTEVENT INCEPT

?? ///

?? /// triggered receiving data events ?? ///

?? ///

Receive data

?? Protected Virtual Void OnCePtevent (InceptEventArgs E)

?? {

??? if (INCEPT! = NULL)

??? {

???? INCEPT (THIS, E);

???}

??}

?? ///

?? /// Event incident occurred ?? ///

?? public evenet erroorevent error;

?? ///

?? /// trigger an error event ?? ///

?? ///

Error data

?? Protected Virtual Void OneRROREVENT (ERROREVENTARGS E)

?? {

??? if (error! = null)

??? {

???? Error (this, e);

???}

??}

??

?

?

? ///

? / / / Receive data events? ///

? public class insinceventargs: Eventargs

? {?

?? Private readonly stream datastream;

?? private readonly socket clientsocket;

?? ///

?? /// Construction ?? ///

?? ///

Received data

?? ///

Received socket

?? Public INCEPTEVENTARGS (Stream Astream, Socket Clientsocket)

?? {

??? DataStream = astream;

??? clientsocket = clientsocket;

??}

?? ///

? /// accept the data stream ?? ///

?? public stream astream

?? {????

??? get {return datastream;} ?????

??}

?? ///

?? /// The receiving socket is ?? ///

?? Public Socket ClientSocket

?? {????

??? get {return clientsocket;} ?????

??}

?}?

? ///

? // Define Receive delegation? ///

PUBLIC DELEGATE VOID INCEPTEVENT (Object Sender, INCEPTEVENTARGS E);

? ///

? // / wrong event? ///

PUBLIC CLASS ERROREVENTARGS: Eventargs

? {?

?? private readonly exception error;

?? ///

?? /// Construction ?? /// ?? ///

Error information object

?? Public ErroreventArgs (Exception Error)

?? {

??? Error = Error;

??}

?? ///

?? /// Error information object ?? ///

?? Public Exception Error

?? {????

??? Get {return error;} ?????

??}

?

? ///

? /// Error commission? ///

PUBLIC DELEGATE VOID ERROREVENT (Object Sender, ErroEventArgs E);

?

?

?

}

Service-Terminal:

Using system; using system.componentmodel; using system.diagnostics; using system.net; using system.net.sockets; using system.threading;

Namespace mykj {? ///

? /// MyTCPipClient provides a message-based server based on the NET TCP_IP protocol? ///

PUBLIC CLASSMPIPSERVERVER: System.comPonentModel.component

? {

?? private int buffersize = 2048;

??prate string tcpipserverip = "";

?? private int tcpipserverport = 11000;

??prate socket listener = null;

?? Private manualReveTevent alldone = new manualRest (false);

?? Private manualReveTevent Senddone = New ManualReveTevent (false);

?? private thread thread = NULL;

??

?? private void startlistence ()

?? {

??? Try

??? {

???? listener = new socket (AddressFamily.InterneTWork,

????? sockettype.stream, protocoltype.tcp);

?????

???? ipaddress ipaddress;

???? if (tcpipserverip.trim () == "")

???? {

????? ipaddress = ipaddress.ny;?

????}

???? Else

???? {

????? ipaddress = ipaddress.parse (TCPIPServerip);

????}

???? ipendpoint local (iPaddress, tcpiPServerport);

????

???? tistener.bind (localndpoint);

????? tiistener.listen (10);

???? while (true)

???? {

????? alldone.reset ();

????? listener.beginaccept (new asyncCallback, listener);

????? alldone.waitone ();

????}

???}

??? catch (Exception E)

??? {

???? OneerrorServereEvent (New ErrorServereventArgs (E, Listener);?

???}

??}

??

?? Private Void ReadCallback (IASYNCRESULT AR) ?? {

??? Socket handler = null;

??? Try

??? {

???? LOCK (AR)

???? {

????? stateObject state = (stateObject) ar.asyncstate;

????? Handler = state.worksocket;

?????

????? INT BYTESREAD = Handler.Endreceive (AR);

?????

????? ife (bytesread> 0)

????? {

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

???????}

???????

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

???????}

??????? ing (state.cortrol == 7 && Readpiont

??????? {

???????? long bi1 = state.buffer [readpiont];

???????? bi1 = bi1 & 0xFF;

???????? state.residualsize = state.residualsize bi1;

???????? state.datastream.setlength (0);

???????? state.datathastream.position = 0;

????????

???????? readpiont ;

???????? state.cortrol = 8;

???????? state.packsize- = 1;

???????}

???????

??????? if (state.cortrol == 8 && readpiont

??????? {

???????? INT BI1 = bytesread-readpion

???????? INT BI2 = (int) (state.residualsize-state.datathastream.length);

???????? IF (bi1> = bi2)

???????? {

????????? state.DataStream.write (state.buffer, readpiont, bi2);

????????? readpiont = bi2;

???????? onwarePTServere (New Ince.DataStream, State.Worksocket, THIS);

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

???????? IF (bi1

???????? {

????????? state.packsize = state.packsize-bi1;

????????? readpiont = bi1;

????????}?

???????? ELSE

???????? {

????????? state.cortrol = 0;

????????? readpiont = (int) state.packsize;

????????}

???????}

??????}

?????? if (Handler.Connected == True)

?????? {

??????? Handler.BeginReceive (state.buffer, 0, buffersize, 0,

???????? New asyncCallback (ReadCallback), State);

??????}

?????}

????? ELSE

????? {

?????? Handler.Shutdown (socketshutdown.both);

?????? Handler.close ();

?????? // throw (New Exception ("Reading Data is less than 1bit");

?????}

????}

???}

??? catch (Exception E)

??? {

???? OneRrorServere (New ErrorServereventArgs (e, handler);

????

???}

??}

??

?? Private Void SendCallback (IasyncResult Ar)

?? {

??? Socket client = (socket) ar.asyncstate;

??? Try

??? {

???? Int bytessent = client.endsend (ar);

???}

??? catch (Exception E)

??? {

???? OneRrorServereEvent (New ErrorServereventArgs (E, Client));

???}

??? finally

??? {

???? semenddone.set ();

???}

??}

??

?? Private Void AcceptCallback (IASYNCRESULT AR)

?? {

??? Socket handler = null;

??? Try

??? {

???? Socket Listener = (socket) ar.asyncState;

???? Handler = listener.ndeNDaccept (ar);

???? stateObject state = new stateObject (buffersize, handler);

???? state.worksocket = handler;

???? Handler.BeginReceive (state.buffer, 0, buffersize, 0,

????? New asyncCallback (ReadCallback), State;

???}

??? catch (Exception E)

??? {

???? OneRrorServere (New ErrorServereventArgs (e, handler);

???}

??? finally

??? {

???? alldone.set (); ???}

??}

??

?? ///

??/// destruct??///

?? ///

do not know

?? protected override void dispose (Bool Disposing)

?? {

??? Abort ();

??}

??

?? ///

?? /// triggered receiving events ?? ///

?? ///

data

?? Protected Virtual Void OnwarePTServerevent (InceptServereventargs E)

?? {

??? if (INCEPTSERVER! = NULL)

??? {

???? INCEPTSERVER (this, e);

???}

??}

?? ///

?? /// trigger an error event ?? ///

?? ///

data

?? Protected Virtual Void OneRRORSERVEREVENT (ERRORSERVEREVENTARGS E)

?? {

??? if (ErrorServer! = null)

??? {

???? ERRORSERVER (this, e);

???}

??}

??

?? ///

?? /// Start listening to access ?? ///

?? public void listening ()

?? {

??? // startlistence ();

??? thread = new thread (new threadstart (startlistening));

??? thread.name = "mytcpipserver.listening";

??? thread.start ();

??}

?? ///

?? // Abnormal abnormal service ?? ///

?? public void abort ()

?? {

??? i (thread! = null)

??? {

???? thread.abort ();

???? listener.close ();

???}

??}

??

?? ///

?? /// Construction ?? ///

?? ///

Parent control

?? public mytcpipserver (System.componentmodel.icontainer Container)

?? {

??? container.add (this);

? ??? initializationComponent ();

??? // ??? // Todo: Add any constructor code after INITIALIZECOMPONENT calls ??? // ??}

?? ///

?? /// Construction ?? ///

?? public mytcpipserver ()

?? {

? ??? initializationComponent ();

??? // ??? // Todo: Add any constructor code after INITIALIZECOMPONENT calls ??? // ??}

? # Region Component Designer generated code ?? ///

?? /// Designer supports the required method - do not use the code editor to modify ?? // This method. ?? ///

?? private void initializecomponent ()

?? {

??} ?? # endregion

?? ///

?? // The server IP address to be connected ?? ///

?? Public String TcpipserverIP

?? {

??? get

??? {

???? Return TCPIPSERVERIP;

???}

??? set

??? {

???? tcpiPServerip = value;

???}

??}

?? ///

?? // The port used to connect to the port used to connect ?? ///

?? public int TCPIPSERVERPORT

?? {

??? get

??? {

???? Return TCPIPSERVERPORT

???}

??? set

??? {

???? tcpiPserverport = value;

???}

??}

?? ///

?? // / buffer size ?? ///

?? public int buffersize

?? {

??? get

??? {

???? Return buffersize;

???}

??? set

??? {

???? buffersize = value;

???}

??}

??

?? ///

?? /// Connected activity status ?? ///

?? Public Bool Activ

?? {

??? get

??? {

???? Return Listener.Connected;

???}

??? // set

??? {{

??? //? Activ = value;

??? //}

??}

?? ///

?? /// Send a stream data ?? ///

?? Public Void Send (Socket Clientsocket, 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;

???? semenddone.waitone ();

????? Bytedata = new byte [buffersize];

????? n = astream.read (Bytedata, 0, Bytedata.Length);

????}

???}

??? catch (Exception E) ??? {

???? OneerrorServereEvent (New ErrorServereventArgs (E, ClientSocket);

???}

??}

??

?? ///

?? /// Receive data events ?? ///

?? Public Event IncePTServeRevent INCEPTSERVER;

?? ///

?? /// Error event ?? ///

?? Public EVENT ERRORSERVEREVENT ERRORSERVER;

?

? ///

? /// state object? ///

PUBLIC CLASS STATEOBJECT

? {

?? ///

?? /// Construction ?? ///

?? ///

Cache

?? ///

Work socket

?? Public StateObject (int buffersize, socket worksocket)

?? {

??? buffer = new byte [buffersize];

??? Worksocket = Worksocket;

??}

?? ///

?? /// cache ?? ///

?? public byte [] buffer = null;

?? ///

?? /// work socket ?? ///

?? public socket worksocket = null; ?????????????

?? ///

??/// data flow??///

?? public stream datastream = new memorystream ();

?? ///

?? /// The remaining size ?? ///

?? public long residualsize = 0;

?? ///

?? /// packet size ?? ///

?? public long packsize = 0;

?? ///

?? // / counter ?? ///

?? public int curtr = 0;

?

?

? ///

? / / / Receive event? ///

Public Class IncePtServereventArgs: Eventargs

? {?

?? Private readonly stream datastream;

?? Private readonly socket serversocket;

?? private readonly mytcpipserver tcpiPserver;

?? ///

?? /// Construction ?? ///

?? ///

data

?? ///

Work socket

?? ///

Provide service TCP / IP objects

?? Public IncePTServereventArgs (Stream Astream, Socket ServerSocket, MyTcpipserver Tcpipserver)

?? {

??? DataStream = astream;

??? serversocket = serversocket;

??? tcpipserver = tcpiPServer;

??}

??? ///

??? // / data ??? ///

?? public stream astream

?? {????

??? get {return datastream;} ?????

??}

?? ///

?? /// work socket ?? ///

?? Public Socket Serversocket

?? {????

??? get {return serversocket;} ?????

??}

?? ///

?? // Provide server objects for TCP / IP services. ?? ///

?? public mytcpipserver tcpiPserver

?? {????

??? Get {return tcpipserver;} ?????

??}

?

? ///

? / / / Receive data commission? ///

PUBLIC DELEGATE VOID INCEPTSERVEREVENT (Object Sender, IncePTServereventArgs E);? ///

? /// Error event delegate? ///

PUBLIC CLASS ERRORSERVERVENTARGS: Eventargs

? {?

?? private readonly exception error;

?? Private readonly socket serversocket;

?? ///

?? /// Construction ?? ///

?? ///

data

?? ///

Problem socket

?? Public ErrorServereventArgs (Exception Error, Socket Serversocket)

?? {

??? Error = Error;

??? serversocket = serversocket;

??}

??? ///

??? // / data ??? ///

?? Public Exception Error

?? {????

??? Get {return error;} ?????

??}

?? ///

?? /// problem socket ?? ///

?? Public Socket Serversocket

?? {????

??? get {return serversocket;} ?????

??}

?

? ///

? /// Error event delegate? ///

PUBLIC DELEGATE VOID ERRORSERVEREVENT (Object Sender, ErrorServelectrorgs E);

}

Completing port mode is the best way to communicate in NT. It is in large throughput, large quantity connection, which is more efficient and performance than other means, because C # is in the internal use of Socket in the program, in the program It is very simple to achieve, please experience.

?

?

转载请注明原文地址:https://www.9cbs.com/read-126463.html

New Post(0)