gpt4 book ai didi

c# - tcpClient 和 IRC 客户端问题

转载 作者:行者123 更新时间:2023-11-30 20:48:18 24 4
gpt4 key购买 nike

有人能告诉我为什么下面的代码不起作用吗?

using System;
using System.IO;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace TCPClient {
public partial class Form1 : Form {

private TcpClient client;
private Thread readThread;
private NetworkStream stream;
private Stream dataStream;
private Encoding dataStreamEncoding;
private StreamWriter writer;
private StreamReader reader;

public Form1() {
InitializeComponent();

this.client = new TcpClient();
this.readThread = new Thread(ReadLoop);
this.dataStreamEncoding = Encoding.Default;

Thread.CurrentThread.Name = "MainThread";
this.readThread.Name = "ReadThread";
}

private void btnConnect_Click(object sender, EventArgs e) {
if (this.client != null && !this.client.Connected) {


try {
this.client.Connect("open.ircnet.net", 6666);

if (this.client != null && this.client.Connected) {
Console.WriteLine("Connected");
}

// Set up network I/O objects.
this.stream = this.client.GetStream();
this.writer = new StreamWriter(this.stream, this.dataStreamEncoding);
this.reader = new StreamReader(this.stream, this.dataStreamEncoding);

//HandleClientConnected(state.Item3);
this.readThread.Start();
} catch (Exception ex) {
Console.WriteLine(ex.Message);
}
}
}

private void btnPing_Click(object sender, EventArgs e) {
Console.WriteLine("Ping Sent");
this.writer.Write("PING");
this.writer.Flush();
}

private void btnLogin_Click(object sender, EventArgs e) {
Console.WriteLine("Login Info Sent");
this.writer.Write( "PASS *\r\n" +
"NICK testing3134\r\n" +
"USER guest 8 * :\"John Doe\"");
this.writer.Flush();
}

private void ReadLoop() {
try {
// Read each message from network stream, one per line, until client is disconnected.
while (this.client != null && this.client.Connected) {
var line = this.reader.ReadLine();
if (line == null)
break;

Console.WriteLine(line);
}

if(!this.client.Connected) {
Console.WriteLine("Disconnected");
}
} catch(Exception ex) {
Console.WriteLine(ex.Message);
}
}
}
}

控制台:

Connected
:ircnet.eversible.com 020 * :Please wait while we process your connection to ircnet.eversible.com
Login Info Sent
Ping Sent
The thread 'ReadThread' (0x10bc) has exited with code 0 (0x0).
ERROR :Closing Link: testing3134[unknown@24.255.34.216] (Ping timeout)

使它起作用的变化:

原文:

    private void btnPing_Click(object sender, EventArgs e) {
Console.WriteLine("Ping Sent");
this.writer.Write("PING");
this.writer.Flush();
}

private void btnLogin_Click(object sender, EventArgs e) {
Console.WriteLine("Login Info Sent");
this.writer.Write( "PASS *\r\n" +
"NICK testing3134\r\n" +
"USER guest 8 * :\"John Doe\"");
this.writer.Flush();
}

工作:

    private void btnPing_Click(object sender, EventArgs e) {
Console.WriteLine("Ping Sent");
this.writer.WriteLine("PING\r\n");
this.writer.Flush();
}

private void btnLogin_Click(object sender, EventArgs e) {
Console.WriteLine("Login Info Sent");
this.writer.WriteLine("PASS *\r\n");
this.writer.Flush();
this.writer.WriteLine("NICK testing3134\r\n");
this.writer.Flush();
this.writer.WriteLine("USER guest 8 * :\"John Doe\"\r\n");
this.writer.Flush();
}

我不仅从 Write 切换到 WriteLine,而且就像接受的答案建议的那样,我在所有发送的请求的末尾添加了换行符。

最佳答案

您没有在 PING 或 USER 消息后包含换行符。

来自 RFC 2812 :

IRC messages are always lines of characters terminated with a CR-LF (Carriage Return - Line Feed) pair

所以你应该:

this.writer.Write("PASS *\r\n" + 
"NICK testing3134\r\n" +
"USER guest 8 * :\"John Doe\"\r\n");

和:

this.writer.Write("PING\r\n");

如果我是你,我也会避免使用 Encoding.Default。 RFC 指定不使用特定的字符编码,但它确实期望它是 8 位编码(而不是潜在的多字节编码)。这是指定编码的糟糕方式,但我可能会明确使用 ASCII 或 ISO-8859-1。

关于c# - tcpClient 和 IRC 客户端问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24795581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com