Пример #1
0
        void Upload()
        {
            TcpListener listener = new TcpListener(new IPEndPoint(IPAddress.Loopback, 0));

            listener.Start();

            Console.WriteLine("Listening on: {0}", listener.LocalEndpoint);

            uploader = new NativeUploader();
            uploader.UploadStream("http://127.0.0.1:" + ((IPEndPoint)listener.LocalEndpoint).Port.ToString(), 1000, () =>
            {
                Console.WriteLine("Upload completed.");
            });

            listener.BeginAcceptSocket((IAsyncResult res) =>
            {
                ThreadPool.QueueUserWorkItem((v) =>
                {
                    using (var socket = listener.EndAcceptSocket(res)) {
                        byte [] buffer = new byte[1024];
                        int read;

                        // receive headers
                        read = socket.Receive(buffer);
                        BeginInvokeOnMainThread(() => { status.Caption = "Received headers..."; dvc.ReloadData(); });
                        Console.WriteLine("\n" + System.Text.ASCIIEncoding.ASCII.GetString(buffer, 0, read));

                        // send 100 Continue
                        socket.Send(System.Text.ASCIIEncoding.ASCII.GetBytes(@"HTTP/1.1 100 Continue\r\n\r\n"));

                        // receive data
                        read = socket.Receive(buffer);
                        BeginInvokeOnMainThread(() => { status.Caption = "Received data"; dvc.ReloadData(); });
                        Console.WriteLine("\n" + System.Text.ASCIIEncoding.ASCII.GetString(buffer, 0, read));

                        // send 200 OK
                        socket.Send(System.Text.ASCIIEncoding.ASCII.GetBytes(@"HTTP/1.1 200 OK\r\n\r\n"));
                    }

                    listener.Stop();
                });
            }, null);
        }
Пример #2
0
		void Upload ()
		{
			TcpListener listener = new TcpListener (new IPEndPoint (IPAddress.Loopback, 0));
			listener.Start ();
			
			Console.WriteLine ("Listening on: {0}", listener.LocalEndpoint);
			
			uploader = new NativeUploader ();
			uploader.UploadStream ("http://127.0.0.1:" + ((IPEndPoint) listener.LocalEndpoint).Port.ToString (), 1000, () =>
			{
				Console.WriteLine ("Upload completed.");
			});
			
			listener.BeginAcceptSocket ((IAsyncResult res) =>
			{
				ThreadPool.QueueUserWorkItem ((v) => 
				{
					using (var socket = listener.EndAcceptSocket (res)) {
						byte [] buffer = new byte[1024];
						int read;
						
						// receive headers
						read = socket.Receive (buffer);
						BeginInvokeOnMainThread (() => { status.Caption = "Received headers..."; dvc.ReloadData (); });
						Console.WriteLine ("\n" + System.Text.ASCIIEncoding.ASCII.GetString (buffer, 0, read));
						
						// send 100 Continue
						socket.Send (System.Text.ASCIIEncoding.ASCII.GetBytes (@"HTTP/1.1 100 Continue\r\n\r\n"));
						
						// receive data
						read = socket.Receive (buffer);
						BeginInvokeOnMainThread (() => { status.Caption = "Received data"; dvc.ReloadData (); });
						Console.WriteLine ("\n" + System.Text.ASCIIEncoding.ASCII.GetString (buffer, 0, read));
						
						// send 200 OK
						socket.Send (System.Text.ASCIIEncoding.ASCII.GetBytes (@"HTTP/1.1 200 OK\r\n\r\n"));
					}
					
					listener.Stop ();
				});
			}, null);
		}