C# tcpclient 不写入套接字...或者我没有阅读所有响应?
C# tcpclient not writing to socket...or maybe I'm not reading all of the response?
我正在尝试编写一个 C# 控制台应用程序来与 PC Miler telnet server 交互。我设法让它在 powershell 中工作,但我的 C# 代码不工作。
static void Main(string[] args)
{
byte[] ReadBuffer = new byte[1024];
string[] stringSeparators = new string[]{"READY"};
try
{
TcpClient Socket = new TcpClient("myServer", 8320);
if (Socket.Connected) {
NetworkStream netStream = Socket.GetStream();
string PCMSResponse = "";
// Check for the READY prompt
if (netStream.CanRead) {
int byteRead = 0;
while (netStream.DataAvailable) {
byteRead = netStream.Read(ReadBuffer, 0, ReadBuffer.Length);
PCMSResponse += Encoding.ASCII.GetString(ReadBuffer, 0 , byteRead).Trim();
}
} else {
Console.WriteLine("Cannot read from myServer");
}
Thread.Sleep(1000);
// Get New Trip
if (netStream.CanWrite) {
Byte[] PCMSCommandBuffer = Encoding.ASCII.GetBytes("PCMSNewTrip");
netStream.Write(PCMSCommandBuffer, 0 , PCMSCommandBuffer.Length);
Thread.Sleep(1000);
} else {
Console.WriteLine("Cannot write to myServer.");
}
if (netStream.CanRead) {
int byteRead = 0;
while (netStream.DataAvailable) {
byteRead = netStream.Read(ReadBuffer, 0, ReadBuffer.Length);
PCMSResponse += Encoding.ASCII.GetString(ReadBuffer, 0, byteRead).Trim();
}
} else {
Console.WriteLine("Cannot read from myServer");
}
string[] ResponseArray = PCMSResponse.Split(stringSeparators, StringSplitOptions.None);
int c = ResponseArray.Length - 2;
string TripID = ResponseArray[c].Replace(System.Environment.NewLine, "").Replace("[=10=]", "");
Console.WriteLine(PCMSResponse);
}
}
catch (SocketException)
{
Console.WriteLine("Unable to connect to server")
}
}
预期输出
ALK PCMILER SERVER READY
pcmsnewtrip
53
READY
实际产量
ALK PCMILER SERVER READY
似乎我实际上并没有写到服务器,因为如果我向服务器写了一个无效的命令,我会看到这样的错误:
ALK PCMILER SERVER READY
pmctripnew
NO SUCH FUNCTION
READY
我知道我可以写入套接字,因为我没有收到错误消息 Cannot write to myServer.
不确定我做错了什么。 :(
如果您还想查看 powershell 代码,请告诉我。
编辑 - 如果您要编辑此 post,至少要保持预期的输出 AS EXPECTED!更改预期输出会更改 post 的意图。 (EG,如果我想要苹果,但你编辑我的 post 要求橙子,我不会得到正确的答案。)
所以问题是 NetworkStream.Write
方法没有以行结束符结束字符串。结果,当我将我的 PCMSCommandBuffer
字节写入流时,服务器从未处理该命令(因为它等待更多的命令或回车键让它知道你已经完成)。我的解决方案是将流分配给 StreamWriter
。然后我可以使用 WriteLine
方法发送行终止符。
streamWriter = new StreamWriter(netStream);
// Get New Trip
if (netStream.CanWrite) {
Byte[] PCMSCommandBuffer = Encoding.ASCII.GetBytes("PCMSNewTrip");
//netStream.Write(PCMSCommandBuffer, 0 , PCMSCommandBuffer.Length);
streamWriter.WriteLine("PCMSNewTrip");
//netStream.Flush();
streamWriter.Flush();
Console.WriteLine(PCMSCommandBuffer.Length + " PCMSNewTrip");
Thread.Sleep(1000);
} else {
Console.WriteLine("Cannot write to EW-APP1.");
}
我正在尝试编写一个 C# 控制台应用程序来与 PC Miler telnet server 交互。我设法让它在 powershell 中工作,但我的 C# 代码不工作。
static void Main(string[] args)
{
byte[] ReadBuffer = new byte[1024];
string[] stringSeparators = new string[]{"READY"};
try
{
TcpClient Socket = new TcpClient("myServer", 8320);
if (Socket.Connected) {
NetworkStream netStream = Socket.GetStream();
string PCMSResponse = "";
// Check for the READY prompt
if (netStream.CanRead) {
int byteRead = 0;
while (netStream.DataAvailable) {
byteRead = netStream.Read(ReadBuffer, 0, ReadBuffer.Length);
PCMSResponse += Encoding.ASCII.GetString(ReadBuffer, 0 , byteRead).Trim();
}
} else {
Console.WriteLine("Cannot read from myServer");
}
Thread.Sleep(1000);
// Get New Trip
if (netStream.CanWrite) {
Byte[] PCMSCommandBuffer = Encoding.ASCII.GetBytes("PCMSNewTrip");
netStream.Write(PCMSCommandBuffer, 0 , PCMSCommandBuffer.Length);
Thread.Sleep(1000);
} else {
Console.WriteLine("Cannot write to myServer.");
}
if (netStream.CanRead) {
int byteRead = 0;
while (netStream.DataAvailable) {
byteRead = netStream.Read(ReadBuffer, 0, ReadBuffer.Length);
PCMSResponse += Encoding.ASCII.GetString(ReadBuffer, 0, byteRead).Trim();
}
} else {
Console.WriteLine("Cannot read from myServer");
}
string[] ResponseArray = PCMSResponse.Split(stringSeparators, StringSplitOptions.None);
int c = ResponseArray.Length - 2;
string TripID = ResponseArray[c].Replace(System.Environment.NewLine, "").Replace("[=10=]", "");
Console.WriteLine(PCMSResponse);
}
}
catch (SocketException)
{
Console.WriteLine("Unable to connect to server")
}
}
预期输出
ALK PCMILER SERVER READY
pcmsnewtrip
53
READY
实际产量
ALK PCMILER SERVER READY
似乎我实际上并没有写到服务器,因为如果我向服务器写了一个无效的命令,我会看到这样的错误:
ALK PCMILER SERVER READY
pmctripnew
NO SUCH FUNCTION
READY
我知道我可以写入套接字,因为我没有收到错误消息 Cannot write to myServer.
不确定我做错了什么。 :(
如果您还想查看 powershell 代码,请告诉我。
编辑 - 如果您要编辑此 post,至少要保持预期的输出 AS EXPECTED!更改预期输出会更改 post 的意图。 (EG,如果我想要苹果,但你编辑我的 post 要求橙子,我不会得到正确的答案。)
所以问题是 NetworkStream.Write
方法没有以行结束符结束字符串。结果,当我将我的 PCMSCommandBuffer
字节写入流时,服务器从未处理该命令(因为它等待更多的命令或回车键让它知道你已经完成)。我的解决方案是将流分配给 StreamWriter
。然后我可以使用 WriteLine
方法发送行终止符。
streamWriter = new StreamWriter(netStream);
// Get New Trip
if (netStream.CanWrite) {
Byte[] PCMSCommandBuffer = Encoding.ASCII.GetBytes("PCMSNewTrip");
//netStream.Write(PCMSCommandBuffer, 0 , PCMSCommandBuffer.Length);
streamWriter.WriteLine("PCMSNewTrip");
//netStream.Flush();
streamWriter.Flush();
Console.WriteLine(PCMSCommandBuffer.Length + " PCMSNewTrip");
Thread.Sleep(1000);
} else {
Console.WriteLine("Cannot write to EW-APP1.");
}