473,320 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp)

WITH OR WITHOUT THIS IT IS THE SAME!!!!
UdpSender.Bind(New IPEndPoint(IPAddress.Parse("192.168.0.1"), 5000))

With UdpSender
.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.AddMembership, New
MulticastOption(IPAddress.parse("224.5.6.7")))
.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.MulticastTimeToLive, 1)
End With

groupEP = New IPEndPoint(IPAddress.Parse("224.5.6.7"), 5000)

bytes = Encoding.ASCII.GetBytes(message)

UdpSender.Connect(groupEP)
UdpSender.Send(bytes, bytes.Length, SocketFlags.None)

UdpSender.Close()

Jul 21 '05 #1
4 2725
"0k" <0k@0k.com> wrote in message news:<E%*******************@news1.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.SendTo(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.Sockets;
using System.Text;

public class UDPMulticastListener
{

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(GroupAddress,GroupPort);
EndPoint remoteEP = (EndPoint) new IPEndPoint(IPAddress.Any,0);

Socket listener = new Socket(AddressFamily.InterNetwork,SocketType.Dgram ,
ProtocolType.Udp);

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

try
{

listener.Bind(localEP);

listener.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.AddMembership,
new MulticastOption(GroupAddress, localIP));

while (!done)
{
Console.WriteLine("Waiting for Multicast packets.......");
listener.ReceiveFrom(bytes, ref remoteEP);

Console.WriteLine("Received broadcast from {0} :\n {1}\n",
groupEP.ToString(),
Encoding.ASCII.GetString(bytes,0,bytes.Length));
}

listener.Close();
}

catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}

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

news:<E%*******************@news1.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.SendTo(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 "MultiCastInterface", 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(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp)
UdpSender.Bind(New IPEndPoint(LocalAddress, LocalPort))

'--- Add membership to multicast group
UdpSender.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.AddMembership, New MulticastOption(GroupAddress,
LocalAddress))

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

'--- Set Correct NIC for sending multicast packets
UdpSender.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.MulticastInterface, optAddress)

'--- Set TTL
UdpSender.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.MulticastTimeToLive, ttl)

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

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

'--- Send to group
UdpSender.SendTo(bytes, bytes.Length, SocketFlags.None, 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.Address 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%*******************@news1.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(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)

WITH OR WITHOUT THIS IT IS THE SAME!!!!
UdpSender.Bind(New IPEndPoint(IPAddress.Parse("192.168.0.1"), 5000))
With UdpSender
.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.AddMembership, New
MulticastOption(IPAddress.parse("224.5.6.7")))
.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.MulticastTimeToLive, 1)
End With

groupEP = New IPEndPoint(IPAddress.Parse("224.5.6.7"), 5000)

bytes = Encoding.ASCII.GetBytes(message)

UdpSender.Connect(groupEP)
UdpSender.Send(bytes, bytes.Length, SocketFlags.None)

UdpSender.Close()

Jul 21 '05 #5

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

Similar topics

11
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...
7
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...
5
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...
9
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....
3
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...
2
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...
3
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
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...
1
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.