You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Core
|
|
|
|
|
{
|
|
|
|
|
public class PClient
|
|
|
|
|
{
|
|
|
|
|
private Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
|
|
|
|
private EndPoint endPoint;
|
|
|
|
|
|
|
|
|
|
public PClient(string ip, int port)
|
|
|
|
|
{
|
|
|
|
|
endPoint = new IPEndPoint(IPAddress.Parse(ip), port);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task StartAsync()
|
|
|
|
|
{
|
|
|
|
|
await socket.ConnectAsync(endPoint);
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
string str = Console.ReadLine() ?? string.Empty;
|
|
|
|
|
byte[] dataBuffer = Encoding.UTF8.GetBytes(str);
|
|
|
|
|
byte[] headerBuffer = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)dataBuffer.Length));
|
|
|
|
|
|
|
|
|
|
await socket.SendAsync(headerBuffer, SocketFlags.None);
|
|
|
|
|
await socket.SendAsync(dataBuffer, SocketFlags.None);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|