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