echo server

This commit is contained in:
2023-02-06 13:11:18 +09:00
parent 6000d5a4e7
commit 8e51b3d046
7 changed files with 148 additions and 0 deletions

39
Echo/Server/Server.cs Normal file
View File

@@ -0,0 +1,39 @@
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Server;
internal class Server
{
static void Main(string[] args)
{
using (Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 20000);
serverSocket.Bind(endPoint);
serverSocket.Listen(20);
using (Socket clientSocket = serverSocket.Accept())
{
Console.WriteLine(clientSocket.RemoteEndPoint);
while (true)
{
byte[] buffer = new byte[256];
int totalBytes = clientSocket.Receive(buffer);
if (totalBytes < 1)
{
Console.WriteLine("Disconnecting client...");
return;
}
string str = Encoding.UTF8.GetString(buffer);
Console.WriteLine(str);
clientSocket.Send(buffer);
}
}
}
}
}

10
Echo/Server/Server.csproj Normal file
View File

@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>