base chat module
This commit is contained in:
37
Chat/Chat.sln
Normal file
37
Chat/Chat.sln
Normal file
@@ -0,0 +1,37 @@
|
||||
|
||||
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", "{44FE944D-8D60-4A9C-B739-32F53A033880}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Core", "Core\Core.csproj", "{0BF351F8-CA96-446D-83DE-00CAE7B4CC5D}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestConsoleClient", "TestConsoleClient\TestConsoleClient.csproj", "{E8D23703-2D2E-43C2-9B77-16EA04CD7331}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{44FE944D-8D60-4A9C-B739-32F53A033880}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{44FE944D-8D60-4A9C-B739-32F53A033880}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{44FE944D-8D60-4A9C-B739-32F53A033880}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{44FE944D-8D60-4A9C-B739-32F53A033880}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{0BF351F8-CA96-446D-83DE-00CAE7B4CC5D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{0BF351F8-CA96-446D-83DE-00CAE7B4CC5D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{0BF351F8-CA96-446D-83DE-00CAE7B4CC5D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{0BF351F8-CA96-446D-83DE-00CAE7B4CC5D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{E8D23703-2D2E-43C2-9B77-16EA04CD7331}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{E8D23703-2D2E-43C2-9B77-16EA04CD7331}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{E8D23703-2D2E-43C2-9B77-16EA04CD7331}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{E8D23703-2D2E-43C2-9B77-16EA04CD7331}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {812F7F1E-3CAD-4992-A7E2-D53C6FB1D1AA}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
10
Chat/Chat/Chat.csproj
Normal file
10
Chat/Chat/Chat.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
Chat/Chat/Program.cs
Normal file
10
Chat/Chat/Program.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace Chat
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello, World!");
|
||||
}
|
||||
}
|
||||
}
|
9
Chat/Core/Core.csproj
Normal file
9
Chat/Core/Core.csproj
Normal file
@@ -0,0 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
35
Chat/Core/PClient.cs
Normal file
35
Chat/Core/PClient.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Core
|
||||
{
|
||||
public class PClient
|
||||
{
|
||||
private Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
private EndPoint endPoint;
|
||||
|
||||
public PClient(string ip, int port)
|
||||
{
|
||||
endPoint = new IPEndPoint(IPAddress.Parse(ip), port);
|
||||
}
|
||||
|
||||
public async Task StartAsync()
|
||||
{
|
||||
await socket.ConnectAsync(endPoint);
|
||||
while (true)
|
||||
{
|
||||
string str = Console.ReadLine() ?? string.Empty;
|
||||
byte[] dataBuffer = Encoding.UTF8.GetBytes(str);
|
||||
byte[] headerBuffer = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)dataBuffer.Length));
|
||||
|
||||
await socket.SendAsync(headerBuffer, SocketFlags.None);
|
||||
await socket.SendAsync(dataBuffer, SocketFlags.None);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
69
Chat/Core/PServer.cs
Normal file
69
Chat/Core/PServer.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Core
|
||||
{
|
||||
public class PServer
|
||||
{
|
||||
private Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
|
||||
public PServer(string ip, int port, int backlog)
|
||||
{
|
||||
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(ip), port);
|
||||
serverSocket.Bind(endPoint);
|
||||
serverSocket.Listen(backlog);
|
||||
}
|
||||
|
||||
public async Task StartAsync()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
Socket clientSocket = await serverSocket.AcceptAsync();
|
||||
Console.WriteLine($"[{DateTime.Now}] Accept client: {clientSocket.RemoteEndPoint}");
|
||||
ThreadPool.QueueUserWorkItem(RunAsync, clientSocket);
|
||||
}
|
||||
}
|
||||
|
||||
private async void RunAsync(object? sender)
|
||||
{
|
||||
if (sender == null)
|
||||
return;
|
||||
|
||||
Socket clientSocket = (Socket)sender;
|
||||
byte[] headerBuffer = new byte[2];
|
||||
|
||||
while (true)
|
||||
{
|
||||
// header buffer
|
||||
int n1 = await clientSocket.ReceiveAsync(headerBuffer, SocketFlags.None);
|
||||
if (n1 < 1)
|
||||
{
|
||||
Console.WriteLine($"[{DateTime.Now}] Disconnect client: {clientSocket.RemoteEndPoint}");
|
||||
clientSocket.Dispose();
|
||||
return;
|
||||
}
|
||||
else if (n1 == 1)
|
||||
{
|
||||
await clientSocket.ReceiveAsync(new ArraySegment<byte>(headerBuffer, 1, 1), SocketFlags.None);
|
||||
}
|
||||
|
||||
// data buffer
|
||||
short dataSize = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(headerBuffer));
|
||||
byte[] dataBuffer = new byte[dataSize];
|
||||
|
||||
int totalRecv = 0;
|
||||
while (totalRecv < dataSize)
|
||||
{
|
||||
int n2 = await clientSocket.ReceiveAsync(new ArraySegment<byte>(dataBuffer, totalRecv, dataSize - totalRecv), SocketFlags.None);
|
||||
totalRecv += n2;
|
||||
}
|
||||
Console.WriteLine($"[{DateTime.Now}] {clientSocket.RemoteEndPoint} client: {Encoding.UTF8.GetString(dataBuffer)}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
12
Chat/Server/Program.cs
Normal file
12
Chat/Server/Program.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using Core;
|
||||
|
||||
namespace Server;
|
||||
|
||||
internal class Program
|
||||
{
|
||||
static async Task Main(string[] args)
|
||||
{
|
||||
PServer server = new PServer("127.0.0.1", 20000, 1000);
|
||||
await server.StartAsync();
|
||||
}
|
||||
}
|
14
Chat/Server/Server.csproj
Normal file
14
Chat/Server/Server.csproj
Normal file
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Core\Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
13
Chat/TestConsoleClient/Program.cs
Normal file
13
Chat/TestConsoleClient/Program.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using Core;
|
||||
|
||||
namespace TestConsoleClient
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
static async Task Main(string[] args)
|
||||
{
|
||||
PClient client = new PClient("127.0.0.1", 20000);
|
||||
await client.StartAsync();
|
||||
}
|
||||
}
|
||||
}
|
14
Chat/TestConsoleClient/TestConsoleClient.csproj
Normal file
14
Chat/TestConsoleClient/TestConsoleClient.csproj
Normal file
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Core\Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
Reference in New Issue
Block a user