gpt4 book ai didi

c# - centos7中net core 3的tcpclient无法收发数据

转载 作者:行者123 更新时间:2023-12-04 19:41:36 24 4
gpt4 key购买 nike

代码在windows下运行良好,但是发布到CentOS 7没有反应

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");

var client = new TcpClient();

client.Connect("192.168.126.1", 6378);

var ns = client.GetStream();

Queue<byte[]> queue = new Queue<byte[]>();

Task.Run(() =>
{
var data = new Byte[10240];

while (client != null && client.Connected)
{
try
{
if (ns.DataAvailable)
{
var len = ns.Read(data, 0, data.Length);

if (len > 0)
{
queue.Enqueue(data.AsSpan().Slice(0, len).ToArray());
}
}
else
{
Thread.Sleep(1);
}
}
catch (Exception ex)
{
Console.WriteLine("接收数据发生异常:" + ex.Message);
}

}
});

Task.Run(() =>
{
List<byte> datas = new List<byte>();

while (client != null && client.Connected)
{
if (queue != null && queue.Count > 0)
{
if (queue.TryDequeue(out byte[] data))
{
datas.AddRange(data);

while (datas.Count > 2)
{
var dr = datas.AsReadOnly();

var index = dr.IndexOf(13);

if (index >= 0 && dr.IndexOf(10) == index + 1)
{
var len = index + 2;
Console.WriteLine(Encoding.UTF8.GetString(datas.Take(len).ToArray()));
datas.RemoveRange(0, len);
}
else
{
break;
}
}
}
}
else
{
Thread.Sleep(1);
}
}
});


var cmd = $"*2{Environment.NewLine}$4{Environment.NewLine}info{Environment.NewLine}$3{Environment.NewLine}all{Environment.NewLine}";

Console.WriteLine($"cmd:{cmd}");

var data = Encoding.UTF8.GetBytes(cmd);

ns.Write(data, 0, data.Length);

Console.WriteLine($"cmd:sended");


Console.ReadLine();
}
}
}

有谁知道发生了什么?
谢谢

最佳答案

关于c# - centos7中net core 3的tcpclient无法收发数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58991809/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com