Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>UdpClient</code> is naturally blocking until bytes are received.</p> <p>This means that you shouldn't get data at all, assuming that you're looking for a way to indicate if you have received data, then once you move past the <code>udpc.Recieve</code>, you should return true.</p> <p>I would also consider changing the code a bit as you will have some compilation issues with the <code>= null</code> statement as this does not translate into a compilable code expression.</p> <p>There is also a problem with your if else statement as you're attempting to read from the UDP client which would consume the sent data.</p> <p>Personally I would opt for a UDP socket, but in order to get you rolling I would change the code to something like this:</p> <pre><code>String text = textBox1.Text; UdpClient udpc = new UdpClient(text,8899); IPEndPoint ep = null; while (true) { MessageBox.Show("Name: "); string name = "Connected"; if (name == "") break; //This will never happen because you have already set the value byte[] sdata = Encoding.ASCII.GetBytes(name); int dataSent = 0; try { dataSent = udpc.Send(sdata, sdata.Length); } catch(Exception e) { dataSent = 0; //There is an exception. Probably the host is wrong } if (dataSent &gt; 0) { try { byte[] rdata = udpc.Receive(ref ep); if(rdata!=null &amp;&amp; rdata.Length &gt; 0) { string job = Encoding.ASCII.GetString(rdata); MessageBox.Show(job) //True here as we managed to recieve without going to the catch statement //and we actually have data in the byte[] } else { MessageBox.Show("We did not recieve any data"); //False here, because the array was empty. } } catch(Exception udpe) { //False here as we had a socket exception or timed out MessageBox.Show(udpe.ToString()); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload