SkyDrive へ移動 ※ VS2012 用 ベースは Form アプリケーションです。本体を非表示にする状態と本体を表示にする状態を通知領域のアイコンを右クリックして表示されるメニューから切り替えれます。 機能としては、ブラウザのアドレスバーの内容を受信してブラウザに返し、本体ではデバッグ表示を行っています。本来の目的は、データベースのデータを更新したり、JSON で返す データベースサーバのテスト用アプリを想定しています。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.Net.Http; using System.Net.Sockets; using System.Reflection; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Forms; namespace HttpServer { public partial class Form1 : Form { // メニュー用オブジェクト private ContextMenu myContextMenu = null; private MenuItem myMenuItem1 = null; private MenuItem myMenuItem2 = null; private MenuItem myMenuItem3 = null; private NotifyIcon myNotifyIcon = null; public Form1() { InitializeComponent(); // メニュー用のインスタンス作成 myContextMenu = new ContextMenu(); myMenuItem1 = new MenuItem(); myMenuItem2 = new MenuItem(); myMenuItem3 = new MenuItem(); // コンテキストメニューにメニュー項目を一つ追加 myContextMenu.MenuItems.AddRange(new MenuItem[] { myMenuItem1, myMenuItem2,myMenuItem3 }); // ****************************************** // メニュー項目1の設定 // ****************************************** myMenuItem1.Index = 0; // 親メニュー内のメニュー項目の位置 myMenuItem1.Text = "終了"; // 終了処理 myMenuItem1.Click += async (Object sender,EventArgs e) => { HttpClient client = new HttpClient(); try { await client.GetStringAsync("http://localhost:8080/q"); } catch (Exception ex) { Debug.WriteLine(ex.Message); } client.Dispose(); myNotifyIcon.Visible = false; myNotifyIcon.Dispose(); Thread.Sleep(5000); System.Windows.Forms.Application.Exit(); }; // ****************************************** // メニュー項目2の設定 // ****************************************** myMenuItem2.Index = 1; // 親メニュー内のメニュー項目の位置 myMenuItem2.Text = "非表示"; // 終了処理 myMenuItem2.Click += (Object sender, EventArgs e) => { this.ShowInTaskbar = false; // タスク バーに表示しない this.WindowState = FormWindowState.Minimized; // 最小化 this.Opacity = 0; // 透明 }; // ****************************************** // メニュー項目3の設定 // ****************************************** myMenuItem3.Index = 2; // 親メニュー内のメニュー項目の位置 myMenuItem3.Text = "表示"; // 終了処理 myMenuItem3.Click += (Object sender, EventArgs e) => { this.ShowInTaskbar = true; // タスク バーに表示 this.WindowState = FormWindowState.Normal; // 通常 this.Opacity = 100; // 不透明 }; // NotifyIcon : 通知領域にアイコンを作成するコンポーネント myNotifyIcon = new NotifyIcon(new Container()); Assembly myAssembly = Assembly.GetExecutingAssembly(); myNotifyIcon.Icon = new Icon("Server.ico"); myNotifyIcon.Text = "lightbox コマンドサーバー"; myNotifyIcon.Visible = true; // メニューをセット myNotifyIcon.ContextMenu = myContextMenu ; } // フォームの初期処理 private void Form1_Load(object sender, EventArgs e) { // スレッド開始 new Thread( () => { // HTTP プロトコル用リスナー TcpListener tl = new TcpListener(System.Net.IPAddress.Any, 8080); tl.Start(); TcpClient tcp = null; NetworkStream stream = null; StreamReader reader = null; StreamWriter writer = null; string line = null; string lineall = null; while (true) { // HTTP の受信待ち tcp = tl.AcceptTcpClient(); // 以下は、受信した場合に処理されます // データの入り口 ( NetworkStream ) stream = tcp.GetStream(); // これが無いと Google Chrome は読み込まなかった Thread.Sleep(500); // ストリームを読み込むオブジェクトを取得 reader = new StreamReader(stream); // writer = new StreamWriter(stream, Encoding.GetEncoding("shift_jis")); writer = new StreamWriter(stream, new UTF8Encoding(false)); // 一回の受信で取得した全文字列 lineall = ""; if (stream.DataAvailable) { // 非同期で一行取得 while (true) { line = reader.ReadLine(); // 空でチェックするしかないようです。 if (line == "") { break; } lineall += line + "\n"; } } Debug.WriteLine(lineall); // HTTP プロトコルに従って、ヘッダと本文を返す writer.WriteLine("HTTP/1.1 200 OK"); writer.WriteLine("Content-Type: text/plain; charset=utf-8"); int length = Encoding.GetEncoding("utf-8").GetByteCount(lineall); writer.WriteLine("Content-Length: " + length); writer.WriteLine(); writer.Write(lineall); // オブジェクトを閉じる writer.Close(); reader.Close(); stream.Close(); if (lineall == "") { // Console.WriteLine("データがありません"); continue; } // アドレスバーで、http://localhost:8080/q と入力した場合はスレッドを終了 if (lineall.Length > 10) { if (lineall.Substring(0, 7) == "GET /q ") { tl.Stop(); break; } } } Debug.WriteLine("スレッドを終了します"); myNotifyIcon.Visible = false; myNotifyIcon.Dispose(); Thread.Sleep(5000); System.Windows.Forms.Application.Exit(); }).Start(); } } }
関連する記事 Framework4.5(C#) のコンソールアプリケーションで、とても簡単に HTTP サーバーを作成できます
|
【VS(C#)の最新記事】
- Replit : cs-list
- C# : Excel の新しいブックのデフォルトのシートのセルに直接値をセットして、オートフィルを Range オブジェクトから実行する
- C#( Form ) : ウインドウ枠の無い吹き出しの作成
- C# のタプル( Visual Studio 2017 でテスト )
- C# : インターネット上の JSON ファイルのフォーマットを クラスとして定義して1行でオブジェクト化して使用する
- C# の文法的文字列処理
- C# : System.Data.Odbc によるデータベースのテーブルからのデータ取得処理( サンプルの SQL は MySQL 用です )
- C# : Excel を データベースとして DataGridView に読み込む
- C# : dynamic 型 による Excel へのアクセス
- C# : フォームを表示せずに、通知領域にアイコンを表示させる常駐プログラム
- Microsoft Access に対してSQLを入力してその結果を DataGridView に表示する最も簡単なコード
- C# : System.Data.Odbc データ取得(SELECT)処理( MySQL ) : ※ using 無し( Dispose 実行 )
- C# : SQL 文を外部テキストにして、String.Format でデータ部分を置き換えて利用する
- C# コンソールアプリを AN HTTPD で実行
- C# : SQLServer( SQLExpress ) の SMO を使用してテーブルの CREATE TABLE 文 を取得する
- C# : DataGridView に TKMP.DLL の IMAP(POP3) で受信したメールを非同期に表示する( 添付ファイルも取得 )
- C# : TKMP.DLLを使った、メール送信テンプレート
- C# と VB.net : TKMP.DLL を使って IMAP でメール本文の一覧を取得する( コンソール )
- C# でDataTable と DataSource を使用して、DataGridView にデータを表示するテンプレート( 行をダブルクリックしてダイアログを表示して行データを処理 )
- (C#) / VS2010 または VS2012 : TKMP.DLL(3.1.2 または 3.1.8)を使った、『さくらインターネット』用メール送信テンプレート