我正在用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 =您可以在其中键入命令的文本框。
答案 0 :(得分:1)
byte [] bufferReaction = Encoding.Default.GetBytes(rec.ToString());
只是将读取的字节数转换为字符串,然后再转换为字节数组
您不需要它(这是错误的),因为从客户端发送的字节已经在buffer
。
您可能想要使用
var fileName = Encoding.Default.GetString(buffer,0,rec);
PS:不要忘记TCP是面向流的协议,从客户端发送100个字节并不意味着你将从服务器读取100个字节。例如,它可以以两个50字节的块来到达。