ホーム » .NET » C# のコードを C++/CLI に

2022年12月
 123
45678910
11121314151617
18192021222324
25262728293031

カテゴリー

アーカイブ

ブログ統計情報

  • 79,653 アクセス



C# のコードを C++/CLI に

先日の「簡易 Web サーバ C#」のコードを C++ で.

///////////////////////////////////////////////////////////////////////////////////
//	C# -> C++/CLI
///////////////////////////////////////////////////////////////////////////////////

#include <stdio.h>
#include <tchar.h>

//using	System;
//using	System.IO;
//using	System.Net;

#using  <System.dll>

bool	test	(void)
{
//
//	class SimpleWebServer
//	{
//		static void Main()
//		{
//			string		root = @"c:\wwwroot\";	// ドキュメント・ルート
//					root = @".\";
			System::String^	root = "./" ;
//			string		prefix = "http://*/";	// 受け付けるURL
//					prefix = "http://127.0.0.1:65432/" ;
			System::String^	prefix = "http://127.0.0.1:55555/" ;
//	
//			HttpListener 			listener = new HttpListener();
			System::Net::HttpListener^	listener = gcnew System::Net::HttpListener();
//			listener. Prefixes. Add(prefix);	// プレフィックスの登録
			listener->Prefixes->Add(prefix);
//			listener. Start();
			listener->Start();
//	
//			while (true) {
			while (true) {
//				HttpListenerContext			context = listener. GetContext();
				System::Net::HttpListenerContext^	context = listener->GetContext();
//				HttpListenerRequest			req = context. Request;
				System::Net::HttpListenerRequest^	req = context->Request;
//				HttpListenerResponse			res = context. Response;
				System::Net::HttpListenerResponse^	res = context->Response;
//	
//				Console.	 WriteLine(req. RawUrl);
				System::Console::WriteLine(req->RawUrl);
//	
//				// リクエストされたURLからファイルのパスを求める
//				string		path = root + req. RawUrl. Replace("/", "\\");
				System::String^	path = root + req->RawUrl->Replace("/", "\\");
//	
//				// ファイルが存在すればレスポンス・ストリームに書き出す
//				if (            File. Exists(path)) {
				if (System::IO::File::Exists(path)) {
//					byte[]			content =             File. ReadAllBytes(path);
					array<System::Byte>^	content = System::IO::File::ReadAllBytes(path);
//					res. OutputStream. Write(content, 0, content. Length);
					res->OutputStream->Write(content, 0, content->Length);
//					}
					}
//				res. Close();
				res->Close();
//				}
				}
//			}
//		}
//
	return	true ;
	}

int _tmain(int argc, _TCHAR* argv[])
{
	::test() ;
	return 0;
	}

Web サーバ   C++/CLI

Is this 投稿 useful? Useful Useless 0 of 0 people say this 投稿 is useful.

2件のコメント

コメントは停止中です。

%d人のブロガーが「いいね」をつけました。