-2

バイトをブール値に変換したい。これはコードです:

String text = textBox1.Text;
UdpClient udpc = new UdpClient(text,8899);
IPEndPoint ep = null;

while (true)
{
    MessageBox.Show("Name: ");
    string name = "Connected";
    if (name == "") break;

    byte[] sdata = Encoding.ASCII.GetBytes(name);
    udpc.Send(sdata, sdata.Length);

    if (udpc.Receive(ref ep)=null)
    {
      //  MessageBox.Show("Host not found");
    }
    else
    {        
        byte[] rdata = udpc.Receive(ref ep);
        string job = Encoding.ASCII.GetString(rdata);
        MessageBox.Show(job);
    }
}

このコード行をブール値に変換したい:

  udpc.Receive(ref ep);
4

2 に答える 2

3

結果をnullと比較するだけでは不十分です...そうすると、実際のデータが失われ、Receive再度呼び出して、効果的にパケットをスキップします。

次を使用する必要があります。

byte[] data = udpc.Receive(ref ep);
if (data == null)
{
    // Whatever
}
else
{
    MessageBox.Show(Encoding.ASCII.GetBytes(data));
}

また、このコードは壊れていることに注意してください。

string name = "Connected";
if (name == "") break;

設定したばかりのときに、どうしてname空の文字列になる可能性があります"Connected"か?

于 2012-10-02T06:11:37.903 に答える