C#tcpclient无法写入套接字...还是我没有读取所有响应?

时间:2019-06-14 21:00:38

标签: c# tcpclient

我正在尝试编写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("\0", "");
            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代码。

编辑-如果您要编辑此帖子,请至少将期望的输出保持为按预期!更改预期的输出会更改帖子的意图。 (例如,如果我要苹果,但您编辑我的帖子问橙子,我不会得到正确的答案。)

1 个答案:

答案 0 :(得分:0)

因此,问题出在NetworkStream.Write方法没有以行终止符结尾的字符串。结果,当我将PCMSCommandBuffer字节写入流时,该服务器从未处理过该命令(因为它等待更多的命令或Enter键来告知您已完成)。我的解决方案是将流分配给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.");
}