You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.8 KiB
C#
55 lines
1.8 KiB
C#
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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |