apm (asynchronous programming model)
This commit is contained in:
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>
|
Reference in New Issue
Block a user