473,856 Members | 1,638 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Socket bind BUG? Please help

0k
Hi everyone, I am trying to write a small app that sends multicast udp
packets using a socket object.

I have more than one NIC on my PC and the following code works OK only if I
disable all the NICs but the one i want to use.

Of course i tried to use Socket.Bind method, but even if i use it to bind to
the correct NIC (I also verify using LocalEndPoint and IT IS the correct
one), the packet is sent on the wrong NIC! (I have a software firewall that
tells me the source NIC). What is really weird is that after executing the
first setsocketoption the program broadcasts to 224.0.0.22, WHY??? The group
is 224.5.6.7!

Can someone help me, i'm going nuts on this problem!

The code i use to send the packet is next, thanx to anyone answering!!!

UdpSender = New Socket(AddressF amily.InterNetw ork, SocketType.Dgra m,
ProtocolType.Ud p)

WITH OR WITHOUT THIS IT IS THE SAME!!!!
UdpSender.Bind( New IPEndPoint(IPAd dress.Parse("19 2.168.0.1"), 5000))

With UdpSender
.SetSocketOptio n(SocketOptionL evel.IP,
SocketOptionNam e.AddMembership , New
MulticastOption (IPAddress.pars e("224.5.6.7")) )
.SetSocketOptio n(SocketOptionL evel.IP,
SocketOptionNam e.MulticastTime ToLive, 1)
End With

groupEP = New IPEndPoint(IPAd dress.Parse("22 4.5.6.7"), 5000)

bytes = Encoding.ASCII. GetBytes(messag e)

UdpSender.Conne ct(groupEP)
UdpSender.Send( bytes, bytes.Length, SocketFlags.Non e)

UdpSender.Close ()

Jul 21 '05 #1
4 2764
"0k" <0k@0k.com> wrote in message news:<E%******* ************@ne ws1.tin.it>...
Hi everyone, I am trying to write a small app that sends multicast udp
packets using a socket object.

I have more than one NIC on my PC and the following code works OK only if I
disable all the NICs but the one i want to use.

Of course i tried to use Socket.Bind method, but even if i use it to bind to
the correct NIC (I also verify using LocalEndPoint and IT IS the correct
one), the packet is sent on the wrong NIC! (I have a software firewall that
tells me the source NIC). What is really weird is that after executing the
first setsocketoption the program broadcasts to 224.0.0.22, WHY??? The group
is 224.5.6.7!

Can someone help me, i'm going nuts on this problem!

The packet sent to 224.0.0.22 is an IGMP V3 packet that is trying
to tell any routers around that you want to join the multicast group.
This is automatically sent by the OS when you set the AddMembership
socket option. If all you need to do is send multicast packets, you do
not need to join the multicast group. That is only used for if you
want to receive multicast packets on the socket.

As far as your problem goes, I do not know why it is not working
properly (assuming you are binding the socket to the correct IP
address for the NIC you want to use). Instead of using the Connect()
method, try using the SendTo() method along with the multicast IP
address: UdpSender.SendT o(bytes, groupEP)

