我正在测试一个应用程序,我用TcpClient连接到服务器,发送字符串并等待响应。这在步进应用程序时有效,但在没有断点的情况下运行时,结果为空。
具体来说:我想联系TS3服务器。 这是代码:
class Program
{
static void Main(string[] args)
{
var response = new ServerConnection().GetResponse();
Console.WriteLine("Response erhalten!");
Console.WriteLine(response);
Console.ReadKey();
}
}
public class ServerConnection
{
public string GetResponse()
{
string result = "";
using (var client = new TcpClient())
{
client.Connect(IPAddress.Parse("193.111.136.17"), 10011);
using (var ns = client.GetStream())
{
byte[] bytesToSend;
bytesToSend = Encoding.ASCII.GetBytes("help");
ns.Write(bytesToSend, 0, bytesToSend.Length);
byte[] response = new byte[1024];
if (ns.DataAvailable)
ns.Read(response, 0, response.Length);
using (var ms = new MemoryStream(response))
{
using (var sr = new StreamReader(ms))
{
result = sr.ReadToEnd();
}
}
}
}
return result;
}
}
任何人都可以告诉我,为什么在正常运行时\0
充满了result
(导致// Arena RPG.cpp : Defines the entry point for the console application.
// Created by Logan Daniels
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
//Prototypes
void introScreen();
string loadGame();
string newGame(string, string, string);
int exit();
//constant global Variables
const string knight = "Knight";
const string mage = "Mage";
const string assasin = "Assasin";
string name, equipment, characterClass;
string gold;
int main()
{
introScreen();
return 0;
}
void introScreen()
{
int option;
cout << "Welcome to Arena!\n"
<< "Please select an option from the list\n"
<< "1.Load\n2.New Game\n3.Exit\n"
<< "Number option: ";
cin >> option;
if (option == 1)
loadGame();
else if (option == 2)
newGame(knight, mage, assasin);
else if (option == 3)
exit();
}
string newGame(const string knight, const string mage, const string assasin)
{
system("CLS");
string name, equipment;
int gold = 100;
string characterClass; // used in loop
cout << "Welcome to the character creation menu!\n"
<< "Enter your name\nName: ";
cin.ignore();
getline(cin, name);
cout << "\nPick a class.\n1.Knight, 2.Assasin, 3.Mage\n";
cin >> characterClass;
while (characterClass != knight && characterClass != mage && characterClass != assasin)
{
cout << "That is an invalid character class\n";
cout << "\nPick a class.\n1.Knight, 2.Assasin, 3.Mage\n";
cin >> characterClass;
}
system("CLS");
cout << "Welcome to the arena!\nIts time ot begin your adventure " << name << endl << "Hit enter when redy!\n";
ofstream saveFile; //New game file
saveFile.open("Game Save Data.txt");
saveFile << name << endl;
saveFile << characterClass << endl;
saveFile << gold << endl;
saveFile << equipment << endl;
saveFile.close();
system("pause");
return name, characterClass, gold, equipment;
}
string loadGame()
{
string name, characterClass, gold, equipment;
ifstream saveFile;
saveFile.open("Game Save Data");
saveFile >> name;
cout << name << endl;
saveFile >> characterClass;
cout << characterClass << endl;
saveFile >> gold;
cout << gold << endl;
saveFile >> equipment;
cout << equipment << endl;
cout << name << endl << characterClass << endl << gold << endl << equipment << endl;
system("pause");
return name, equipment, characterClass, gold;
}
int exit()
{
return 0;
}
的空白文本),但是在执行此操作时包含一些“有用”的文字?这让我发疯了... aaarg !!
提前致谢!
答案 0 :(得分:0)
NetworkStream.Read
方法阻塞,无需检查NetworkStream.DataAvailable
属性的值。
如果您的代码“执行得太快”,NetworkStream.DataAvailable
属性的值可能为false
,因此不会从流中读取数据。