Загрузка...

How can I make the wait for a signal from the server last forever? c#

Thread in C# created by gxdlxsssss Apr 18, 2024. 171 view

  1. gxdlxsssss
    gxdlxsssss Topic starter Apr 18, 2024 1 Apr 15, 2024
    CSHARP
    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;
    }
    }
    Как сделать так, чтобы ожидание сигнала с сервера длилось бесконечно?
     
  2. skyenot
    skyenot Apr 18, 2024 7555 Jun 6, 2019
    Никак. Рано или поздно сервер должен закрыть соединение (даже если миллион секунд, все равно спустя миллион секунд он закроет его).
    Используй поллинг/лонг поллинг или вебсокеты.

    Ну либо ебись с вот этим всем (че я должен за тебя гуглить?):
    [IMG]
     
Loading...
Top