-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
XuanThuLab
committed
Aug 22, 2020
1 parent
a4a4257
commit ccbd3fd
Showing
4 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} | ||
} |