以下是我使用的代码:
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);
}
}
问题出在ConnectCompleted()
中的Do ... While循环中目前的输出是: 注意事项: *查找您的主机名 注意AUTH: 检查Ident 注意事项: 找到您的主机名 注意事项:* 无身份回复
答案 0 :(得分:0)
当远程端关闭连接(或关闭发送)时,流结束。我认为这不是IRC协议的情况。
无法确定当前是否没有可用数据或远程端是否没有发送更多数据。我想一个新的IRC消息可以随时到达。
因此,您必须根据数据停止阅读。也许你可以检测到一些命令让你继续前进。