C#TCP Server-Client:无法在无限循环中从客户端接收数据

时间:2014-11-03 18:05:50

标签: c# wpf tcp client server

面临以下问题。从文本文件中读取数据。每隔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

1 个答案:

答案 0 :(得分:0)

你把循环置于错误的位置!

您的客户端只连接一次;哪个是对的。但是,在GetData功能中,您接受新连接。因此,在第一次读取之后,循环再次调用GetData,在等待 new 连接时阻塞,并且没有任何反应。

相反,您希望循环服务器类本身的Accept调用,当您获得连接时,启动&#34; Read&#34;它的线程(当你获得数据时引发一个事件)。主应用程序中不应存在用于处理服务器操作的循环。