我有一个小的 shell 程序,其中客户端是用 C++ 编写的,而服务器是用 C# 编写的。客户端应该只向服务器写一些 cmd 命令的 awnseres。除了一件事之外,一切都运行得很完美:服务器输出的都是垃圾!!
这是 C# 中的服务器:
static NetworkStream Stream;
static byte[] AWNSER = new byte[255];
static void Main(string[] args)
{
TcpListener listen = new TcpListener(IPAddress.Any, 123);
listen.Start();
Stream = listen.AcceptTcpClient().GetStream();
Stream.BeginRead(AWNSER, 0, 255, HNDLR, null);
while (true)
{
var str = Console.ReadLine();
byte[] MESSAGE = Encoding.UTF8.GetBytes(str + "&& cd");
Stream.Write(MESSAGE, 0, MESSAGE.Length);
}
}
static void HNDLR(IAsyncResult a)
{
Stream.EndRead(a);
Stream.BeginRead(AWNSER, 0, 255, HNDLR, null);
Console.WriteLine(Encoding.UTF8.GetString(AWNSER));
}
这是 C++ 中的客户端代码
FILE *fp;
char AWNSER[255];
WSAData wsaData;
WORD DllVersion = MAKEWORD(2, 1);
if (WSAStartup(DllVersion, &wsaData) != 0) //no error
{
cout << "Winsock startup failed";
}
SOCKADDR_IN addr;
int addrlen = sizeof(addr);
addr.sin_port = htons(123);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("199.166.177.22");
SOCKET Connection = socket(AF_INET, SOCK_STREAM, NULL);
if (connect(Connection, (SOCKADDR*)&addr, addrlen) != 0)
{ /*ERROR*/
}
else
{
fp = _popen("dir", "r");
while (fgets(AWNSER, 255, fp) != NULL) {
cout << AWNSER;
send(Connection, AWNSER, 255, NULL);
AWNSER[255];
}
_pclose(fp);
}
牛魔王的故事
相关分类