Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
tcpclient套接字上接收到的字符串存在C#问题_C#_String_Sockets_Tcpclient - Fatal编程技术网

tcpclient套接字上接收到的字符串存在C#问题

tcpclient套接字上接收到的字符串存在C#问题,c#,string,sockets,tcpclient,C#,String,Sockets,Tcpclient,我有tcp客户端C#代码。它可以很好地连接到任何端口并获取数据。它也适用于linux/unix服务器。但在一个例子中,当我连接到linux服务器(openwrt)时,我可以接收数据,但不能显示字符串。代码显示该字符串包含我想要的内容,但MessageBox.Show不显示该字符串 有什么问题吗 string ip = "192.168.0.1"; string command ="MT15"; string result = "None"; int i = 0; int bytesRead;

我有tcp客户端C#代码。它可以很好地连接到任何端口并获取数据。它也适用于linux/unix服务器。但在一个例子中,当我连接到linux服务器(openwrt)时,我可以接收数据,但不能显示字符串。代码显示该字符串包含我想要的内容,但
MessageBox.Show
不显示该字符串

有什么问题吗

string ip = "192.168.0.1";
string command ="MT15";
string result = "None";

int i = 0;
int bytesRead;

byte[] rec_message = new byte[65535];

StringBuilder Whole_Message = new StringBuilder();
int port = 8889;

var client = new TcpClient();

NetworkStream ns;
int total = 0;

if (!client.ConnectAsync(ip, port).Wait(1000)) 
{ 
     result = "Failed"; 
}

byte[] byteTime = Encoding.ASCII.GetBytes(command);
ns = client.GetStream();
ns.Write(byteTime, 0, byteTime.Length);

// the string.contain Shows the string contains the mac address
while (!result.Contains("C2:04:28:00:5F:F1"))
{
    bytesRead = ns.Read(rec_message, 0, rec_message.Length);
    total = total + bytesRead;

    result = Encoding.ASCII.GetString(rec_message, 0, total);
    Whole_Message.AppendFormat("{0}", result);

    //row = Whole_Message.ToString().Split(',');
}

string r = Whole_Message.ToString();
// the string.contain returns true, so the string contains the mac address

if (r.Contains("C2:04:28:00:5F:F1"))
{
    MessageBox.Show(r);
}

client.Close();
但消息框仅显示“MT15”

tcpdump数据包嗅探结果:

18:06:21.430073 IP 192.168.0.2.6481 > 192.168.0.1.8889: tcp 4
E..,HX....cg%...%....Q".....5#v.P.@t....MT15..

18:06:21.439163 IP 192.168.0.1.8889 > 192.168.0.2.6481: tcp 0
E..(*.@.=..,%...%..."..Q5#v.....P....|..

18:06:21.475525 IP 192.168.0.1.8889 > 192.168.0.2.6481: tcp 23
E..?*.@.=...%...%..."..Q5#v.....P.......MT15..C2:04:28:00:5F:F1
消息为“MT15..C2:04:28:00:5F:F1”,但消息框仅显示“MT15” 这不是messagebox的问题,我无法存储和读取datatable或debug.write。

听起来您的数据中有一些空字符,一些API(特别是:核心Windows API,如
messagebox
)将其视为C风格的终止字符串。令人困惑的是,并非所有API都能做到这一点——特别是在托管代码中,在托管代码中,字符串不应以null结尾

例如:

MessageBox.Show(“hello\0world”);
仅在消息框中显示
hello

当看到空字符时,可能需要问一个基本问题,即该数据是否真的是文本数据;如果你对此感到高兴,那么你可能只需要剥掉它们:

string s = ... // might contain null characters
s = s.Replace("\0",""); // now it doesn't

我没有访问服务器代码的权限,它是二进制的。我首先想到:
Encoding.ASCII.GetString(rec\u message,0,total)
看起来非常错误-应该是
bytesRead
吗?和
Whole_Message.AppendFormat(“{0}”,result)?只是
追加(结果)
?总计是字节读取量。改变它没有什么区别。它可以处理来自服务器的其他消息。整个信息也没有什么区别。字符串包含mac地址,但无法显示。嗯,是的,它确实有很大的区别;它可能不是您最直接的bug,但是的:这是绝对的,100%毫无疑问:非常错误在十六进制中,您在这里收到的有效负载是什么?你有没有可能得到一个零字节?许多字符串处理API在遇到零字符(
\0
)时停止,因为它被认为是C样式的以null结尾的字符串