Intermittent socket exception

Q

QDL

Hello everyone,

i have a windows services (framework 1.1) that uses a socket to send
Multicast UDP Datagrams. On one particular server (HP with Windows 2003
x64), sometimes i get an exception:

SocketException: The requested address is not valid in its context.

I believe the offending lines are this ones:

UdpSender = New Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp)
UdpSender.Bind(New IPEndPoint(LocalAddress, LocalPort))

I know that exception is raised when you use an invalid IP/Port, the problem
is that I checked that LocalAddress and LocalIP are ALWAYS the same, but
sometimes i get the exception. Also we suspect this problem is causing a
memory leak...

Any idea?

TIA
Paolo
 
K

Kevin Spencer

Hi Paolo,

First, unless you want to listen on the EndPoint, you don't need to call
Bind. But assuming that you do, it may be a problem with binding to the Port
you're trying to bind to. Unless you have a reason to, you can use 0 as the
Port parameter, and the service provider will assign an available port
number for you. You can also use System.Net.IPAddress.Any as the IP address,
and the service provider will assign an appropriate address.

--
HTH,

Kevin Spencer
Microsoft MVP

DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
Q

QDL

[CUT]

Hello Kevin and thanx for answering,

I actually use Socket.Bind in order to bind to a specific network card on
the machine, we had problems when in the past where more than a network card
existed on the machine. I did get same samples browsing in MSDN for VS.2003
looking at MulticastOption class.

The problem on that particular x64 machine is that I get the error very
seldom and for a limited amount of time (some minutes) looks like it's a NIC
driver issue (or perhaps x64 issue?), as we have the same service installed
100+ times and behaving well (all on 32bit machines, tho).

Anyway, here is the complete routine I use to send UDP datagrams, maybe u
can have a look at it and give me some clues.

TIA
Paolo

Public Shared Function Send(ByVal LocalAddress As IPAddress, ByVal
LocalPort As Integer, ByVal GroupAddress As IPAddress, ByVal GroupPort As
Integer, ByVal ttl As Integer, ByVal message As String) As Boolean
Dim UdpSender As Socket
Dim groupEP As IPEndPoint
Dim bytes As Byte()

Try
UdpSender = New Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp)
UdpSender.Bind(New IPEndPoint(LocalAddress, LocalPort))

UdpSender.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.AddMembership, New MulticastOption(GroupAddress,
LocalAddress))
UdpSender.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.MulticastInterface, LocalAddress.GetAddressBytes)
UdpSender.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.MulticastTimeToLive, ttl)

groupEP = New IPEndPoint(GroupAddress, GroupPort)

bytes = Encoding.ASCII.GetBytes(message)

UdpSender.SendTo(bytes, bytes.Length, SocketFlags.None, groupEP)

Return True
Catch ex As Exception
Return False
Finally
If (Not UdpSender Is Nothing) Then
UdpSender.Close()
UdpSender = Nothing
End If

groupEP = Nothing
End Try
 
K

Kevin Spencer

Hi Paolo,

It does sound like it may be related to the X64 system or drivers. The
64-bit systems are relatively new, and more likely to have some problems.
One possible solution might be to do a couple of retries, with a pause
between each one, before throwing an exception, as networking issues can
tend to be intermittent and resolve themselves. In any case, from what
you're reporting, it sounds like the case here.

--
HTH,

Kevin Spencer
Microsoft MVP

DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

QDL said:
[CUT]

Hello Kevin and thanx for answering,

I actually use Socket.Bind in order to bind to a specific network card on
the machine, we had problems when in the past where more than a network
card existed on the machine. I did get same samples browsing in MSDN for
VS.2003 looking at MulticastOption class.

The problem on that particular x64 machine is that I get the error very
seldom and for a limited amount of time (some minutes) looks like it's a
NIC driver issue (or perhaps x64 issue?), as we have the same service
installed 100+ times and behaving well (all on 32bit machines, tho).

Anyway, here is the complete routine I use to send UDP datagrams, maybe u
can have a look at it and give me some clues.

TIA
Paolo

Public Shared Function Send(ByVal LocalAddress As IPAddress, ByVal
LocalPort As Integer, ByVal GroupAddress As IPAddress, ByVal GroupPort As
Integer, ByVal ttl As Integer, ByVal message As String) As Boolean
Dim UdpSender As Socket
Dim groupEP As IPEndPoint
Dim bytes As Byte()

Try
UdpSender = New Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp)
UdpSender.Bind(New IPEndPoint(LocalAddress, LocalPort))

UdpSender.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.AddMembership, New MulticastOption(GroupAddress,
LocalAddress))
UdpSender.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.MulticastInterface, LocalAddress.GetAddressBytes)
UdpSender.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.MulticastTimeToLive, ttl)

groupEP = New IPEndPoint(GroupAddress, GroupPort)

bytes = Encoding.ASCII.GetBytes(message)

UdpSender.SendTo(bytes, bytes.Length, SocketFlags.None, groupEP)

Return True
Catch ex As Exception
Return False
Finally
If (Not UdpSender Is Nothing) Then
UdpSender.Close()
UdpSender = Nothing
End If

groupEP = Nothing
End Try



Kevin Spencer said:
Hi Paolo,

First, unless you want to listen on the EndPoint, you don't need to call
Bind. But assuming that you do, it may be a problem with binding to the
Port you're trying to bind to. Unless you have a reason to, you can use 0
as the Port parameter, and the service provider will assign an available
port number for you. You can also use System.Net.IPAddress.Any as the IP
address, and the service provider will assign an appropriate address.

--
HTH,

Kevin Spencer
Microsoft MVP

DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
Q

QDL

Hello Kevin and again thanx for the answer,

[CUT]
One possible solution might be to do a couple of retries, with a pause
between each one, before throwing an exception, as networking issues can
tend to be intermittent and resolve themselves
[CUT]

I did introduce a solution similar to that, I changed the way the sending
thread uses the SendUDP routine, after an error the message is discarded and
will be sent again later, as it is not very important for the whole
application.

Thanx
Paolo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top