[英]Telnet Server: Send and Receive data
I am now trying to make a simple client (using Windows.Forms) which connects to a Telnet server, and is able to send and receive data. 我现在正在尝试制作一个简单的客户端(使用Windows.Forms),该客户端连接到Telnet服务器,并且能够发送和接收数据。
I have managed to connect successfully to the server, but I can't figure out how to send data back to the server. 我已经成功连接到服务器,但是我不知道如何将数据发送回服务器。 (After that I need to figure out how to receive the response data, but I haven't made it that far yet.)
(在那之后,我需要弄清楚如何接收响应数据,但是我还没有走这么远。)
Here's my current code: 这是我当前的代码:
using System;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace MUDClient {
public partial class Form1 : Form {
const string SERVER_IP = "166.78.5.182";
const int PORT_NO = 9999;
NetworkStream nwStream;
TcpClient client;
public Form1() {
InitializeComponent();
}
private void btn_connect_Click(object sender, EventArgs e) {
connectToServer();
}
private void btn_disconnect_Click(object sender, EventArgs e) {
client.Close();
nwStream.Close();
rtb_outputWindow.AppendText("\n\nClient: Disconnected.");
}
private void outputWindowTextChanged(object sender, EventArgs e) {
rtb_outputWindow.SelectionStart = rtb_outputWindow.Text.Length;
rtb_outputWindow.ScrollToCaret();
}
private void connectToServer() {
client = new TcpClient(SERVER_IP, PORT_NO);
//client.ReceiveTimeout = 7000;
nwStream = client.GetStream();
new Thread(() => {
Thread.CurrentThread.IsBackground = true;
while (client.Connected) {
byte[] bytesToRead = new byte[client.ReceiveBufferSize];
int bytesRead = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize);
updateOutputWindow(Encoding.ASCII.GetString(bytesToRead, 0, bytesRead));
}
}).Start();
}
private void updateOutputWindow(string text) {
if (InvokeRequired) {
Invoke(new MethodInvoker(delegate () {
updateOutputWindow(text);
}));
}
else {
rtb_outputWindow.AppendText(text);
}
}
private void userSentInput(object sender, KeyEventArgs e) {
RichTextBox inputField = (RichTextBox)sender;
string userInput = inputField.Text.Trim();
if ((e.KeyData == Keys.Enter) && (sender == rtb_inputField)) {
if (nwStream != null) {
if (!string.IsNullOrWhiteSpace(userInput)) {
byte[] bytesToSend = Encoding.ASCII.GetBytes(userInput);
nwStream.Write(bytesToSend, 0, bytesToSend.Length);
nwStream.Flush();
rtb_inputField.Clear();
rtb_inputField.Update();
}
}
}
}
}
}
My program has two RichTextBoxes (one for user input and one for "server output" aka received data), and two buttons (connect and disconnect). 我的程序有两个RichTextBoxes(一个用于用户输入,一个用于“服务器输出”,又名已接收数据)和两个按钮(连接和断开连接)。
Clicking the Connect Button runs the connectToServer() method which successfully opens a connection between the server and the client. 单击“连接”按钮将运行connectToServer()方法,该方法将成功打开服务器与客户端之间的连接。
To send data I want to type into the RichTextBox (rtb_inputField), and when the user presses Enter the data is sent. 要发送数据,我想输入RichTextBox(rtb_inputField),然后在用户按下Enter键时发送数据。 This is the part that isn't working.
这是无效的部分。 Nothing seems to happen when I press Enter.
当我按Enter时似乎什么也没发生。
EDIT: I updated the code. 编辑:我更新了代码。 I've added a continuous connection loop within a new thread.
我在新线程中添加了一个连续连接循环。 This works because I can see the welcome screen from the server.
这行得通,因为我可以从服务器看到欢迎屏幕。 I still can't send anything to the server, though.
但是,我仍然无法将任何内容发送到服务器。 And if I click the Disconnect button the program crashes with the Exception "An unhandled exception of type 'System.IO.IOException'" in the continuous loop.
并且,如果我单击“断开连接”按钮,则程序将在连续循环中崩溃,并出现异常“ System.IO.IOException类型的未处理的异常”。
Try flushing the stream after the Write: 尝试在Write之后刷新流:
nwStream.Flush()
You'll also need to contentiously read by wrapping the reading in a loop 您还需要通过将阅读结果包装成一个循环来有争议地阅读
while (client.Connected)
{
byte[] bytesToRead = new byte[client.ReceiveBufferSize];
int bytesRead = s.Read(bytesToRead, 0, client.ReceiveBufferSize);
Console.Write(Encoding.ASCII.GetString(bytesToRead, 0, bytesRead));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.