Recording (1)

Source: Internet
Author: User
 system.  URI  MS =  New   URI  (" http://www.microsoft.com/en/us/default.aspx ");  console . writeline ( "Scheme: {0}" , Ms. scheme);  console . writeline ( "Host: {0}" , Ms. host);  console . writeline ( "port: {0}" , Ms. port);  console . writeline ( "absolutepath: {0}" , Ms. absolutepath);  console . writeline ( "query: {0}" , Ms. query);  console . readkey (); 
Public void Run (){ // Obtain the local loopback network address, that is, 127.0.0.1.  IPaddress Address = IPaddress . Loopback; // Create an accessible endpoint. 49152 indicates the port number.  Ipendpoint Endpoint = New  Ipendpoint (Addresses, 49152 ); // Create a socket using an IPv4 address and the transmission control protocol TCP. bidirectional, reliable, and connection-based byte stream  Socket Socket = New  Socket ( Addressfamily . InterNetwork, Sockettype . Stream, Protocoltype . TCP ); // Bind the socket to an endpoint Socket. BIND (endpoint ); // Set the length of the connection queue Socket. Listen (10 ); Console . Writeline ( "Start listening, Port: {0 }." , Endpoint. Port ); While ( True ){// Start listening. This method will block thread execution until it receives a connection request from a client.  Socket Client = socket. Accept (); // Output the client address  Console . Writeline (client. remoteendpoint ); // Prepare to read the data requested by the client. The read data is saved in an array.  Byte [] Buffer = New byte [4096]; // Accept data  Int Length = client. Receive (buffer, 4096, Socketflags . None );// Translate the requested data into a UTF-8 System. Text. Encoding Utf8 = system. Text. Encoding . Utf8; String Requeststring = utf8.getstring (buffer, 0, length ); // Display the request content  Console . Writeline (requeststring ); // Status line  String Statusline = "HTTP/1.1 200 OK \ r \ n" ; Byte [] Statuslinebytes = utf8.getbytes (statusline );// Prepare the webpage sent to the client  String Responsebody = @ "<HTML>  ; Byte [] Responsebodybytes = utf8.getbytes (responsebody ); // Response Header  String Responseheader = String . Format ("Content-Type: text/html; charset = UTF-8 \ r \ ncontent-length: {0} \ r \ n" , Responsebody. Length ); Byte [] Responseheaderbytes = utf8.getbytes (responseheader ); // Send status information to the client Client. Send (statuslinebytes ); // Send a response header to the client Client. Send (responseheaderbytes ); // Separate rows of the header and content Client. Send ( New byte [] {13, 10 }); // Send content to the client Client. Send (responsebodybytes );// Disconnect from the client Client. Close (); If ( Console . Keyavailable) Break ;} // Close the server Socket. Close ();}
 Public void Run (){ // Obtain the local loopback network address, that is, 127.0.0.1. IPaddress address = IPaddress. loopback; // Create an accessible endpoint. 49152 indicates the port number. Ipendpoint endpoint = New Ipendpoint (addresses, 49152 );// Create a TCP listener Tcplistener newserver = New Tcplistener (endpoint ); // Start the listener Newserver. Start (); Console . Writeline ( "Start listening ..." ); While ( True ){ // Wait for client connection Tcpclient newclient = newserver. accepttcpclient (); Console . Writeline ( "A connection has been established! " );// Get a network stream Networkstream NS = newclient. getstream (); // Use the UTF-8 encoding During Processing System. Text. Encoding Utf8 = system. Text. Encoding . Utf8; Byte [] Request = New byte [4096]; Int Length = NS. Read (request, 0, 4096 ); String Requeststring = utf8.getstring (request, 0, length ); Console . Writeline (requeststring );// Status line  String Statusline = "HTTP/1.1 200 OK \ r \ n" ; Byte [] Statuslinebytes = utf8.getbytes (statusline ); // Prepare the webpage sent to the client  String Responsebody = @ "<HTML>  ; Byte [] Responsebodybytes = utf8.getbytes (responsebody ); // Response Header  String Responseheader = String . Format ( "Content-Type: text/html; charset = UTF-8 \ r \ ncontent-length: {0} \ r \ n" , Responsebody. Length ); Byte [] Responseheaderbytes = utf8.getbytes (responseheader ); // Output the response status line NS. Write (statuslinebytes, 0, statuslinebytes. Length ); // Output the Response Header NS. Write (responseheaderbytes, 0, responseheaderbytes. Length ); // Output a blank line between the response header and content NS. Write ( New byte [] {13, 10}, 0, 2 ); // Output content NS. Write (responsebodybytes, 0, responsebodybytes. Length ); // Close the connection to the client Newclient. Close (); If ( Console . Keyavailable) Break ;} // Close the server Newserver. Stop ();}
Public void Run (){ // Check whether the system supports  If (! Httplistener. issupported ){ Throw new System. Invalidoperationexception ( "The httplistener must be Windows XP SP2 or server 2003 or later! " );} // Note that the prefix must end with a forward slash (/).  String [] Prefixes = New String [] { "Http: // localhost: 49152 /" };// Create a listener. Httplistener listener = New Httplistener (); // Add the prefix of the listener.  Foreach ( String S In Prefixes) {listener. prefixes. Add (s );} // Start listening Listener. Start (); Console . Writeline ( "Listening ..." ); While ( True ){// Note: The getcontext method will block the thread until the request arrives. Httplistenercontext context = listener. getcontext (); // Obtain the request object Httplistenerrequest request = context. request; Console . Writeline ( "{0} {1} HTTP/1.1" , Request. httpmethod, request. rawurl ); Console . Writeline ( "Accept: {0 }" , String . Join ( "," , Request. accepttypes )); Console . Writeline ("Accept-language: {0 }" , String . Join ( "," , Request. userages )); Console . Writeline ( "User-Agent: {0 }" , Request. useragent ); Console . Writeline ( "Accept-encoding: {0 }" , Request. headers [ "Accept-encoding" ]); Console . Writeline ( "Connection: {0 }" , Request. keepalive?"Keep-alive" : "Close" ); Console . Writeline ( "Host: {0 }" , Request. userhostname ); Console . Writeline ( "Pragma: {0 }" , Request. headers [ "Pragma" ]); // Obtain the response object Httplistenerresponse response = context. response; // Construct the response content  String Responsestring =@ "<HTML>  ; // Set the response header content, length, encoding Response. contentlength64 = system. Text. Encoding . Utf8.getbytecount (responsestring); response. contenttype = "Text/html; charset = UTF-8" ; // Output Response content System. Io. Stream Output = response. outputstream; system. Io. Streamwriter Writer =New System. Io. Streamwriter (Output); writer. Write (responsestring ); // The output stream must be disabled. Writer. Close (); If ( Console . Keyavailable) Break ;} // Close the server Listener. Stop ();}

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.