I have a HTTPLonster which I do not want to stop after every request, so I request the requests to be retrieved asynchronously I'm using BeginGetContext. It's not working properly right now.
Mainly for the startling () function, my IP address is searched properly, and the assignment starts. However, when I get the listener.BeginGetContext (new asyncCallback (OnRequest), listener), then
in StartListening () and this response. Jumps into start-up (ips); In Men
and then it just stops. I'm not sure why there is any sign?
Here's what I've done so far.
This is where I start listening for requests:
public static zero start article (string [] prefix) {HttpListener listener = new HttpListener (); If (prefix == faucet) prefix length == 0) throwing new logic expression ("prefix"); Forex (string in prefixes) {listener.Prefixes.Add ("http: //" + s + "/"); } Listenener.Start (); Console.WriteLine ("\ nListening ..."); Listner. Bangagat Contex (new asynchronous callback (audience query), listener); }
And where I handle the requests:
Public stable zero onquest (IASINSCR result result) {HttpListener listener = (HttpListener) Results. Asyncast; HttpListenerContext reference = listener.EndGetContext (results); String url = context.Request.RawUrl; String [] split = url.Split ('/'); Int lastIndex = split.Length - 1; Int x, y, z; X = Convert. ToInt32 (partition [last index]); Y = Convert toInt32 (partition [last index - 1]); Z = Convert toInt32 (Partition [Last Index - 2]); HttpListenerResponse response = reference Response; Reggae ImageReges = New Reggae (@ "Something"); Var matches = imageRegex.Match (url); If (matches.Success) {string path = "C: \ SomeDir"; Path = string.format (route, matches group [1]. Captain [0], match group [2]. Captain [0], match group [3]. Cappers [0]); // Image bitmap BM = Load new bitmap (path); Memorystream BMstream = new memorystream (); BM. Save (bmstream, image format.png); Byte [] Buffer = BMSTream. ToArray (); // Receive a response stream and write feedback on it. reaction. Contact LayLange 64 = BMSTream Lamps; reaction. Content type = "image / png"; reaction. KeepAlive = True; Stream output = response.OutputStream; Output. Write (buffer, 0, buffer lang); // You must close the output stream output.Close (); Listener.Stop (); } Response.Close (); Listner. Bangagat Contex (new asynchronous callback (audience query), listener); }
And here my main is:
class program {static or oid main (string [] arg) {string name = (args.Length & lt ; 1)? Dns.GetHostName (): args [0]; Try / IPv4 Address IP Andres [] ADRR = Array Try F & L (DNS gatehost entry (string.elevity). Address list, A = & gt; A. AddressFamily == AddressFamily.Inter Network); Console.print line ("your IP address:"); Foreach (IP address address in address) console Videoline ("{0} {1}", name, edit); // Automatically set the IP address string [] ips = addrs.Select (ip = & gt; ip.ToString ()). ToArray (); (IPS) Response.StartListening; } Hold (exception e) {Console.WriteLine (e.Message); }
after BeginGetContext
, this temporary form Receives requests from, so it does not block the calling thread (main thread, in this case). And because it does not block it, the main thread ends and thus your program also ends, because this is the main thread.
You can fix it by using ManualResetEvent
( in the system. Threading
namespace). This blocks the main thread.
class program {public static manual alias Event serverManualResetSevent event; Static Zero Main (string [] ARG) {server to start server {// your code} server manual reset event = new manual reset event (wrong); ServerManualResetEvent.WaitOne (); } Hold {// Your Hold Code}}}
Now the main thread is blocked and if you stop it, or if you stop it from the code, then your program only stops:
Program.ServerManualResetEvent.Set ();
Then it no longer blocks the main thread.
Comments
Post a Comment