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.
45 lines
1.5 KiB
C#
45 lines
1.5 KiB
C#
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);
|
|
}
|
|
}
|
|
} |