c#If语句总是返回false

c#If语句总是返回false,c#,linux,if-statement,mono,C#,Linux,If Statement,Mono,我目前正在为一个个人项目开发一个小型服务器/客户端应用程序。当客户端从Windows PC上运行时,服务器将在Mono下的Linux中运行 我有一个问题,我正在向服务器传递数据(在本例中是字符串值“on”和“off”),但是if语句总是返回一个假值 来自服务器的代码如下所示 public void startServer() { TcpListener listen = new TcpListener(serverIP, 10000); listen.Start();

我目前正在为一个个人项目开发一个小型服务器/客户端应用程序。当客户端从Windows PC上运行时,服务器将在Mono下的Linux中运行

我有一个问题,我正在向服务器传递数据(在本例中是字符串值“on”和“off”),但是if语句总是返回一个假值

来自服务器的代码如下所示

public void startServer() {
    TcpListener listen = new TcpListener(serverIP, 10000);
    listen.Start();

    Console.Clear();
    Console.WriteLine("IP Address = {0}", serverIP.ToString());
    Console.WriteLine("Server is listening for connection");

    Socket a = listen.AcceptSocket();

    Console.WriteLine("Connection from {0}", a.RemoteEndPoint);

    ASCIIEncoding respond = new ASCIIEncoding();
    a.Send(respond.GetBytes("You are connected to LED Control Server"));

    bool keepListening = true;

    while (keepListening) {
        byte[] b = new byte[1000];
        a.Receive(b);
        Console.WriteLine("Message Received from {0}, string: {1}", a.RemoteEndPoint, recData(b));
        string serverMsg = procIns(recData(b));
        a.Send(respond.GetBytes(serverMsg));
    }
}

private string recData(byte[] b) //receiving data from the client {
    int count = b.Length;
    string message = "";
    for (int a = 0; a < count; a++) {
        message += Convert.ToChar(b[a]);
    }
    return message;
}

private string procIns(string instruction) {
    string returnMsg;

    Console.WriteLine(instruction.ToLower());

    if (instruction.ToLower() == "on") {
        FileGPIO gpio = new FileGPIO();
        gpio.OutputPin(FileGPIO.enumPIN.gpio17, true);
        returnMsg = "GPIO 17 1";
    } else if (instruction.ToLower() == "off") {
        FileGPIO gpio = new FileGPIO();
        gpio.OutputPin(FileGPIO.enumPIN.gpio17, false);
        returnMsg = "GPIO 17 0";
    } else {
        returnMsg = "Invalid Command";
    }

    return returnMsg;
}
public void startServer(){
TcpListener listen=新的TcpListener(服务器IP,10000);
听。开始();
Console.Clear();
WriteLine(“IP地址={0}”,serverIP.ToString());
WriteLine(“服务器正在侦听连接”);
套接字a=listen.AcceptSocket();
WriteLine(“来自{0}的连接”,a.RemoteEndPoint);
ascienceoding respond=新的ascienceoding();
a、 发送(respond.GetBytes(“您已连接到LED控制服务器”);
bool keepListening=true;
同时(继续听){
字节[]b=新字节[1000];
a、 接收(b);
WriteLine(“从{0}接收的消息,字符串:{1}”,a.RemoteEndPoint,recData(b));
字符串serverMsg=procIns(recData(b));
a、 发送(respond.GetBytes(serverMsg));
}
}
私有字符串recData(字节[]b)//从客户端接收数据{
int count=b.长度;
字符串消息=”;
对于(int a=0;a

procIns方法中if语句返回false的原因我不知道,如果有人能提供任何建议,我将不胜感激

我猜应该是填充空间。试试这个

if (instruction.Trim().ToLower() == "on")

我想应该是填充空间。试试这个

if (instruction.Trim().ToLower() == "on")
a.Receive(b)
方法返回接收的字节数。您可以将该值存储在某个变量中,并将该变量传递给
recData
方法

    private string recData(byte[] b, int bytesRcvd) {
    string message = "";
    for (int a = 0; a < bytesRcvd; a++) {
        message += Convert.ToChar(b[a]);
    }
    return message;
}
private string recData(字节[]b,int bytesRcvd){
字符串消息=”;
对于(int a=0;a
接收的字节数将有助于截断字节数组中的额外值

a.Receive(b)
方法返回接收的字节数。您可以将该值存储在某个变量中,并将该变量传递给
recData
方法

    private string recData(byte[] b, int bytesRcvd) {
    string message = "";
    for (int a = 0; a < bytesRcvd; a++) {
        message += Convert.ToChar(b[a]);
    }
    return message;
}
private string recData(字节[]b,int bytesRcvd){
字符串消息=”;
对于(int a=0;a

收到的字节数将有助于截断字节数组中的额外值。

是否检查了任何填充空格?使用调试器,查看指令的实际值。我向您保证,
if
语句没有被破坏。仅供参考,
recData
是不必要的。您可以使用类似于
System.Text.Encoding.UTF8.GetString(b)的东西相反。
a.接收(b)
不能确保您将获得
b.Length
字节。检查其返回值。
b.Length
应该大约为1000。您在哪里跟踪实际接收的字节数?是否检查了任何填充空格?使用调试器,查看指令的实际值。我向您保证,
if
语句没有被破坏。仅供参考,
recData
是不必要的。您可以使用类似于
System.Text.Encoding.UTF8.GetString(b)的东西相反。
a.接收(b)
不能确保您将获得
b.Length
字节。检查其返回值。
b.Length
应该大约为1000。您在哪里跟踪实际接收的字节数?仍然得到相同的结果,我已经检查了变量值,它有字符串“on”,但随后是加载的/o/o/o/o/o/o/o/o是什么导致的?在您问题的示例中,您没有检查返回数据的大小。你分配了一个1000字节的数组,但是a.Receive(b)
实际上只填充了第一个0。如果仍然得到相同的值,我已经检查了变量值,它有一个字符串“on”,但随后是/o/o/o/o/o/o/o的加载,这是什么原因呢?在你问题的示例中,你没有检查返回数据的大小。您分配了一个1000字节的数组,但是
a.Receive(b)
实际上只填充了前0个字节,您不应该在这里的循环中使用
Convert.ToChar
。选择字符串编码,然后使用
System.Text.encoding
子类之一。还要检查前导/尾随空格。您不应该在这里的循环中使用
Convert.ToChar
。选择字符串编码,然后使用
System.Text.encoding
子类之一。还要检查前导/尾随空格。