echo server

main
syneffort 2 years ago
parent 6000d5a4e7
commit 8e51b3d046
  1. 38
      Echo/Client/Client.cs
  2. 10
      Echo/Client/Client.csproj
  3. 31
      Echo/Echo.sln
  4. 10
      Echo/Echo/Echo.csproj
  5. 10
      Echo/Echo/Program.cs
  6. 39
      Echo/Server/Server.cs
  7. 10
      Echo/Server/Server.csproj

@ -0,0 +1,38 @@
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Client;
internal class Client
{
static void Main(string[] args)
{
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 20000);
socket.Connect(endPoint);
while (true)
{
string str = Console.ReadLine();
if (str == "exit")
return;
byte[] buffer = Encoding.UTF8.GetBytes(str);
socket.Send(buffer);
byte[] buffer2 = new byte[256];
int byteRead = socket.Receive(buffer2);
if (byteRead < 1)
{
Console.WriteLine("Disconnecting server...");
return;
}
string str2 = Encoding.UTF8.GetString(buffer2);
Console.WriteLine($"[Receive] {str2}");
}
}
}
}

@ -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>

@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.3.32929.385
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Server", "Server\Server.csproj", "{E8C28E9B-3C0F-4A89-9F95-FB078E7B1D81}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client", "Client\Client.csproj", "{224CAEA2-AA8A-4DA7-AE3C-F9BE71938D63}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E8C28E9B-3C0F-4A89-9F95-FB078E7B1D81}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E8C28E9B-3C0F-4A89-9F95-FB078E7B1D81}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E8C28E9B-3C0F-4A89-9F95-FB078E7B1D81}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E8C28E9B-3C0F-4A89-9F95-FB078E7B1D81}.Release|Any CPU.Build.0 = Release|Any CPU
{224CAEA2-AA8A-4DA7-AE3C-F9BE71938D63}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{224CAEA2-AA8A-4DA7-AE3C-F9BE71938D63}.Debug|Any CPU.Build.0 = Debug|Any CPU
{224CAEA2-AA8A-4DA7-AE3C-F9BE71938D63}.Release|Any CPU.ActiveCfg = Release|Any CPU
{224CAEA2-AA8A-4DA7-AE3C-F9BE71938D63}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1B391772-9687-4A60-8A3B-A725F424A383}
EndGlobalSection
EndGlobal

@ -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>

@ -0,0 +1,10 @@
namespace Echo
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}

@ -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);
}
}
}
}
}

@ -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>
Loading…
Cancel
Save