echo server
This commit is contained in:
39
Echo/Server/Server.cs
Normal file
39
Echo/Server/Server.cs
Normal 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
10
Echo/Server/Server.csproj
Normal 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>
|
Reference in New Issue
Block a user