为什么我的 Universal App StreamSocket 附加了 3 个空字符?
Why is my Universal App StreamSocket appending 3 null characters?
我正在为 SocketStream 设置我的通用应用程序,但我的服务器在第一次传输时收到 3 个空字符。它在其他所有内容上附加了 3 个空字符。这是我的代码:
/// <summary>
/// This is the click handler for the 'ConnectSocket' button.
/// </summary>
/// <param name="sender">Object for which the event was generated.</param>
/// <param name="e">Event's parameters.</param>
private async void ConnectSocket_Click(object sender, RoutedEventArgs e)
{
// By default 'HostNameForConnect' is disabled and host name validation is not required. When enabling the
// text box validating the host name is required since it was received from an untrusted source
// (user input). The host name is validated by catching ArgumentExceptions thrown by the HostName
// constructor for invalid input.
try
{
hostName = new HostName(HostNameForConnect.Text);
}
catch (ArgumentException)
{
return;
}
StreamSocket socket = new StreamSocket();
// If necessary, tweak the socket's control options before carrying out the connect operation.
// Refer to the StreamSocketControl class' MSDN documentation for the full list of control options.
socket.Control.KeepAlive = false;
// Save the socket, so subsequent steps can use it.
CoreApplication.Properties.Add("clientSocket", socket);
try
{
await socket.ConnectAsync(hostName, ServiceNameForConnect.Text);
// Mark the socket as connected. Set the value to null, as we care only about the fact that the
// property is set.
CoreApplication.Properties.Add("connected", null);
//Do some basic conversations
DataWriter dw = new DataWriter(socket.OutputStream);
DataReader dr = new DataReader(socket.InputStream);
dr.InputStreamOptions = InputStreamOptions.Partial;
await dr.LoadAsync(250);
Response.Text=("Response: " + dr.ReadString(dr.UnconsumedBufferLength)+Environment.NewLine);
await Task.Delay(TimeSpan.FromSeconds(1));
Response.Text+="Sending USER Guest"+Environment.NewLine;
dw.WriteUInt32(dw.MeasureString("USER Guest"));
dw.WriteString("USER Guest");
await dw.StoreAsync();
await dw.FlushAsync();
await Task.Delay(TimeSpan.FromSeconds(1));
await dr.LoadAsync(250);
Response.Text+="Response: " + dr.ReadString(dr.UnconsumedBufferLength)+Environment.NewLine;
await Task.Delay(TimeSpan.FromSeconds(1));
Response.Text += "Sending PASS " + Environment.NewLine;
dw.WriteUInt32(dw.MeasureString("PASS "));
dw.WriteString("PASS ");
await dw.StoreAsync();
await dw.FlushAsync();
await Task.Delay(TimeSpan.FromSeconds(1));
await dr.LoadAsync(250);
Response.Text += "Response: " + dr.ReadString(dr.UnconsumedBufferLength) + Environment.NewLine;
await Task.Delay(TimeSpan.FromSeconds(1));
Response.Text += "Sending PWD" + Environment.NewLine;
dw.WriteUInt32(dw.MeasureString("PWD"));
dw.WriteString("PWD");
await dw.StoreAsync();
await dw.FlushAsync();
await Task.Delay(TimeSpan.FromSeconds(1));
await dr.LoadAsync(250);
Response.Text += "Response: " + dr.ReadString(dr.UnconsumedBufferLength) + Environment.NewLine;
}
catch (Exception exception)
{
// If this is an unknown status it means that the error is fatal and retry will likely fail.
if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
{
}
}
}
我的服务器收到“[=29=][=29=][=29=]”。我从不发送空白消息或带有空字符的消息。没看懂。
有趣的是,如果我第二次发送 USER 访客:
Response.Text+="Sending USER Guest"+Environment.NewLine;
dw.WriteUInt32(dw.MeasureString("USER Guest"));
dw.WriteString("USER Guest");
await dw.StoreAsync();
await dw.FlushAsync();
await Task.Delay(TimeSpan.FromSeconds(1));
await dr.LoadAsync(250);
Response.Text+="Response: " + dr.ReadString(dr.UnconsumedBufferLength)+Environment.NewLine;
await Task.Delay(TimeSpan.FromSeconds(1));
Response.Text += "Sending USER Guest" + Environment.NewLine;
dw.WriteUInt32(dw.MeasureString("USER Guest"));
dw.WriteString("USER Guest");
await dw.StoreAsync();
await dw.FlushAsync();
await Task.Delay(TimeSpan.FromSeconds(1));
await dr.LoadAsync(250);
Response.Text += "Response: " + dr.ReadString(dr.UnconsumedBufferLength) + Environment.NewLine;
await Task.Delay(TimeSpan.FromSeconds(1));
服务器收到“[=29=][=29=][=29=]”THEN "USER Guest[=27=][=27=][=27=]"。所以有两个问题:
为什么要附加 3 个空字符,为什么第一条消息发送为空?
更新:我知道它不是服务器,因为我将服务器与另一个 c# 控制台应用程序一起使用。比较码为:
Console.WriteLine("Sending Username Command");
sw.WriteLine("USER Guest");
sw.Flush();
System.Threading.Thread.Sleep(1000);
Console.WriteLine("Response: " + sr.ReadLine());
Console.WriteLine("Sending Password Command");
sw.WriteLine("PASS ");
sw.Flush();
System.Threading.Thread.Sleep(1000);
Console.WriteLine("Response: " + sr.ReadLine());
Console.WriteLine("Sending PWD command");
sw.WriteLine("PWD");
sw.Flush();
其中 sw 是 streamwriter。这里服务器收到"USER Guest"
停止这样做:
dw.WriteUInt32(dw.MeasureString("USER Guest"));
dw.WriteString("USER Guest");
然后开始这样做:
dw.WriteString("USER Guest\n");
目前,您正在发送一个长度值,作为一个以 4 个字节发送的 uint,然后发送一个字符串。碰巧的是,您发送的字符串的长度恰好是 10 个字节。因此,您要发送的前 4 个字节是 0、0、0、10。碰巧,字符代码 10 与 \n
相同,因此它读取为完整的文本行 - 与您的相同控制台应用程序(显然有效)通过调用 WriteLine
.
来完成
添加了空字符,因为我使用了
dw.WriteUInt32(dw.MeasureString("USER Guest"));
因为我的文字只需要1个字节,其他3个字节被3个空字符占用了。我把它改成了
dw.WriteByte((byte)dw.MeasureString("USER Guest"));
现在我附加了 0 个空字符。我仍然有问题,因为第一次通信只是一个空字符串,而不是 3 个空字符。
我正在为 SocketStream 设置我的通用应用程序,但我的服务器在第一次传输时收到 3 个空字符。它在其他所有内容上附加了 3 个空字符。这是我的代码:
/// <summary>
/// This is the click handler for the 'ConnectSocket' button.
/// </summary>
/// <param name="sender">Object for which the event was generated.</param>
/// <param name="e">Event's parameters.</param>
private async void ConnectSocket_Click(object sender, RoutedEventArgs e)
{
// By default 'HostNameForConnect' is disabled and host name validation is not required. When enabling the
// text box validating the host name is required since it was received from an untrusted source
// (user input). The host name is validated by catching ArgumentExceptions thrown by the HostName
// constructor for invalid input.
try
{
hostName = new HostName(HostNameForConnect.Text);
}
catch (ArgumentException)
{
return;
}
StreamSocket socket = new StreamSocket();
// If necessary, tweak the socket's control options before carrying out the connect operation.
// Refer to the StreamSocketControl class' MSDN documentation for the full list of control options.
socket.Control.KeepAlive = false;
// Save the socket, so subsequent steps can use it.
CoreApplication.Properties.Add("clientSocket", socket);
try
{
await socket.ConnectAsync(hostName, ServiceNameForConnect.Text);
// Mark the socket as connected. Set the value to null, as we care only about the fact that the
// property is set.
CoreApplication.Properties.Add("connected", null);
//Do some basic conversations
DataWriter dw = new DataWriter(socket.OutputStream);
DataReader dr = new DataReader(socket.InputStream);
dr.InputStreamOptions = InputStreamOptions.Partial;
await dr.LoadAsync(250);
Response.Text=("Response: " + dr.ReadString(dr.UnconsumedBufferLength)+Environment.NewLine);
await Task.Delay(TimeSpan.FromSeconds(1));
Response.Text+="Sending USER Guest"+Environment.NewLine;
dw.WriteUInt32(dw.MeasureString("USER Guest"));
dw.WriteString("USER Guest");
await dw.StoreAsync();
await dw.FlushAsync();
await Task.Delay(TimeSpan.FromSeconds(1));
await dr.LoadAsync(250);
Response.Text+="Response: " + dr.ReadString(dr.UnconsumedBufferLength)+Environment.NewLine;
await Task.Delay(TimeSpan.FromSeconds(1));
Response.Text += "Sending PASS " + Environment.NewLine;
dw.WriteUInt32(dw.MeasureString("PASS "));
dw.WriteString("PASS ");
await dw.StoreAsync();
await dw.FlushAsync();
await Task.Delay(TimeSpan.FromSeconds(1));
await dr.LoadAsync(250);
Response.Text += "Response: " + dr.ReadString(dr.UnconsumedBufferLength) + Environment.NewLine;
await Task.Delay(TimeSpan.FromSeconds(1));
Response.Text += "Sending PWD" + Environment.NewLine;
dw.WriteUInt32(dw.MeasureString("PWD"));
dw.WriteString("PWD");
await dw.StoreAsync();
await dw.FlushAsync();
await Task.Delay(TimeSpan.FromSeconds(1));
await dr.LoadAsync(250);
Response.Text += "Response: " + dr.ReadString(dr.UnconsumedBufferLength) + Environment.NewLine;
}
catch (Exception exception)
{
// If this is an unknown status it means that the error is fatal and retry will likely fail.
if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
{
}
}
}
我的服务器收到“[=29=][=29=][=29=]”。我从不发送空白消息或带有空字符的消息。没看懂。
有趣的是,如果我第二次发送 USER 访客:
Response.Text+="Sending USER Guest"+Environment.NewLine;
dw.WriteUInt32(dw.MeasureString("USER Guest"));
dw.WriteString("USER Guest");
await dw.StoreAsync();
await dw.FlushAsync();
await Task.Delay(TimeSpan.FromSeconds(1));
await dr.LoadAsync(250);
Response.Text+="Response: " + dr.ReadString(dr.UnconsumedBufferLength)+Environment.NewLine;
await Task.Delay(TimeSpan.FromSeconds(1));
Response.Text += "Sending USER Guest" + Environment.NewLine;
dw.WriteUInt32(dw.MeasureString("USER Guest"));
dw.WriteString("USER Guest");
await dw.StoreAsync();
await dw.FlushAsync();
await Task.Delay(TimeSpan.FromSeconds(1));
await dr.LoadAsync(250);
Response.Text += "Response: " + dr.ReadString(dr.UnconsumedBufferLength) + Environment.NewLine;
await Task.Delay(TimeSpan.FromSeconds(1));
服务器收到“[=29=][=29=][=29=]”THEN "USER Guest[=27=][=27=][=27=]"。所以有两个问题:
为什么要附加 3 个空字符,为什么第一条消息发送为空?
更新:我知道它不是服务器,因为我将服务器与另一个 c# 控制台应用程序一起使用。比较码为:
Console.WriteLine("Sending Username Command");
sw.WriteLine("USER Guest");
sw.Flush();
System.Threading.Thread.Sleep(1000);
Console.WriteLine("Response: " + sr.ReadLine());
Console.WriteLine("Sending Password Command");
sw.WriteLine("PASS ");
sw.Flush();
System.Threading.Thread.Sleep(1000);
Console.WriteLine("Response: " + sr.ReadLine());
Console.WriteLine("Sending PWD command");
sw.WriteLine("PWD");
sw.Flush();
其中 sw 是 streamwriter。这里服务器收到"USER Guest"
停止这样做:
dw.WriteUInt32(dw.MeasureString("USER Guest"));
dw.WriteString("USER Guest");
然后开始这样做:
dw.WriteString("USER Guest\n");
目前,您正在发送一个长度值,作为一个以 4 个字节发送的 uint,然后发送一个字符串。碰巧的是,您发送的字符串的长度恰好是 10 个字节。因此,您要发送的前 4 个字节是 0、0、0、10。碰巧,字符代码 10 与 \n
相同,因此它读取为完整的文本行 - 与您的相同控制台应用程序(显然有效)通过调用 WriteLine
.
添加了空字符,因为我使用了
dw.WriteUInt32(dw.MeasureString("USER Guest"));
因为我的文字只需要1个字节,其他3个字节被3个空字符占用了。我把它改成了
dw.WriteByte((byte)dw.MeasureString("USER Guest"));
现在我附加了 0 个空字符。我仍然有问题,因为第一次通信只是一个空字符串,而不是 3 个空字符。