Copy Source | Copy HTML using System; using System.Collections.Generic; using System.Text; namespace HTTPServer { class Server { static void Main( string [] args) { } } }Copy Source | Copy HTML using System; using System.Collections.Generic; using System.Text; namespace HTTPServer { class Server { static void Main( string [] args) { } } }Copy Source | Copy HTML using System; using System.Collections.Generic; using System.Text; namespace HTTPServer { class Server { static void Main( string [] args) { } } }Copy Source | Copy HTML using System; using System.Collections.Generic; using System.Text; namespace HTTPServer { class Server { static void Main( string [] args) { } } }Copy Source | Copy HTML using System; using System.Collections.Generic; using System.Text; namespace HTTPServer { class Server { static void Main( string [] args) { } } }Copy Source | Copy HTML using System; using System.Collections.Generic; using System.Text; namespace HTTPServer { class Server { static void Main( string [] args) { } } }Copy Source | Copy HTML using System; using System.Collections.Generic; using System.Text; namespace HTTPServer { class Server { static void Main( string [] args) { } } }Copy Source | Copy HTML using System; using System.Collections.Generic; using System.Text; namespace HTTPServer { class Server { static void Main( string [] args) { } } }Copy Source | Copy HTML using System; using System.Collections.Generic; using System.Text; namespace HTTPServer { class Server { static void Main( string [] args) { } } }Copy Source | Copy HTML using System; using System.Collections.Generic; using System.Text; namespace HTTPServer { class Server { static void Main( string [] args) { } } }Copy Source | Copy HTML using System; using System.Collections.Generic; using System.Text; namespace HTTPServer { class Server { static void Main( string [] args) { } } }Copy Source | Copy HTML using System; using System.Collections.Generic; using System.Text; namespace HTTPServer { class Server { static void Main( string [] args) { } } }Copy Source | Copy HTML using System; using System.Collections.Generic; using System.Text; namespace HTTPServer { class Server { static void Main( string [] args) { } } }
Copy Source | Copy HTML using System; using System.Collections.Generic; using System.Text; namespace HTTPServer { class Server { static void Main( string [] args) { } } }
Copy Source | Copy HTMLクラス サーバー{TcpListenerリスナー。 // TCPクライアントを受け入れるオブジェクト//サーバーの起動パブリック サーバー ( intポート){//指定されたポートの「リスナー」を作成しますリスナー= 新しい TcpListener (IPAddress.Any、ポート);Listener.Start(); //実行する//無限ループ内while ( true ){//新しい顧客を受け入れるListener.AcceptTcpClient();}}//サーバーを停止します〜 サーバー (){//「リスナー」が作成された場合if (リスナー!= null ){//やめてListener.Stop();}}static void Main( string [] args){//ポート80に新しいサーバーを作成します新しい サーバー ( 80 );}}
Copy Source | Copy HTML
Copy Source | Copy HTML//クライアントハンドラクラスクラスの クライアント{//クラスのコンストラクター。 彼はTcpListenerから受信したクライアントを転送する必要がありますパブリック クライアント (TcpClient クライアント ){//シンプルなHTMLページのコードstring Html = "<html> <body> <h1>動作します!</ h1> </ body> </ html>" ;//必須のヘッダー:サーバーの応答、コンテンツのタイプと長さ。 2行の空行の後-コンテンツ自体string Str = "HTTP / 1.1 200 OK \ nContent-type:text / html \ nContent-Length:" + Html.Length.ToString()+ "\ n \ n" + Html;//文字列をバイト配列にキャストしますbyte [] Buffer = Encoding.ASCII.GetBytes(Str);//クライアントに送信しますClient .GetStream()。Write( Buffer 、 0 、 Buffer .Length);//接続を閉じますクライアント .Close();}}
Copy Source | Copy HTML//新しいクライアントを受け入れ、処理のためにそれらをClientクラスの新しいインスタンスに転送します新しい クライアント (Listener.AcceptTcpClient());
Copy Source | Copy HTMLstatic void ClientThread( Object StateInfo){新しい クライアント ((TcpClient)StateInfo);}
Copy Source | Copy HTML//新しいクライアントを受け入れますTcpClient Client = Listener.AcceptTcpClient();//ストリームを作成しますスレッドスレッド = 新しい スレッド ( 新しい ParameterizedThreadStart (ClientThread));//そして、このスレッドを実行し、受信したクライアントに渡しますThread .Start(クライアント);
Copy Source | Copy HTML//新しい顧客を受け入れます。 クライアントが受け入れられた後、新しいスレッド(ClientThread)に転送されます//スレッドプールを使用します。ThreadPool.QueueUserWorkItem( new WaitCallback (ClientThread)、Listener.AcceptTcpClient());
Copy Source | Copy HTML//必要な最大スレッド数を定義します//プロセッサごとに4つありますint MaxThreadsCount = Environment .ProcessorCount * 4 ;//ワーカースレッドの最大数を設定しますThreadPool.SetMaxThreads(MaxThreadsCount、MaxThreadsCount);//ワーカースレッドの最小数を設定しますThreadPool.SetMinThreads( 2、2 );
Copy Source | Copy HTML//クライアントリクエストを保存する行を宣言します文字列 Request = "" ;//クライアントから受信したデータを保存するバッファbyte [] Buffer = 新しいバイト [ 1024 ];//クライアントから受信したバイト数を格納する変数intカウント;//クライアントストリームからデータを受信するまで読み取りますwhile ((Count = Client.GetStream()。Read( Buffer 、 0 、 Buffer .Length))> 0 ){//このデータを文字列に変換し、リクエスト変数に追加しますリクエスト+ = Encoding.ASCII.GetString( Buffer 、 0 、Count);//リクエストは、シーケンス\ r \ n \ r \ nで中断する必要があります//リクエスト行の長さが4キロバイトを超える場合、データ受信を自分で中断します// POSTリクエスト(など)からデータを受信する必要はありませんが、定期的なリクエスト//理論的には4キロバイトを超えてはいけませんif (Request.IndexOf( "\ r \ n \ r \ n" )> = 0 || Request.Length> 4096 ){休憩 ;}}
Copy Source | Copy HTML//正規表現を使用してクエリ文字列を解析します//同時に、GETリクエストのすべての変数をカットしますReqMatch = Regexに 一致します。 Match (リクエスト、 @ "^ \ w + \ s +([^ \ s \?] +)[^ \ S] * \ s + HTTP /.* |" );//リクエストが失敗した場合if (ReqMatch == Match .Empty){//クライアントにエラー400を渡す-無効なリクエストSendError(クライアント、 400 );帰る}//クエリ文字列を取得しますstring RequestUri = ReqMatch.Groups [ 1 ] .Value;//エスケープされた文字を変換して、元の形式に縮小します//たとえば、「%20」->「」RequestUri = Uri .UnescapeDataString(RequestUri);//行にコロンが含まれる場合、エラー400を送信します//これは、http://example.com/../../file.txtなどのURLから保護するために必要です。if (RequestUri.IndexOf( ".." )> = 0 ){SendError(クライアント、 400 );帰る}//クエリ行が「/」で終わる場合、index.htmlを追加しますif (RequestUri.EndsWith( "/" )){RequestUri + = "index.html" ;}
Copy Source | Copy HTML文字列 FilePath = "www /" + RequestUri;//このファイルがwwwフォルダーに存在しない場合、404エラーを送信しますif (! File .Exists(FilePath)){SendError(クライアント、 404 );帰る}//クエリ文字列からファイル拡張子を取得しますstring Extension = RequestUri.Substring(RequestUri.LastIndexOf( '。' ));//コンテンツタイプstring ContentType = "" ;//ファイル拡張子によってコンテンツのタイプを判別しようとしていますスイッチ (拡張機能){ケース ".htm" :ケース ".html" :ContentType = "text / html" ;休憩 ;ケース 「.css」 :ContentType = "text / stylesheet" ;休憩 ;ケース 「.js」 :ContentType = "text / javascript" ;休憩 ;ケース ".jpg" :ContentType = "image / jpeg" ;休憩 ;ケース 「.jpeg」 :ケース ".png" :ケース ".gif" :ContentType = "image /" + Extension.Substring( 1 );休憩 ;デフォルト :if (Extension.Length> 1 ){ContentType = "application /" + Extension.Substring( 1 );}他に{ContentType = "application / unknown" ;}休憩 ;}//エラーを防ぐためにファイルを開きますFileStream FS;試してみる{FS = new FileStream (FilePath、FileMode.Open、FileAccess.Read、FileShare.Read);}catch ( 例外 ){//エラーが発生した場合、クライアントに500エラーを送信しますSendError(クライアント、 500 );帰る}//ヘッダーを送信しますstring Headers = "HTTP / 1.1 200 OK \ nContent-Type:" + ContentType + "\ nContent-Length:" + FS.Length + "\ n \ n" ;byte [] HeadersBuffer = Encoding.ASCII.GetBytes(ヘッダー);Client.GetStream()。Write(HeadersBuffer、 0 、HeadersBuffer.Length);//ファイルの終わりに達するまでwhile (FS.Position <FS.Length){//ファイルからデータを読み取りますCount = FS.Read( Buffer 、 0 、 Buffer .Length);//そしてそれらをクライアントに渡しますClient.GetStream()。書き込み( Buffer 、 0 、Count);}//ファイルと接続を閉じますFS.Close();Client.Close();
Copy Source | Copy HTML//エラーページを送信するprivate void SendError(TcpClientクライアント、 intコード){//「200 OK」形式の文字列を取得します// HttpStatusCodeはすべてのHTTP / 1.1ステータスコードを保存しますstring CodeStr = Code.ToString()+ "" +((HttpStatusCode)Code).ToString();//シンプルなHTMLページのコードstring Html = "<html> <body> <h1>" + CodeStr + "</ h1> </ body> </ html>" ;//必須のヘッダー:サーバーの応答、コンテンツのタイプと長さ。 2行の空行の後-コンテンツ自体string Str = "HTTP / 1.1" + CodeStr + "\ nContent-type:text / html \ nContent-Length:" + Html.Length.ToString()+ "\ n \ n" + Html;//文字列をバイト配列にキャストしますbyte [] Buffer = Encoding.ASCII.GetBytes(Str);//クライアントに送信しますClient.GetStream()。Write( Buffer 、 0 、 Buffer .Length);//接続を閉じますClient.Close();}
Source: https://habr.com/ru/post/J120157/More articles:パブリックステートバグトラッカーCSSを使用したHTML5プレースホルダーのスタイル設定競争。 記事を書いた-1年間無料のVPNを入手(+ニュース)Windows Phone 7.1でデバイスのハードウェア特性をプログラムで確認する方法。 マンゴーケンコー延長チューブ船から球まで/公共投資の闘争に積極的に関与しているReactOSプロジェクト手書き数式認識「100ドルのラップトップ」は半額になりますマウス。 内観CISの科学論文の引用指数は非常に低いAll Articles