0

I implemented a server which connects with multiple clients. The server reads a text file and send the first line to the clients and waits 6 seconds and sends the next line and so on. Now I want to send a line only if the button were clicked. How do I do that?

In my button event I put the method in a task because the server has to handle other coming connectivty request from the clients.

Server side:

private void SendFilesButton_Click(object sender, EventArgs e)
    {
        Task SendTask = Task.Factory.StartNew(() => SendFiles());
    } 


    public void SendFiles()
    {
        try
        {
            tcpClient = tcpListener.AcceptTcpClient();
            if (tcpClient.Connected)
            {

                using (StreamReader reader = new StreamReader("C:\\Users\\Chudnofsky\\Desktop\\Projekt\\Neu\\Messwerte.txt"))
                {

                    lock (this)
                    {
                        string line;
                        for (int i = 1; i < 2400; i++)
                        {
                            line = reader.ReadLine() + Environment.NewLine;
                            stream = tcpClient.GetStream();
                            byte[] toSend = Encoding.ASCII.GetBytes(line);
                            stream.Write(toSend, 0, toSend.Length);
                            stream.Flush();
                            i++;
                            Thread.Sleep(6000);
                        }

                    }

                }

            }

        }
        catch (Exception)
        {

            System.Windows.Forms.MessageBox.Show("Datei konnte nicht gelesen werden!");
        }
    }

1 Answers1

2

There are 2 simple ways to do this.


If the Messwerte.txt file doesn't change between requests store its contents in a member variable using the File.ReadAllLines method:
private string[] lines = File.ReadAllLines("C:\\Messwerte.txt");
private int nextLine = 0;

Then change this:

line = reader.ReadLine() + Environment.NewLine;

To this:

line = lines[nextLine] + Environment.NewLine;
nextLine++;


Alternatively you dont have to read all lines at once and if the file is growing using the File.ReadLines() method is better suited:
int lineCount = 0;
foreach (var lineInFile in File.ReadLines("C:\\Messwerte.txt"))
{
   if (lineCount == nextLine) {
       line = lineInFile;
       nextLine++;
       break;
   }
   lineCount++;
}

As pointed out by @Slai, here is the ideal way to implement this second method:

line = File.ReadLines("C:\\Messwerte.txt").ElementAtOrDefault(nextLine++);
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • 3
    or combine both methods `line = File.ReadLines("C:\\Messwerte.txt").ElementAtOrDefauld(nextLine++);` – Slai Aug 02 '16 at 02:51
  • Scanning file for n-th line every time is somewhat expensive... Probably fine for OP's case where file likely to be small. For nice code one would write some sort asynchronous iterator. – Alexei Levenkov Aug 02 '16 at 03:33
  • hi @VladimirChudnofsky, if you have any more questions please feel free to let me know, any alterations to your question please [edit] it. Cheers – Jeremy Thompson Aug 02 '16 at 08:33
  • Thanks @JeremyThompson but its not working it reads the first line and stops. It should read the line everytime I click the button and sends it to the client and by clicking the button again it should read the next line. How do I do that? – Vladimir Chudnofsky Aug 05 '16 at 08:14
  • It's most likely because the nextline variable is being used on different threads, maybe you might pass that variable to the SendFiles() method or use a sync lock or one of those type of strategies. – Jeremy Thompson Aug 05 '16 at 08:17
  • @JeremyThompson I put it in a lock statement but the server only sends the first line.I want the Server to send the first line and after a button click it sends the second line and so on. How do I do this? – Vladimir Chudnofsky Aug 10 '16 at 11:04
  • Remove the Thread.Sleep(6000); and don't do it in a Task. Get the basics working before you complicate things using a Multi-Threaded approach. – Jeremy Thompson Aug 10 '16 at 11:10