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()
The output so far is: NOTICE AUTH : * Looking up your hostname NOTICE AUTH : Checking Ident NOTICE AUTH : Found your hostname NOTICE AUTH :* No ident response
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.
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.
Therefore you have to stop reading based on data. Maybe there is some command you can detect that tells you to move on.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.