1
votes

Faced with the following problem. Reads data from a text file. Every 40 milliseconds, the data is sent to the server. Server must read the data into an infinite loop in a separate stream. But this is not happening. What is wrong?

Client:

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();
    }

send data:

 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();
    }

Server:

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;


    }


}

Receive data, but for some reason does not work

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("...");
        }
    }

}

Okay, the client sends a byte array, this part is working correctly. Then we need to get this array of bytes into an infinite loop in a separate thread. Like in this place:

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


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

But somehow, the server receives a byte array once and nothing happens next. function GetData you can find in code

1
First problem - you're ignoring the return value of stream.Read(buffer, 0, buffer.Length); - Jon Skeet
I think you need to add some more Debug.WriteLine statements so that you have much more a handle on what is going on. - Sjips
Change this line: stream.Read(buffer, 0, buffer.Length); into var bytesRead = stream.Read(buffer, 0, buffer.Length); and inspect / debug.writeline the value. Is it what you expect (i.e., does it correspond with the array size that you are sending)? - Sjips

1 Answers

0
votes

You put the loop in the wrong spot!

Your client connects exactly once; which is correct. However, in your GetData function you accept a new connection. Thus, after the first read, the loop calls GetData again, which blocks while waiting for a new connection, and nothing happens.

Instead, you want to loop around the Accept call in the server class itself, and when you get a connection, start a "Read" thread for it (raise an event when you get data). There should be no looping in your main application for handling server operations.