apm (asynchronous programming model)
This commit is contained in:
31
APM/APM.sln
Normal file
31
APM/APM.sln
Normal file
@@ -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", "{D04D1A29-83AA-4E60-8CEA-51C4FECBF54A}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client", "Client\Client.csproj", "{79E1A445-695E-4CBA-98D4-EE80DCD0D892}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{D04D1A29-83AA-4E60-8CEA-51C4FECBF54A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{D04D1A29-83AA-4E60-8CEA-51C4FECBF54A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{D04D1A29-83AA-4E60-8CEA-51C4FECBF54A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{D04D1A29-83AA-4E60-8CEA-51C4FECBF54A}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{79E1A445-695E-4CBA-98D4-EE80DCD0D892}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{79E1A445-695E-4CBA-98D4-EE80DCD0D892}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{79E1A445-695E-4CBA-98D4-EE80DCD0D892}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{79E1A445-695E-4CBA-98D4-EE80DCD0D892}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {7E0DE83C-5101-4E7B-AB41-42B7DC8A4F98}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
10
APM/APM/APM.csproj
Normal file
10
APM/APM/APM.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>
|
10
APM/APM/Program.cs
Normal file
10
APM/APM/Program.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace APM
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello, World!");
|
||||
}
|
||||
}
|
||||
}
|
50
APM/Client/Client.cs
Normal file
50
APM/Client/Client.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
|
||||
namespace Client;
|
||||
|
||||
internal class Client
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 20000);
|
||||
|
||||
socket.BeginConnect(endPoint, ConnectCompleted, socket);
|
||||
|
||||
while (true)
|
||||
{
|
||||
string str = Console.ReadLine() ?? string.Empty;
|
||||
socket.Send(Encoding.UTF8.GetBytes(str));
|
||||
}
|
||||
}
|
||||
|
||||
private static void ConnectCompleted(IAsyncResult ar)
|
||||
{
|
||||
if (ar.AsyncState == null)
|
||||
return;
|
||||
|
||||
Socket socket = (Socket)ar.AsyncState;
|
||||
socket.EndConnect(ar);
|
||||
|
||||
string str = Console.ReadLine() ?? string.Empty;
|
||||
byte[] buffer = Encoding.UTF8.GetBytes(str);
|
||||
|
||||
socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, SendCompleted, socket);
|
||||
}
|
||||
|
||||
private static void SendCompleted(IAsyncResult ar)
|
||||
{
|
||||
if (ar.AsyncState == null)
|
||||
return;
|
||||
|
||||
Socket socket = (Socket)ar.AsyncState;
|
||||
socket.EndSend(ar);
|
||||
|
||||
string str = Console.ReadLine() ?? string.Empty;
|
||||
byte[] buffer = Encoding.UTF8.GetBytes(str);
|
||||
|
||||
socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, SendCompleted, socket);
|
||||
}
|
||||
}
|
10
APM/Client/Client.csproj
Normal file
10
APM/Client/Client.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>
|
61
APM/Server/Server.cs
Normal file
61
APM/Server/Server.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
|
||||
namespace Server;
|
||||
|
||||
internal class Server
|
||||
{
|
||||
private static readonly int BUFFER_SIZE = 256;
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
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(1000);
|
||||
|
||||
serverSocket.BeginAccept(AcceptCompleted, serverSocket);
|
||||
|
||||
while (true)
|
||||
{
|
||||
Thread.Sleep(1000);
|
||||
}
|
||||
}
|
||||
|
||||
private static void AcceptCompleted(IAsyncResult ar)
|
||||
{
|
||||
if (ar.AsyncState == null)
|
||||
return;
|
||||
|
||||
Socket serverSocket = (Socket)ar.AsyncState;
|
||||
Socket clientSocket = serverSocket.EndAccept(ar);
|
||||
|
||||
Console.WriteLine(clientSocket.RemoteEndPoint);
|
||||
|
||||
serverSocket.BeginAccept(AcceptCompleted, serverSocket);
|
||||
|
||||
byte[] buffer = new byte[BUFFER_SIZE];
|
||||
clientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, ReceiveCompleted, (clientSocket, buffer)); // state: tuple
|
||||
}
|
||||
|
||||
private static void ReceiveCompleted(IAsyncResult ar)
|
||||
{
|
||||
if (ar.AsyncState == null)
|
||||
return;
|
||||
|
||||
(Socket clientSocket, byte[] buffer) = ((Socket, byte[]))ar.AsyncState;
|
||||
int n1 = clientSocket.EndReceive(ar);
|
||||
if (n1 < 1)
|
||||
{
|
||||
Console.WriteLine("Client is disconnected");
|
||||
clientSocket.Dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
Console.WriteLine(Encoding.UTF8.GetString(buffer));
|
||||
|
||||
byte[] newBuffer = new byte[BUFFER_SIZE];
|
||||
clientSocket.BeginReceive(newBuffer, 0, newBuffer.Length, SocketFlags.None, ReceiveCompleted, (clientSocket, newBuffer)); // state: tuple
|
||||
}
|
||||
}
|
10
APM/Server/Server.csproj
Normal file
10
APM/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