Chandrapal Singh Jhala
Chandrapal Singh Jhala

Reputation: 245

How to make UdpClient non blocking

I am new in Socket programming and C#. I am trying to create an application in which I am using UdpClient.

But even there is data on socket but sometime the Receive function not receiving any data and goes to blocking state. Just to prevent this from blocking I use a counter. The receive function is on thread.

And in main function I have create a counter

int count = 0;
bool ret = IsDataReceived();
while(!ret && count < 30)
{
  ret = IsDataReceived();
  count++;
}

if(count>= 30)
{
thread.abort();

udpclient.close();
}
else
{
Console.WriteLine("Data Received");
}

I am currenlty doing like this but I want to know that how can I make a UdpClient non blocking.

Thanks in Advance.

Upvotes: 0

Views: 2627

Answers (1)

Frenchy
Frenchy

Reputation: 17037

I dont know how is written your function IsDataReceive, if you are using:

UdpClient.Receive

this operation Blocks until a message returns on the socket

if you dont want to have a blocking operation you could use:

UdpClient.ReceiveAsync

Upvotes: 1

Related Questions