C # Socket Impersonation HTTP Server helper class

Source: Internet
Author: User

0x01 written in front

0x02 HTTP protocol

0x03 TCP/IP

0x04 See Code

0X05 Summary

0x01 written in front

Because of the work, it is often necessary to communicate between servers, or between processes, to assign tasks, and so on. With the socket operation, too much trouble, need to establish a connection, processing messages. So

You are ready to create a socket-Emulation HTTP Server helper class that can be called directly from WebClient.

0x02 HTTP protocol

My understanding: The HTTP protocol, is actually relies on the TCP/IP protocol, also can send the message through the socket, just said. The content format that is sent satisfies the HTTP conditions and can be interpreted as an HTTP protocol.

Common HTTP Headers . HTTP versions can also be written in 1.1.

http/1.0  okcontent-type:text/htmlconnection:keep-alivecontent- encoding:utf-8
0x03 TCP/IP

My understanding: The TCP/IP protocol is actually a classification of a class of protocols, and many protocols are based on this protocol to deliver the message. For more detailed information, please search for yourself.

0x04 See Code

SocketHttpHelper.cs Socket Impersonation HTTP helper class

1  Public classSockethttphelper2 {3     Private stringIP ="127.0.0.1";4     Private intPort =8123;5     Private intCount =0;6     PrivateSocket Server =NULL;7 8      Public stringDefaultreturn =string. Empty;9 Ten      Public Eventfunc<string,string,string> Handler =NULL; One  A      PublicSockethttphelper () -     { -     } the  -      PublicSockethttphelper (stringIpintPort) -     { -          This. IP =IP; +          This. Port =Port; -     } +  A      Public voidStartlisten (intCount =Ten) at     { -          This. Count =count; -Thread T =NewThread (NewThreadStart (ProcessThread)); -T.isbackground =true; - T.start (); -     } in  -      Public voidclosesocket () to     { +         Try -         { the server. Close (); *         } $         Catch { }Panax Notoginseng     } -  the     Private voidProcessThread () +     { AServer =Newsockets (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp); theServer. Bind (NewSystem.Net.IPEndPoint (System.Net.IPAddress.Parse (IP), port)); + server. Listen (count); -          while(true) $         { $             Try -             { -Socket client =server. Accept (); theThreadPool.QueueUserWorkItem (NewWaitCallback (Listenexecute), client); -             }Wuyi             Catch { } the             finally -             { Wu             } -         } About     } $  -     Private voidListenexecute (Objectobj) -     { -Socket client = obj asSocket; A         Try +         { the             stringIP = (client. Remoteendpoint asSystem.Net.IPEndPoint). Address.tostring (); -             byte[] buffer =New byte[1024x768]; $             intCount =client. Receive (buffer); the             if(Count >0) the             { the                 stringContent = Encoding.UTF8.GetString (buffer,0, count); the  -                 //Parse Content inRegex Actionregex =NewRegex (@"get\s+/(? <action>\w+) \? (? <args>[^\s]{0,})"); the                 stringAction = actionregex.match (content). groups["Action"]. Value; the                 stringargs = Actionregex.match (content). groups["args"]. Value; About                 stringHeadstr =@"http/1.0 OK the content-type:text/html the connection:keep-alive the Content-encoding:utf-8 +  - "; the                 if(Handler! =NULL)Bayi                 { the                     Try the                     { -                         stringresult =Handler (action, args); -                         stringdata =string. Format (Headstr +result); the client. Send (Encoding.UTF8.GetBytes (data)); the                     } the                     Catch { } the                     finally -                     { the                     } the                 } the                 Else94                 { the                     stringdata =string. Format (Headstr +Defaultreturn); the client. Send (Encoding.UTF8.GetBytes (data)); the                 }98             } About         } -         Catch { }101         finally102         {103             Try104             { the client. Shutdown (Socketshutdown.both);106 client. Close ();107 client. Dispose ();108             }109             Catch { } the         }111     } the}
View Code0X05 Summary

Because, each time to start from the socket to write, write more only to find that the original need to write a common class, the point is not time-consuming.

C # Socket Impersonation HTTP Server helper class

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.