我正在研究阅读器程序。它基于Winorms。
我需要一个代码,其中winform将通过TCP(端口3573)发送一些按需数据。 (按要求,我的意思是程序通过TCP接收命令时发出的命令GET。
我是一个新手,所以我很难将所有这些内容结合在一起:线程,TCPRead TCP发送,事件处理程序...
因此,我在这里需要有关完整代码或示例的帮助。
我尝试了一些来自互联网的示例代码,但是没有用(线程,TCPreader和TCPsender,通过TCPreaded处理事件)
在TCP Reader上,我们收到GET,然后发送一些字符串,让TCP Sender说“ hello world”
答案 0 :(得分:0)
套接字确实很难正确使用,而且API只是……令人讨厌。由于那只是在问错误,因此我建议使用“管道” API,该API与现代async
代码 更加一致,并且更容易正确(并且具有在帧处理方面更好的选择)。所以;这是一个管道示例;
请注意,这需要Pipelines.Sockets.Unofficial,它通过以下方式位于nuget上:
<PackageReference Include="Pipelines.Sockets.Unofficial" Version="2.0.22" />
(添加此内容会自动添加您需要的所有其他作品)
using Pipelines.Sockets.Unofficial;
using System;
using System.IO.Pipelines;
using System.Net;
using System.Text;
using System.Threading.Tasks;
static class Program
{
static async Task Main()
{
var endpoint = new IPEndPoint(IPAddress.Loopback, 9042);
Console.WriteLine("[server] Starting server...");
using (var server = new MyServer())
{
server.Listen(endpoint);
Console.WriteLine("[server] Starting client...");
Task reader;
using (var client = await SocketConnection.ConnectAsync(endpoint))
{
reader = Task.Run(() => ShowIncomingDataAsync(client.Input));
await WriteAsync(client.Output, "hello");
await WriteAsync(client.Output, "world");
Console.WriteLine("press [return]");
Console.ReadLine();
}
await reader;
server.Stop();
}
}
private static async Task ShowIncomingDataAsync(PipeReader input)
{
try
{
while (true)
{
var read = await input.ReadAsync();
var buffer = read.Buffer;
if (buffer.IsEmpty && read.IsCompleted) break; // EOF
Console.WriteLine($"[client] Received {buffer.Length} bytes; marking consumed");
foreach (var segment in buffer)
{ // usually only one segment, but can be more complex
Console.WriteLine("[client] " + Program.GetAsciiString(segment.Span));
}
input.AdvanceTo(buffer.End); // "we ate it all"
}
}
catch { }
}
private static async Task WriteAsync(PipeWriter output, string payload)
{
var bytes = Encoding.ASCII.GetBytes(payload);
await output.WriteAsync(bytes);
}
internal static unsafe string GetAsciiString(ReadOnlySpan<byte> span)
{
fixed (byte* b = span)
{
return Encoding.ASCII.GetString(b, span.Length);
}
}
}
class MyServer : SocketServer
{
protected override Task OnClientConnectedAsync(in ClientConnection client)
=> RunClient(client);
private async Task RunClient(ClientConnection client)
{
Console.WriteLine($"[server] new client: {client.RemoteEndPoint}");
await ProcessRequests(client.Transport);
Console.WriteLine($"[server] ended client: {client.RemoteEndPoint}");
}
private async Task ProcessRequests(IDuplexPipe transport)
{
try
{
var input = transport.Input;
var output = transport.Output;
while (true)
{
var read = await input.ReadAsync();
var buffer = read.Buffer;
if (buffer.IsEmpty && read.IsCompleted) break; // EOF
Console.WriteLine($"[server] Received {buffer.Length} bytes; returning it, and marking consumed");
foreach (var segment in buffer)
{ // usually only one segment, but can be more complex
Console.WriteLine("[server] " + Program.GetAsciiString(segment.Span));
await output.WriteAsync(segment);
}
input.AdvanceTo(buffer.End); // "we ate it all"
}
}
catch { }
}
}
我可以用原始套接字编写此代码,但是要展示最佳实践和避免问题,将需要更多代码,所有这些丑陋已经隐藏在“管道”中。
输出:
[server] Starting server...
[server] Starting client...
[server] new client: 127.0.0.1:63076
press [return]
[server] Received 5 bytes; returning it, and marking consumed
[server] hello
[server] Received 5 bytes; returning it, and marking consumed
[client] Received 5 bytes; marking consumed
[client] hello
[server] world
[client] Received 5 bytes; marking consumed
[client] world