Inicia o pograma com server e client de socket TPC.

main
kevin.caires 2 years ago
commit 2b664bf803

1
.gitignore vendored

@ -0,0 +1 @@
obj/*

41
.vscode/tasks.json vendored

@ -0,0 +1,41 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/backDot.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/backDot.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/backDot.csproj"
],
"problemMatcher": "$msCompile"
}
]
}

@ -0,0 +1,20 @@
using System;
namespace backDot.Commom{
public class Constants{
public static int Port = 3666;
public static string EndOfMessage = "<|EOM|>";
public static string GetServerHost(){
string? _clientHost = Environment.GetEnvironmentVariable("backdothost");
if (_clientHost == null || _clientHost == ""){
_clientHost = "127.0.0.0";
}
Console.WriteLine(_clientHost);
return _clientHost;
}
}
}

@ -0,0 +1,32 @@
using System;
using System.Threading.Tasks;
using backDot.TheDot;
namespace backDot;
public class Program{
public static async Task Main(string[] args){
string invalidMessage = "Invalid args!";
if (args.Length != 1){
Console.WriteLine(invalidMessage);
return;
}
string _arg = args[0];
if (_arg == "server" || _arg == "--s"){
AppServer _server = new AppServer();
await _server.RunServer();
}
else if (_arg == "client" || _arg == "--c"){
AppClient _client = new AppClient();
await _client.RunClient();
}
else{
Console.WriteLine(invalidMessage);
return;
}
}
}

@ -0,0 +1,45 @@
using System.Text;
using System.Net;
using System.Net.Sockets;
using backDot.Commom;
namespace backDot.TheDot{
public class AppClient{
private int port = Constants.Port;
private IPHostEntry? ipHost;
private IPAddress? ipAddress;
private IPEndPoint? ipEndPoint;
public async Task RunClient(){
this.ipHost = await Dns.GetHostEntryAsync(Constants.GetServerHost());
this.ipAddress = this.ipHost.AddressList[0];
this.ipEndPoint = new IPEndPoint(this.ipAddress, this.port);
using Socket client = new Socket(
this.ipEndPoint.AddressFamily,
SocketType.Stream,
ProtocolType.Tcp
);
await client.ConnectAsync(this.ipEndPoint);
while (true){
var message = $"Test message{Constants.EndOfMessage}";
var messageBytes = Encoding.UTF8.GetBytes(message);
await client.SendAsync(messageBytes, SocketFlags.None);
Console.WriteLine($"Send message: {message}");
var buffer = new byte[1_024];
var bytesReceived = await client.ReceiveAsync(buffer, SocketFlags.None);
var received = Encoding.UTF8.GetString(buffer, 0, bytesReceived);
if (received == "<|ACK|>"){
Console.WriteLine($"Reveived: {received}");
break;
}
}
client.Shutdown(SocketShutdown.Both);
}
}
}

@ -0,0 +1,55 @@
using System.Text;
using System.Net;
using System.Net.Sockets;
using backDot.Commom;
namespace backDot.TheDot{
public class AppServer{
private int port = Constants.Port;
private IPHostEntry? ipHost;
private IPAddress? ipAddress;
private IPEndPoint? ipEndPoint;
public async Task RunServer(){
this.ipHost = await Dns.GetHostEntryAsync("127.0.0.1");
this.ipAddress = this.ipHost.AddressList[0];
this.ipEndPoint = new IPEndPoint(this.ipAddress, port);
using Socket listener = new Socket(
this.ipEndPoint.AddressFamily,
SocketType.Stream,
ProtocolType.Tcp
);
listener.Bind(this.ipEndPoint);
Console.WriteLine(@$"
Server up.
Listen on {this.ipAddress}:{this.port}
");
listener.Listen(100);
var handler = await listener.AcceptAsync();
while (true){
var buffer = new byte[1_024];
var byteContent = await handler.ReceiveAsync(buffer, SocketFlags.None);
var content = Encoding.UTF8.GetString(buffer, 0, byteContent);
if (content.IndexOf(Constants.EndOfMessage) > -1){
Console.WriteLine(@$"
Receive Message: {content.Replace(Constants.EndOfMessage, "")}
");
var messageToClient = "<|ACK|>";
var byteMessage = Encoding.UTF8.GetBytes(messageToClient);
await handler.SendAsync(byteMessage, 0);
Console.WriteLine($"Send: {messageToClient}");
// TODO: Remover isso e trocar a lógica para enviar comandos para o cliente.
break;
}
}
}
}
}

@ -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>

@ -0,0 +1,23 @@
{
"runtimeTarget": {
"name": ".NETCoreApp,Version=v6.0",
"signature": ""
},
"compilationOptions": {},
"targets": {
".NETCoreApp,Version=v6.0": {
"backDot/1.0.0": {
"runtime": {
"backDot.dll": {}
}
}
}
},
"libraries": {
"backDot/1.0.0": {
"type": "project",
"serviceable": false,
"sha512": ""
}
}
}

Binary file not shown.

Binary file not shown.

@ -0,0 +1,9 @@
{
"runtimeOptions": {
"tfm": "net6.0",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "6.0.0"
}
}
}

Binary file not shown.
Loading…
Cancel
Save