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)