multi thread
This commit is contained in:
36
MultiThread/Server/Server.cs
Normal file
36
MultiThread/Server/Server.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace Server;
|
||||
|
||||
internal class Server
|
||||
{
|
||||
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);
|
||||
|
||||
while (true)
|
||||
{
|
||||
Socket clientSocket = serverSocket.Accept();
|
||||
Console.WriteLine(clientSocket.RemoteEndPoint);
|
||||
|
||||
Thread t1 = new Thread(() =>
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
byte[] buffer = new byte[256];
|
||||
int n1 = clientSocket.Receive(buffer);
|
||||
if (n1 < 1)
|
||||
{
|
||||
clientSocket.Dispose();
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
t1.Start();
|
||||
}
|
||||
}
|
||||
}
|
10
MultiThread/Server/Server.csproj
Normal file
10
MultiThread/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