C # component implementation of the TCP/IP Protocol Based on Message Communication completion port (source code)

Source: Internet
Author: User

Source code:
Client:
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 provides message-based clients 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;
?? 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 effececallback (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 ("the read data is smaller than 1bit "));
?????}
????? If (handler. Connected = true)
????? {
?????? Handler. BeginReceive (state. buffer, 0, bufferSize, 0,
??????? New AsyncCallback (effececallback), 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, 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 (effececallback), 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 of data
?? ///
?? /// Data stream
?? Public void Send (Stream Astream)
?? {
??? Try
??? {
???? If (ClientSocket. Connected = false)
???? {
????? Throw (new Exception ("messages cannot be sent if the server is not connected! "));
????}
???? 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.

Related Articles]

  • Analysis and Summary of TCP/IP attack principles
  • Detailed description of TCP/IP protocol datagram Structure
  • Interpreting TCP/IP protocol by connecting to an instance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.