UdpClient throws Socket Exception on subsequent Send calls

  • Thread starter Thread starter Piotr Bieniek
  • Start date Start date
P

Piotr Bieniek

Hello,

I have a problem with UDP sockets. It concerns UdpClient class as well. It
throws strange exceptions on subsequent Send calls. Exception is
SocketException with native error code 10049. I noticed that it happens when
I call Send for the second time in my program. It seemed strange, so I
compiled and run Chat sample program from MSDN. To my surprise, it worked.
But then I modified it. The only change was that I duplicated sending code,
so the program should send two packets instead of one and the exception was
thrown.

What am I doing wrong?

Here is the source code that ilustrates my problem:

---
UdpClient m_Client=new UdpClient();
IPEndPoint m_RemoteEP=new IPEndPoint(0x2301000A, 12434);
for (;;)
{
string s="abc";
byte [] buffer = new Byte[s.Length + 1];
int len = System.Text.Encoding.ASCII.GetBytes( s.ToCharArray(), 0,
s.Length, buffer, 0);
int ecode = m_Client.Send(buffer, len, m_RemoteEP);
System.Threading.Thread.Sleep(1000);
}
---

Exception is thrown when m_Client.Send is thrown for the second time. IP
Address in m_RemoteEP declaration does not matter. If I first call Connect
(outside the loop) and then use Send without m_RemoteEP, it does not help.

Thanks in advance
 
Hello,
The 10049 SocketException is "Address not available from the local
machine". Your code sample shows you are trying to send the UDP packet
to IP address 10.1.0.35, which is an unroutable network address
(similar to the 192.168. network). Is your test PC also on the 10.
network? If not, there is no way for this packet to get to the
destination. Hope this helps solve your problem.

Thanks a lot, but it's not the case. My computer is in the same subnet and
if I change destination IP address to another (for example 213.180.130.200,
which is routable) it doesn't help either. What's very strange is that the
first packet is sent (my firewall warns me about it), but the exception is
thrown when I call Send method for the second time.

Can you compile my source and reproduce the problem? Maybe it's something
with my .NET Framework version...

Anyway, thanks a lot for your time.

Piotr Bieniek
(e-mail address removed)

Do not type "nospam" in my address, if you want to contact me.
 
Hello,

The problem solved. Of course it had nothing with programming. After turning
off my personal firewall my problems disappeared. :)
 
Back
Top