Skip to content

Commit

Permalink
Tpc Server
Browse files Browse the repository at this point in the history
  • Loading branch information
XuanThuLab committed Aug 22, 2020
1 parent a4a4257 commit ccbd3fd
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 0 deletions.
27 changes: 27 additions & 0 deletions CS029_Networking/9.TcpListener/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/netcoreapp2.1/HttpClientExample.dll",
"args": [],
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
42 changes: 42 additions & 0 deletions CS029_Networking/9.TcpListener/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/HttpClientExample.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/HttpClientExample.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"${workspaceFolder}/HttpClientExample.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
]
}
13 changes: 13 additions & 0 deletions CS029_Networking/9.TcpListener/HttpClientExample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Server.WebListener" Version="1.1.4" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="4.5.1" />
</ItemGroup>

</Project>
86 changes: 86 additions & 0 deletions CS029_Networking/9.TcpListener/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using System.Net;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.IO;

namespace TCP
{
class Program
{
public class TpcServerAsyncv {
readonly int PortNumber;
public TpcServerAsyncv(int portNumber) => PortNumber = portNumber;
public async Task StartLinster()
{
try
{
var listener = new TcpListener(IPAddress.Any, PortNumber);
Console.WriteLine($"Listener lắng nghe ở cổng {PortNumber}");
listener.Start();

while (true)
{
Console.WriteLine("Chờ client kết nối ...");
TcpClient client = await listener.AcceptTcpClientAsync();
Task t = RunClientRequestAsync(client);
}
}
catch (Exception ex)
{
Console.WriteLine($"Exception of type {ex.GetType().Name}, Message: {ex.Message}");
}
}
private Task RunClientRequestAsync(TcpClient client)
{
Action action = async () => {
try
{
using (client)
{
Console.WriteLine("client kết nối");
using (NetworkStream stream = client.GetStream())
using (StreamWriter writer = new StreamWriter(stream))
using (StreamReader reader = new StreamReader(stream))
{
writer.AutoFlush = true;
bool exit = false;
while (!exit) {
string data = await reader.ReadLineAsync();
switch (data.ToLower())
{
case "time":
await writer.WriteLineAsync(DateTime.Now.ToLongTimeString());
break;
case "exit":
exit = true;
await writer.WriteLineAsync("exit");
break;
default:
await writer.WriteLineAsync("Không thấy lệnh");
break;
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Lỗi {ex.GetType().Name}, Message: {ex.Message}");
}
Console.WriteLine("Client ngắt kế nối");
};

Task task = new Task(action);
task.Start();
return task;
}
}
static async Task Main(string[] args)
{
await (new TpcServerAsyncv(1950)).StartLinster();
}

}
}

0 comments on commit ccbd3fd

Please sign in to comment.