gpt4 book ai didi

C# TCP 服务器-客户端 : can't receive data from the client in an infinite loop

转载 作者:可可西里 更新时间:2023-11-01 02:54:27 25 4
gpt4 key购买 nike

遇到以下问题。从文本文件中读取数据。每 40 毫秒向服务器发送一次数据。服务器必须将数据读入单独流中的无限循环。但这并没有发生。怎么了?

客户:

class Client
{
private TcpClient _client;
private Stream _stream;
private Boolean _isConnected;
private double[] _values;


public Client(String ipAddress, int portNum)
{
_client = new TcpClient();
_client.Connect(ipAddress, portNum);
}


public void SendValues(double[] values)
{
_values = values;
HandleCommunication();
}


public void HandleCommunication()
{
_isConnected = true;
_stream = _client.GetStream();

var dataBytes = new List<byte>();

foreach (double value in _values)
{
dataBytes.AddRange(BitConverter.GetBytes(value));

}

_client.SendBufferSize = _values.Length * sizeof(double);
_stream.Write(dataBytes.ToArray(), 0, _client.SendBufferSize);
Console.Write("Sending...");
_stream.Flush();
}

发送数据:

 public partial class MainWindow : Window
{
private double[] values;
private System.Timers.Timer timer = new System.Timers.Timer(40);
Client _client = new Client("127.0.0.1", 55443);


public MainWindow()
{
InitializeComponent();
OpenSrcFile();

timer.Elapsed += TimerOnElapsed;
timer.Enabled = true;
}


private void TimerOnElapsed(object sender, ElapsedEventArgs e)
{
Console.WriteLine("is sending...");
_client.SendValues(values);
}


public void OpenSrcFile()
{
List<string> lines = File.ReadAllLines(@"file.txt").ToList();
string r = lines[0];
int sizeX = int.Parse(lines[1]);
lines.RemoveRange(0, 2);
values = GetValues(lines.ToArray());
}


private double[] GetValues(string[] lines)
{
return lines.Select(line => double.Parse(line.Replace(" ", ""), CultureInfo.InvariantCulture)).ToArray();
}

服务器:

class Server
{
private TcpListener _server;
private Boolean _isRunning;

public Server(int port)
{
_server = new TcpListener(IPAddress.Parse("127.0.0.1"), port);
_server.Start();

_isRunning = true;
}

public double[] GetData()
{

// wait for client connection

TcpClient client = _server.AcceptTcpClient();

Console.WriteLine("Connected!");

var stream = client.GetStream();

byte[] buffer = new byte[256*sizeof (double)];


stream.Read(buffer, 0, buffer.Length);

double[] newValues = new double[buffer.Length/sizeof (double)];



for (int i = 0; i < newValues.Length; i++)
{
newValues[i] = BitConverter.ToDouble(buffer, i*sizeof (double));
}

stream.Flush();

return newValues;


}


}

接收数据,但由于某些原因不起作用

public partial class MainWindow : Window
{
private Thread _thread;
private double[] _values;
private Server _server = new Server(55443);

public MainWindow()
{
InitializeComponent();
InitializeThread();
}


private void InitializeThread()
{
_thread = new Thread(GetData);
_thread.IsBackground = true;
_thread.Start();
}


private void GetData()
{
while (true)
{
_values = _server.GetData();
Console.WriteLine("...");
}
}

}

好的,客户端发送一个字节数组,这部分工作正常。然后我们需要在单独的线程中将这个字节数组放入无限循环中。就像在这个地方:

private void InitializeThread()
{
_thread = new Thread(GetData);
_thread.IsBackground = true;
_thread.Start();
}


private void GetData()
{
while (true)
{
_values = _server.GetData();
Console.WriteLine("...");
}
}

但是不知何故,服务器只接收到一个字节数组,接下来什么也没有发生。您可以在代码中找到函数 GetData

最佳答案

你把循环放错地方了!

您的客户端仅连接一次;哪个是对的。但是,在您的 GetData 函数中您接受了一个新连接。因此,在第一次读取之后,循环再次调用 GetData,这会在等待连接时阻塞,但什么也没有发生。

相反,您想在服务器类本身中循环调用 Accept,当您获得连接时,为它启动一个“Read”线程(当您获得数据时引发一个事件) .在处理服务器操作的主应用程序中不应有循环。

关于C# TCP 服务器-客户端 : can't receive data from the client in an infinite loop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26720229/

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