Create a simple HTTP service (automatically find unused ports)

Source: Internet
Author: User

            vart =NewThread (NewThreadStart (() ={HttpListener Listener=NewHttpListener (); varprefix =string. Format ("http://localhost:{0}/", Port); Listener.                Prefixes.add (prefix); Listener.                Start (); HttpListenerContext Context=Listener.                GetContext (); HttpListenerRequest Request=context.                Request; Httplistenerresponse Response=context.                Response; stringResponsestring ="<HTML><BODY> Hello world!</body>"; byte[] buffer =System.Text.Encoding.UTF8.GetBytes (responsestring); Response. ContentLength64=buffer.                Length; System.IO.Stream Output=Response.                OutputStream; Output. Write (Buffer,0, buffer.                Length); Output.                Close (); Listener.            Stop ();            })); T.start ();

Using System.Net;

        Static intGetport () {ipglobalproperties ipproperties=ipglobalproperties.getipglobalproperties (); Ipendpoint[] Ipendpoints=ipproperties.getactivetcplisteners (); varPort = -; varFlag =true;  while(flag) {flag= Ipendpoints.any (P = = {returnP.port = = -; }); if(flag) {Port+=1; if(Port = =60000) {MessageBox.Show ("The ports are occupied, please contact the Administrator"); Flag=false; Environment.exit (0); }                }                Else{flag=false;  Break; }            }            returnPort; }

Create a simple HTTP service (automatically find unused ports)

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.