LoginSignup
0
0

cscの作法 その321

Last updated at Posted at 2023-05-29

概要

cscの作法、調べてみた。
サーバーサイドc#なwebserverを発明したので紹介する。

  • ポート40002
  • アドレスlocalhost
  • ブラウザからアクセス
  • サーバーサイドc#
  • じゃんけんを返す

写真

image.png

サンプルコード



using System;
using System.Net;
using System.Text;
using System.Globalization;

namespace App
{
	class Program {
		static void Main(string[] args) {
			try
			{
				HttpListener listener = new HttpListener();
				listener.Prefixes.Clear();
				listener.Prefixes.Add(@"http://localhost:40002/");
				listener.Start();
				Console.WriteLine("start");
				while (true)
				{
					HttpListenerContext context = listener.GetContext();
					HttpListenerRequest request = context.Request;
					HttpListenerResponse response = context.Response;
					Console.WriteLine(request.RawUrl);
					if (request.RawUrl.IndexOf("favicon") == -1)
					{
						string a = request.RawUrl.Replace("/index.html?q=", "");
						string b = "2";
						string name = "012";
						Random random = new Random();
						StringInfo nameInfo = new StringInfo(name);
						int newNameIndex = random.Next(nameInfo.LengthInTextElements);
						b = nameInfo.SubstringByTextElements(newNameIndex, 1);
						string str = "じゃんけん  0, グー 1, チョキ  2, パー";
						switch (a)
						{
						case "0":
							switch (b)
							{
							case "0":
								str = "グーとグーで、あいこ";
							break;
							case "1":
								str = "グーとチョキで、あなたの勝ち";
							break;
							case "2":
								str = "グーとパーで、あなたの負け";
							break;
							default:
							break;
							}
						break;
						case "1":
							switch (b)
							{
							case "0":
								str = "チョキとグーで、あなたの負け";
							break;
							case "1":
								str = "チョキとチョキで、あいこ";
							break;
							case "2":
								str = "チョキとパーで、あなたの勝ち";
							break;
							default:
							break;
							}
						break;
						case "2":
							switch (b)
							{
							case "0":
								str = "パーとグーで、あなたの勝ち";
							break;
							case "1":
								str = "パーとチョキで、あなたの負け";
							break;
							case "2":
								str = "パーとパーで、あいこ";
							break;
							default:
							break;
							}
						break;
						default:
						break;
						}
						string html0 = "<html><head><meta charset='utf-8'/></head><body><h1>server side c#</h1><form method='get' action='index.html'>入力:<input type='text' name='q' size='30'><input type='submit' value='送信'><br><textarea rows='15' cols='50'>";
						string html1 = "</textarea></form></body></html>";
						byte[] text = Encoding.UTF8.GetBytes(html0 + str + html1);
						response.OutputStream.Write(text, 0, text.Length);
						Console.WriteLine("ok");
					}
					else
					{
						response.StatusCode = 404;
						Console.WriteLine("404");
					}
					response.Close();
				}
			}
			catch (Exception e)
			{
				Console.WriteLine(e.Message);
			}
		}
	}
}





以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0