When you talk about the software firewall, is that running on this
PC, or a remote PC? Internal firewall software is well known for
causing network programming problems. You might try disabling it and
testing your program. Alternatively, try loading a packet sniffer
program such as the free WinPcap drivers and the Analyzer program
(http://analyzer.polito.it) and see what is says about the packets.

Hope this gives you some ideas to work with. Good luck.

Rich Blum - Author
"C# Network Programming" (Sybex)
http://www.sybex.com/sybexbooks.nsf/Booklist/4176
"Network Performance Open Source Toolkit" (Wiley)
http://www.wiley.com/WileyCDA/WileyT...471433012.html
Jul 21 '05 #2
When you create your MulticastOption object for joining the group be sure to
use the MulticastOption (groupIP, localIP) constructor. The
MulticastOption (groupIP) constructor won't work with multi-NIC machines. If
you also want to implement a listener, each multicast listener is bound to
one specific NIC on your machine so you will need to create a listener for
each NIC.

-Ron

Here's some C# sample code:

using System;
using System.Net;
using System.Net.Sock ets;
using System.Text;

public class UDPMulticastLis tener
{

private static readonly IPAddress GroupAddress =
IPAddress.Parse ("224.168.100.2 ");
private const int GroupPort = 11000;

private static void StartListener()
{
bool done = false;
byte[] bytes = new Byte[100];
IPEndPoint groupEP = new IPEndPoint(Grou pAddress,GroupP ort);
EndPoint remoteEP = (EndPoint) new IPEndPoint(IPAd dress.Any,0);

Socket listener = new Socket(AddressF amily.InterNetw ork,SocketType. Dgram,
ProtocolType.Ud p);

Console.Write(" Enter the IP Address to bind to : ");
IPAddress localIP = IPAddress.Parse (Console.ReadLi ne());
EndPoint localEP = (EndPoint)new IPEndPoint(loca lIP, GroupPort);

try
{

listener.Bind(l ocalEP);

listener.SetSoc ketOption(Socke tOptionLevel.IP ,
SocketOptionNam e.AddMembership ,
new MulticastOption (GroupAddress, localIP));

while (!done)
{
Console.WriteLi ne("Waiting for Multicast packets......." );
listener.Receiv eFrom(bytes, ref remoteEP);

Console.WriteLi ne("Received broadcast from {0} :\n {1}\n",
groupEP.ToStrin g(),
Encoding.ASCII. GetString(bytes ,0,bytes.Length ));
}

listener.Close( );
}

catch (Exception e)
{
Console.WriteLi ne(e.ToString() );
}
}

public static int Main(String[] args)
{
StartListener() ;
return 0;
}
"Rich Blum" <ri*******@juno .com> wrote in message
news:cc******** *************** ***@posting.goo gle.com...
"0k" <0k@0k.com> wrote in message

news:<E%******* ************@ne ws1.tin.it>...
Hi everyone, I am trying to write a small app that sends multicast udp
packets using a socket object.

I have more than one NIC on my PC and the following code works OK only if I disable all the NICs but the one i want to use.

Of course i tried to use Socket.Bind method, but even if i use it to bind to the correct NIC (I also verify using LocalEndPoint and IT IS the correct
one), the packet is sent on the wrong NIC! (I have a software firewall that tells me the source NIC). What is really weird is that after executing the first setsocketoption the program broadcasts to 224.0.0.22, WHY??? The group is 224.5.6.7!

Can someone help me, i'm going nuts on this problem!

The packet sent to 224.0.0.22 is an IGMP V3 packet that is trying
to tell any routers around that you want to join the multicast group.
This is automatically sent by the OS when you set the AddMembership
socket option. If all you need to do is send multicast packets, you do
not need to join the multicast group. That is only used for if you
want to receive multicast packets on the socket.

As far as your problem goes, I do not know why it is not working
properly (assuming you are binding the socket to the correct IP
address for the NIC you want to use). Instead of using the Connect()
method, try using the SendTo() method along with the multicast IP
address: UdpSender.SendT o(bytes, groupEP)

When you talk about the software firewall, is that running on this
PC, or a remote PC? Internal firewall software is well known for
causing network programming problems. You might try disabling it and
testing your program. Alternatively, try loading a packet sniffer
program such as the free WinPcap drivers and the Analyzer program
(http://analyzer.polito.it) and see what is says about the packets.

Hope this gives you some ideas to work with. Good luck.

Rich Blum - Author
"C# Network Programming" (Sybex)
http://www.sybex.com/sybexbooks.nsf/Booklist/4176
"Network Performance Open Source Toolkit" (Wiley)
http://www.wiley.com/WileyCDA/WileyT...471433012.html

Jul 21 '05 #3
0k
[CUT]

Thank you to both of you answering.

I know, Rich that adding membership to group is only for receiving packets,
what I dunno is that if i dont add membership transmit won't work...

Yes, Ron i knew i should use that MultiCastOption constructor for joining
the multicast group, but that is for receiving.

I have found what the problem is.

For the transmission problem (how to use the correct NIC if PC has more than
one NIC), I discovered the SocketOption called "MultiCastInter face", this
tells the socket what NIC to use for UDP multicast packets, what is weird is
that it doesn't get a IPAddress object as param but it wants a integer (here
is the code i use to setup the socket for multicast send if more than one
NIC is on PC (it works also with one NIC only :) ).

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(), optAddress As Integer

Try
'--- Create socket and bind to local NIC
UdpSender = New Socket(AddressF amily.InterNetw ork, SocketType.Dgra m,
ProtocolType.Ud p)
UdpSender.Bind( New IPEndPoint(Loca lAddress, LocalPort))

'--- Add membership to multicast group
UdpSender.SetSo cketOption(Sock etOptionLevel.I P,
SocketOptionNam e.AddMembership , New MulticastOption (GroupAddress,
LocalAddress))

'--- Get address in integer form (NOTE VS.NET2k3 SAYS .ADDRESS
FUNCTION IS OBSOLETE!)
optAddress = LocalAddress.Ad dress

'--- Set Correct NIC for sending multicast packets
UdpSender.SetSo cketOption(Sock etOptionLevel.I P,
SocketOptionNam e.MulticastInte rface, optAddress)

'--- Set TTL
UdpSender.SetSo cketOption(Sock etOptionLevel.I P,
SocketOptionNam e.MulticastTime ToLive, ttl)

'--- Create EndPoint for multicast group
groupEP = New IPEndPoint(Grou pAddress, GroupPort)

'--- Convert in array o' bytes
bytes = Encoding.ASCII. GetBytes(messag e)

'--- Send to group
UdpSender.SendT o(bytes, bytes.Length, SocketFlags.Non e, groupEP)

'--- Close
UdpSender.Close ()

'--- Help poor old GC
UdpSender = Nothing
groupEP = Nothing

'--- ok
Return True
Catch ex As Exception
Return False
End Try
End Function

As I was saying before, thanx to both of you answering me. By the way I have
a new question: the IPAddress.Addre ss is now obsolete, how can I get the
integer form of an IP Address, now? Tried to cast IPAddress to integer but
it didn't work (Invalid cast exception).

Bye

0k
Jul 21 '05 #4
You need to use the MulticastOption (groupIP, localIP) constructor when doing
multicast on multi NIC machines.

-Ron

"0k" <0k@0k.com> wrote in message news:E%******** ***********@new s1.tin.it...
Hi everyone, I am trying to write a small app that sends multicast udp
packets using a socket object.

I have more than one NIC on my PC and the following code works OK only if I disable all the NICs but the one i want to use.

Of course i tried to use Socket.Bind method, but even if i use it to bind to the correct NIC (I also verify using LocalEndPoint and IT IS the correct
one), the packet is sent on the wrong NIC! (I have a software firewall that tells me the source NIC). What is really weird is that after executing the
first setsocketoption the program broadcasts to 224.0.0.22, WHY??? The group is 224.5.6.7!

Can someone help me, i'm going nuts on this problem!

The code i use to send the packet is next, thanx to anyone answering!!!

UdpSender = New Socket(AddressF amily.InterNetw ork, SocketType.Dgra m, ProtocolType.Ud p)

WITH OR WITHOUT THIS IT IS THE SAME!!!!
UdpSender.Bind( New IPEndPoint(IPAd dress.Parse("19 2.168.0.1"), 5000))
With UdpSender
.SetSocketOptio n(SocketOptionL evel.IP,
SocketOptionNam e.AddMembership , New
MulticastOption (IPAddress.pars e("224.5.6.7")) )
.SetSocketOptio n(SocketOptionL evel.IP,
SocketOptionNam e.MulticastTime ToLive, 1)
End With

groupEP = New IPEndPoint(IPAd dress.Parse("22 4.5.6.7"), 5000)

bytes = Encoding.ASCII. GetBytes(messag e)

UdpSender.Conne ct(groupEP)
UdpSender.Send( bytes, bytes.Length, SocketFlags.Non e)

UdpSender.Close ()

Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
8531
by: anuradha.k.r | last post by:
hi, i am writing a socket program in python,both client side and server side.I've written the client side which is working perfectly fine(checked it against server program written in C).but as for my server program written in python it simply hangs.it does not show any error also.I've tried sample programs available .I don understand what the reason is as i am quite new to it. here is teh server side program: ///////////////////////
7
2390
by: Colin | last post by:
I'm writing a little console socket server but I'm having some difficulty. Can I ask your advice - where is the best place to get some help on that topic? It would be nice if some people who knew what they were doing could take a look at my code and tell me where and why I'm going wrong. Any suggestions of groups or forums?
5
462
by: 0k | last post by:
Hi everyone, I am trying to write a small app that sends multicast udp packets using a socket object. I have more than one NIC on my PC and the following code works OK only if I disable all the NICs but the one i want to use. Of course i tried to use Socket.Bind method, but even if i use it to bind to the correct NIC (I also verify using LocalEndPoint and IT IS the correct one), the packet is sent on the wrong NIC! (I have a software...
9
5567
by: zxo102 | last post by:
Hi everyone, I am using a python socket server to collect data from a socket client and then control a image location ( wxpython) with the data, i.e. moving the image around in the wxpython frame. But the "app.MainLoop()" in wxpython looks like conflicting with the "while 1:" in socket server. After I commented the "app.MainLoop()", everything is working except two things: 1. if I click anywhere on the screen with the mouse, the image is...
3
3431
by: doc | last post by:
What will a flash xml client socket connect to? I have a working php TCP/IP server socket bound to a port >1023 and the flash client will not even connect to it. I can connect to it with non-xml client. Can anyone explain this to me - it has been driving me mad for weeks! Please help! Thanks
2
4129
by: apollo135 | last post by:
Dear All, Could someone help and tell me how to handle multiple send and receive operations with udp sockets? In fact here is my problem: server.c is composing of serveral sub programs (the same for client.c) -----server.c------- ................ int main(){ ............
3
1796
by: Clement | last post by:
Please help me....... I am getting blocked in bind() system call....... i don't know why can you please any one tell me why........ #include<stdio.h> #include<sys/un.h>
5
6882
by: natambu | last post by:
I have a linux box with multiple ip addresses. I want to make my python client connect from one of the ip addresses. Here is my code, no matter what valid information I put in the bind it always comes from the default ip address on the server. Am I doing something wrong? ------------- #!/usr/bin/python import socket
1
7207
by: keksy | last post by:
Hi every1, I am writing a small client/server application and in it I want to send an image asynchronous from the client to the server through a TCP socket. I found an example code on the MSDN site, which is actually for sending strings. I tried to adapt this code so that the client sends an image instead of a string. However, there is something wrong on the server side (i guess)... The server starts listening, the client starts sending...
0
9919
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11061
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10699
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10787
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10390
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9534
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
4578
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4176
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3203
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.