How can i do to wait for signal forever?

  Kiến thức lập trình

How can i do to wait for signal forever?

IPAddress serverAddress = IPAddress.Parse("192.168.1.66");
const int serverPort = 7777;
string filename = "test.txt";

using var client = new TcpClient(serverAddress.ToString(), serverPort);
using var stream = client.GetStream();

byte[] buf = new byte[65536];
await ReadBytes(sizeof(long));
long remainingLength = IPAddress.NetworkToHostOrder(BitConverter.ToInt64(buf, 0));

using var file = File.Create(filename);
while (remainingLength > 0)
{
    int lengthToRead = (int)Math.Min(remainingLength, buf.Length);
    await ReadBytes(lengthToRead);
    await file.WriteAsync(buf, 0, lengthToRead);
    remainingLength -= lengthToRead;
}

async Task ReadBytes(int howmuch)
{
    int readPos = 0;
    while (readPos < howmuch)
    {
        var actuallyRead = await stream.ReadAsync(buf, readPos, howmuch - readPos);
        if (actuallyRead == 0)
            throw new EndOfStreamException();
        readPos += actuallyRead;
    }
}

I need to infinite wait for signal while waiting for file from server port

Await is waiting for 20 seconds, then it kills programm

New contributor

godless is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Maybe it would help if you removed these lines:

        if (actuallyRead == 0)
            throw new EndOfStreamException();

Also, the loop is limited on the value of howmuch. If you don’t need this value you could also use an infinite loop:

while (true)
{
    ...
}

LEAVE A COMMENT