commit 2b664bf80347f3d9da74efbc7c76536c82fcbbda Author: kevin.caires Date: Mon May 15 16:30:59 2023 -0300 Inicia o pograma com server e client de socket TPC. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6764f3a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +obj/* \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..3d50434 --- /dev/null +++ b/.vscode/tasks.json @@ -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" + } + ] +} \ No newline at end of file diff --git a/Commom/Constants.cs b/Commom/Constants.cs new file mode 100644 index 0000000..bc93e8b --- /dev/null +++ b/Commom/Constants.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..67a0c6d --- /dev/null +++ b/Program.cs @@ -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; + } + } +} diff --git a/TheDot/AppClient.cs b/TheDot/AppClient.cs new file mode 100644 index 0000000..e75af00 --- /dev/null +++ b/TheDot/AppClient.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/TheDot/AppServer.cs b/TheDot/AppServer.cs new file mode 100644 index 0000000..eccc8f2 --- /dev/null +++ b/TheDot/AppServer.cs @@ -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; + } + } + } + } +} \ No newline at end of file diff --git a/backDot.csproj b/backDot.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/backDot.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/bin/Debug/net6.0/backDot.deps.json b/bin/Debug/net6.0/backDot.deps.json new file mode 100644 index 0000000..b5bc280 --- /dev/null +++ b/bin/Debug/net6.0/backDot.deps.json @@ -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": "" + } + } +} \ No newline at end of file diff --git a/bin/Debug/net6.0/backDot.dll b/bin/Debug/net6.0/backDot.dll new file mode 100644 index 0000000..ae14cd4 Binary files /dev/null and b/bin/Debug/net6.0/backDot.dll differ diff --git a/bin/Debug/net6.0/backDot.pdb b/bin/Debug/net6.0/backDot.pdb new file mode 100644 index 0000000..62f2a83 Binary files /dev/null and b/bin/Debug/net6.0/backDot.pdb differ diff --git a/bin/Debug/net6.0/backDot.runtimeconfig.json b/bin/Debug/net6.0/backDot.runtimeconfig.json new file mode 100644 index 0000000..4986d16 --- /dev/null +++ b/bin/Debug/net6.0/backDot.runtimeconfig.json @@ -0,0 +1,9 @@ +{ + "runtimeOptions": { + "tfm": "net6.0", + "framework": { + "name": "Microsoft.NETCore.App", + "version": "6.0.0" + } + } +} \ No newline at end of file diff --git a/bin/Debug/net6.0/ref/backDot.dll b/bin/Debug/net6.0/ref/backDot.dll new file mode 100644 index 0000000..616e053 Binary files /dev/null and b/bin/Debug/net6.0/ref/backDot.dll differ