简体   繁体   English

使用networkstream和streamreader从网络读取流

[英]reading stream from network using networkstream and streamreader

  • I'm trying to connect to IRC server using a C# code using async methods of NetworkStream and StreamReader. 我正在尝试使用NetworkStream和StreamReader的异步方法使用C#代码连接到IRC服务器。
    • It seems like the connection phase is completed successfully, but when I'm trying to read data it gets stuck. 似乎连接阶段已成功完成,但是当我尝试读取数据时,它被卡住了。
    • I'm reading line by line until reader.EndOfStream is reached (also tried reader.Peek() != -1), but still the loop is not ending... 我正在逐行阅读,直到达到reader.EndOfStream(也尝试过reader.Peek()!= -1),但循环仍未结束...

Here's the code I'm using: 这是我正在使用的代码:

public class MainViewModel : ViewModelBase
{
    public string HostAddress { get; set; }
    public int Port { get; set; }

    public MainViewModel()
    {
        HostAddress = "amsterdam.nl.eu.undernet.org";
        Port = 6667;

        Connect(HostAddress);
    }

    private void Connect(string hostAddress)
    {
        var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        socket.BeginConnect(hostAddress, Port, ConnectCompleted, socket);
    }

    private async void ConnectCompleted(IAsyncResult ar)
    {
        var socket = (Socket)ar.AsyncState;
        socket.EndConnect(ar);

        var networkStream = new NetworkStream(socket);
        var reader = new StreamReader(networkStream, Encoding.UTF8);


        do
        {
            var line = await reader.ReadLineAsync();

            Console.WriteLine(line);

        } while (!reader.EndOfStream);

        // Not getting here

        await WriteAsync("NICK mickey", networkStream);
        await WriteAsync("USER mickey 8 * : realname", networkStream);

        do
        {
            var line = await reader.ReadLineAsync();

            Console.WriteLine(line);

        } while (!reader.EndOfStream);


        reader.Close();
        networkStream.Close();
        socket.Close();
    }



    private async Task WriteAsync(string message, NetworkStream stream)
    {
        var command = Encoding.UTF8.GetBytes(string.Format("{0}\r\n", message));

        Console.WriteLine("[S] {0}", message);

        await stream.WriteAsync(command, 0, command.Length);

        await stream.FlushAsync();

        Thread.Sleep(1000);
    }
}

The problem is in the Do...While loop in ConnectCompleted() 问题出在ConnectCompleted()中的Do ... While循环中

The output so far is: NOTICE AUTH : * Looking up your hostname NOTICE AUTH : 到目前为止的输出是:NOTICE AUTH: *查找您的主机名NOTICE AUTH: Checking Ident NOTICE AUTH : 检查身份通知授权: Found your hostname NOTICE AUTH :* No ident response 找到了您的主机名NOTICE AUTH:*无身份响应

  • but then stuck... Please help me to understand when stream is over. 但是卡住了...请帮助我了解视频流何时结束。

The stream ends when the remote side closes the connection (or shuts down sending). 当远程端关闭连接(或关闭发送)时,流结束。 I assume this is not the case with the IRC protocol. 我认为IRC协议不是这种情况。

There is no way to find out whether there currently is no data available or whether the remote side is not sending any more data. 无法找到当前是否没有可用数据或远端是否正在发送更多数据。 I guess a new IRC message could arrive at any time. 我想可能会随时收到新的IRC消息。

Therefore you have to stop reading based on data. 因此,您必须停止基于数据的读取。 Maybe there is some command you can detect that tells you to move on. 也许有一些命令可以检测到,告诉您继续前进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM