0

我正在用 c# 编写一个 telnet 客户端和服务器。问题是当我在客户端输入命令时。我的服务器报错:“系统找不到指定的文件

我的服务器在控制台应用程序中,我的客户端在 winform 中。非常感谢。

以下是代码: 服务器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Diagnostics;

namespace TelnetServerVictim
{
    class Program
    {
        static void Main(string[] args)
        {
            IPAddress HOST = IPAddress.Parse("127.0.0.1");
            IPEndPoint serverEP = new IPEndPoint(HOST, 443);
            Socket sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            sck.Bind(serverEP);
            sck.Listen(443);

            try
            {
                Console.WriteLine("Listening for clients...");
                Socket msg = sck.Accept();

                while (true)
                {
                    // Send a welcome greet
                    byte[] buffer = Encoding.Default.GetBytes("Welcome to the server of Kobernicus!!");
                    msg.Send(buffer, 0, buffer.Length, 0);
                    buffer = new byte[255];

                    // Read the sended command
                    int rec = msg.Receive(buffer, 0, buffer.Length, 0);
                    byte[] bufferReaction = Encoding.Default.GetBytes(rec.ToString());

                    // Run the command
                    Process prcsCMD = new Process();
                    prcsCMD.StartInfo.FileName = bufferReaction.ToString();
                    prcsCMD.StartInfo.UseShellExecute = false;
                    prcsCMD.StartInfo.Arguments = string.Empty;
                    prcsCMD.StartInfo.RedirectStandardOutput = true;
                    prcsCMD.Start();

                    string output = prcsCMD.StandardOutput.ReadToEnd();
                    byte[] cmdOutput = Encoding.Default.GetBytes(output);
                    msg.Send(cmdOutput,0,cmdOutput.Length,0);
                    cmdOutput = new byte[255];
                }
                sck.Close();
                msg.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

客户:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;

namespace TelnetClientME
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Socket sck;
        private void btnConnect_Click(object sender, EventArgs e)
        {
            sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 443);
            try
            {
                sck.Connect(endPoint);
                MessageBox.Show("You are connected to the server!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnSendMSG_Click(object sender, EventArgs e)
        {
            byte[] buffer = Encoding.Default.GetBytes(txtCMD.Text);
            sck.Send(buffer, 0, buffer.Length, 0);
            buffer = new byte[255];
            int rec = sck.Receive(buffer, 0, buffer.Length, 0);

            txtOutputCMD.Text = rec.ToString();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtOutputCMD.Clear();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            sck.Close();
            Application.Exit();
        }
    }
}

非常感谢。txtOutputCMD = 命令输出所在的 RichTextBox。txtCMD = 一个文本框,您可以在其中输入命令。

4

1 回答 1

1

byte[] bufferReaction = Encoding.Default.GetBytes(rec.ToString());

只是将读取的字节数转换为字符串,然后转换为字节数组

您不需要它(这是错误的),因为从客户端发送的字节已经在buffer.

您可能想使用

var fileName = Encoding.Default.GetString(buffer,0,rec);

PS:不要忘记 TCP 是面向流的协议,从客户端发送 100 个字节并不意味着您将从服务器读取 100 个字节。例如,它可以以两个 50 字节的块的形式到达。

于 2013-04-01T20:26:35.260 回答