ReceiveTimeout on UDP Socket

  • Thread starter Thread starter Mark G. Zeringue
  • Start date Start date
M

Mark G. Zeringue

We are new to dotnet and C#. One of the functions we need to do is port
over an existing Delphi application to C#, this application uses a protocol
based on UDP. Basically, a packet is sent and the sender waits for an ACK,
if the acks does not arrive in the time out period, the sender resends.

In trying to implement this with dotnet using sockets we can format and send
the data and get the acks just fine, the problem arises when we get a
receive timeout. The first time it seems to work, when we retry our packet,
it is like the socket remembers the previous timeout and immediately
returns, does not wait. I can find nothing to explain this behavior. I
tried a test with UDPClient and then did the same test with just a Socket
and the same results.

Can some explain whats going on here? Of course if I stop the program and
restart it, it always works the first time.

some of our test code which is called inside a button click:

s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp );
s.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReceiveTimeout,4
000) ;
s.Connect(UDPEndPoint);
byte [] Header = BuildBinaryHeader() ;
s.Send(Header);// Send the data
byte [] rxdata = new byte [255];
try
{
s.Receive(rxdata);
label1.Text = Encoding.ASCII.GetString(rxdata);
}
catch(SocketException e)
{
listBox1.Items.Add("SocketException caught!!!");
listBox1.Items.Add("Source : " + e.Source);
listBox1.Items.Add("Message : " + e.Message);
label1.Text = "error";
}

thanks mark
 
Back
Top