Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / September 2007

Tip: Looking for answers? Try searching our database.

Intermittent socket exception

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
QDL - 24 Sep 2007 14:19 GMT
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
Kevin Spencer - 25 Sep 2007 11:03 GMT
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.

Signature

HTH,

Kevin Spencer
Microsoft MVP

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

> Hello everyone,
>
[quoted text clipped - 19 lines]
> TIA
> Paolo
QDL - 25 Sep 2007 13:07 GMT
[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

> Hi Paolo,
>
[quoted text clipped - 28 lines]
>> TIA
>> Paolo
Kevin Spencer - 26 Sep 2007 13:44 GMT
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.

Signature

HTH,

Kevin Spencer
Microsoft MVP

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

> [CUT]
>
[quoted text clipped - 86 lines]
>>> TIA
>>> Paolo
QDL - 27 Sep 2007 08:00 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.