
実現したいこと
・クライエント側のブラウザに"Hello, client!"を表示させる(webアプリ作成)。
(クライエントがサーバー側のフォルダにある画像ファイルから表示したい画像のファイル名をリクエストし、受信した画像をクライアント側のブラウザに表示する。最終的には、中間にキャッシュサーバーを置き、サーバーからのレスポンスタイムを短くすることが目的になります。
前提
Mac.OS
Visual Studio
C#
.NET 7
TCP Listener/Client
NetWorkStream
この課題は、V Sを活用してC#、.NET 7、TCP Listener/Clientを用いたプログラムを作ることだけで、テンプレートの指示はされておりません。
自分自身、C#, VSは一度も使ったことがなく、いろんなサイトを調べていますが、MacのV Sでこのようなアプリの参考があまり見つけられなかったため、独自でアプリを構築しているところでした。
もし、アドバイスやご存じの参考サイト等ございましたら、ご教示いただけると幸いです。
発生している問題・エラーメッセージ
"HT" あるいは"HTTP1.1"が表示される。
該当のソースコード
<クライアント側:Index.cs.html.cs>
C#
1using System; 2using System.Collections.Generic; 3using System.Diagnostics; 4using System.IO.Compression; 5using System.Linq; 6using System.Net; 7using System.Net.Sockets; 8using System.Text; 9using Microsoft.AspNetCore.Mvc; 10using Microsoft.AspNetCore.Mvc.RazorPages; 11 12 13namespace client.Pages 14{ 15 public class IndexModel : PageModel 16 { 17 private readonly ILogger<IndexModel> _logger; 18 19 public List<string> receiveList { get; set; } = new List<string>(); 20 public List<string> receiveImage { get; set; } = new List<string>(); 21 22 23 public IndexModel(ILogger<IndexModel> logger) 24 { 25 _logger = logger; 26 } 27 28 public void OnGet() 29 { 30 receiveList = getList(); 31 receiveImage = downloadImage(); 32 33 _logger.LogInformation("Response: {@Response}", receiveList); 34 } 35 36 37 public List<string> getList() 38 { 39 List<string> responseArray = new List<string>(); 40 byte[] buffer = new byte[1024 * 8]; 41 42 try 43 { 44 var client = new TcpClient(); 45 client.Connect("localhost", 8081); 46 47 var stream = client.GetStream(); 48 // request 49 buffer = Encoding.UTF8.GetBytes("hi"); 50 stream.Write(buffer, 0, buffer.Length); 51 52 // response 53 var length = stream.Read(buffer, 0, buffer.Length); 54 var response = Encoding.UTF8.GetString(buffer, 0, length); 55 responseArray.Add(response); 56 57 client.Close(); 58 } 59 catch (Exception ex) 60 { 61 responseArray.Add($"Error occurred: {ex.Message}"); 62 } 63 64 return responseArray; 65 } 66 } 67}
<サーバー側:Program.cs>
C#
1using Microsoft.AspNetCore.Hosting; 2using Microsoft.Extensions.Hosting; 3using Microsoft.AspNetCore.Http; 4using Microsoft.Extensions.DependencyInjection; 5using System; 6using System.Net; 7using System.Net.Sockets; 8using System.Text; 9 10public class Program 11{ 12 public static void Main(string[] args) 13 { 14 createBuilder(args); 15 16 TcpListener server = new TcpListener(IPAddress.Any, 8081); 17 server.Start(); 18 19 while (true) 20 { 21 TcpClient client = server.AcceptTcpClient(); 22 23 HandleConnection(client); 24 25 client.Close(); 26 server.Stop(); 27 } 28 } 29 30 private static void HandleConnection(TcpClient client) 31 { 32 List<string> fileList = new List<string>(); 33 34 using (NetworkStream stream = client.GetStream()) 35 { 36 // Receive data 37 byte[] buffer = new byte[1024 * 8]; 38 int byteSize = stream.Read(buffer, 0, buffer.Length); 39 string request = Encoding.UTF8.GetString(buffer, 0, byteSize); 40 41 if (request == "hi") 42 { 43 // Send the list of file 44 StreamWriter writer = new StreamWriter(stream, Encoding.UTF8); 45 var response = "Hello, client!"; 46 47 writer.Write(response); 48 } 49 //else 50 //{ 51 // // Send an error message 52 // byte[] errorHeader = Encoding.UTF8.GetBytes("error:Invalid request"); 53 54 // stream.Write(errorHeader, 0, errorHeader.Length); 55 //} 56 } 57 } 58 59 60 public static void createBuilder(string[] args) 61 { 62 // Add services to the container. 63 var builder = WebApplication.CreateBuilder(args); 64 builder.Services.AddRazorPages(); 65 66 var app = builder.Build(); 67 68 // Configure the HTTP request pipeline. 69 if (!app.Environment.IsDevelopment()) 70 { 71 app.UseExceptionHandler("/Error"); 72 // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. 73 app.UseHsts(); 74 } 75 76 app.UseHttpsRedirection(); 77 app.UseStaticFiles(); 78 79 app.UseRouting(); 80 81 app.UseAuthorization(); 82 83 app.MapRazorPages(); 84 85 app.Run(); 86 87 } 88}
試したこと
IPアドレス、ポート番号の変更
MVCでの設計
補足情報(FW/ツールのバージョンなど)
この課題がC#、.NET 7、TCP Listener/Clientを使うことが条件のため、他のソケット通信は試していません。
Firewallはオフにして、すべてのコネクトを受け入れられるように設定しています。




