Currently I have this C# code to open Socket Multicast connection.
IPAddress destAddr = IPAddress.Parse(m_Address);
m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
m_Socket.SetSocketOption(SocketOptionLevel.IP,SocketOptionName.MulticastTimeToLive, m_TTL);
m_EndPoint = new IPEndPoint(destAddr, m_Port);
//Try to fix Network issue
m_Socket.MulticastLoopback = true;
m_Socket.Ttl = 255;
I connect using this code and Send Packet to Reciver,what happen is:
On my device every thing work fine and receiver receive data without any problem.
Using an other device on the same network using ethernet adapter packets go out (i see it in wire-shark) but never received !, same device using WiFi every thing working nice !.
An other device on an other network using WiFi nothing work!
Any advice?
Again if I use Wire-Shark I see packets go out without problem!
I did figure it out
Me as a Sender use WiFi no problem happened, the problem comes up if sender use Cable from different Switch other than the switch receiver plugged on it !
So no thing to do in programming,it's Switch settings .
Please refer to this Cisco Tutorial for problem and solution
Multicast does not work on the same vLan
Related
I am trying to do discovery via UDP, my code sends a multicast message and other devices on the network reply back to me. I am using UdpClient on .NET 4.5.2, binding it on a random port and sending my message to a multicast address that the devices are listening on (e.g. 233.255.255.237:8003). The devices reply to me from the multicast port 8003, but some reply from their own ip (e.g. 10.0.23.66) and some from local broadcast ip (e.g. 10.0.23.255).
This runs just fine on Windows, I can see replies from all devices, but when running on Mono (version 5.2.0.224), I can only see the messages sent from the devices that don't use the local broadcast ip.
When I do tcpdump and open it in Wireshark I can clearly see the UDP messages reaching me, even those with Source = 10.0.23.255 they have the correct destination ip and port(my random port), but the code never picks them up...
I have searched SO and the web and tried literally everything over the past 2 days, different UdpClient constructors, 2 UdpClients on the same port(otherwise no replies are be picked up, receiving code has to listen on the same port as sending code is using) - one for sending and one for receiving, using a plain socket for receiving, setting EnableBroadcast = true, binding to a specific port(multicast port and other), using JoinMulticastGroup(shouldn't matter though, I am only sending messages to multicast address, not receiving the, the replies come directly to my local andpoint), and then some, but nothing works and I'm at the end of my wits...
Maybe there is a mono bug / different behavior, or some arcane setting that can be used, I would appreciate any help.
Here is how my code looks like(omitting parts like cleanup when disposing, etc.):
client = new UdpClient { MulticastLoopback = false, EnableBroadcast = true };
client.Client.Bind(new IPEndPoint(IPAddress.Any, 0));
client.BeginReceive(EndReceive, null);
private void EndReceive(IAsyncResult ar)
{
try
{
var source = new IPEndPoint(IPAddress.Any, 0);
var data = client.EndReceive(ar, ref source);
Console.WriteLine("{0} received msg:\r\n{1}", source.Address, Encoding.UTF8.GetString(data));
}
catch (Exception e)
{
Console.WriteLine(e);
}
client.BeginReceive(EndReceive, null);
}
For sending the multicast message I use client.Send() in try catch as well, the messages are definitely sent and clients are responding as seen on Wireshark, just under Windows I get all the responses written out to Console, under Linux/Mono only the ones that respond from Source=10.0.23.XXX and and the ones from 10.0.23.255 seem to be filtered out (I compared the messages carefully in Wireshark, they are the same whether my code runs on Win or Linux/Mono).
Recently I completed my network sniffer for the company that I am currently working with.
I want to know:
Is there any way to edit the captured packets before reaching the destination? (UDP/TCP)
Is there any other procedure to capture packets?
Here is my code template/procedure:
1. At first, when loading I get all the devices/hostnames on the computer.
IPHostEntry HostEntry = Dns.GetHostEntry((Dns.GetHostName()));
2. For sniffing the socket to capture the packets has to be a raw socket, with the address family being of type internetwork, and protocol being IP.
mainSocket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
3. Bind the socket to the selected IP address.
mainSocket.Bind(new IPEndPoint(IPAddress.Parse(TEXT-OF-YOUR-HOST-ENTRY), 0));
4. Set the socket options.
5. Capture outgoing packets to byte[] array.
mainSocket.IOControl(IOControlCode.ReceiveAll, YOUR-TRUE-BYTES, YOUR-OUTGOING-BYTES);
6. Start receiving the packets asynchronously.
mainSocket.BeginReceive(BYTE-DATA, 0, BYTE-DATA-LENGTH, SocketFlags.None, new AsyncCallback(YOUR-RECEIVE-FUNCTION), null);
7. Analyze/Parse the bytes received. And don't forget to call another to BeginReceive so that we continue to receive the incoming packets.
8. Finally for parsing, you can create your own class or using the already invented classes/dlls to study your captured packages.
Thats all I have.
I hope this helps for someone trying to start some network sniffer in C#.
I hope that someone shows me the better ways to do this.
Also is there any chance to edit the packages I captured before they reach their destination?
Thank you for your time.
I know this question has been asked many times. I've read ALL the answers and tried EVRY piece of code I could find. After a few days I'm so desperate that I have to ask you for help.
I have a device and a PC in my home network. The device sends UDP broadcast messages. On my PC I can see those messages in wireshark:
Source Destination Length
192.168.1.102 0.0.0.0 UDP 60 Source port: 9050 Destination port: 0
That means the packets are arriving on my PC. My next step was to create a C# application that receives those packets. As mentioned above I tried every possible solution, but it just won't receive anything.
So I guess there must be something very basic I'm doing wrong.
Can anyone help me out? Thanks!
Just experienced the same issue, and wanted to share what fixed it for me.
Briefly: it seems that Windows Firewall was somehow the cause of this weird behavior, and just disabling the service does not help. You have to explicitly allow incoming UDP packets for specific program (executable) in Windows Firewall inbound rules list.
For full case description, read on.
My network setup is: IP of my (receiving) machine was 192.168.1.2, IP of sending machine was 192.168.1.50, and subnet mask on both machines was 255.255.255.0.
My machine is running Windows 7 x64.
This is the code (roughly) that I used:
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint iep = new IPEndPoint(IPAddress.Any, 0);
sock.Bind(iep);
sock.EnableBroadcast = true;
EndPoint ep = (EndPoint)iep;
byte[] buffer = new byte[1000];
sock.ReceiveFrom(buffer, ref ep);
Initially this did not work unless I sent a broadcast packet from that socket before I call ReceiveFrom on it. I.e. adding this line before ReceiveFrom call:
sock.SendTo(someData, new IPEndPoint(IPAddress.Broadcast, somePort))
When I didn't send the broadcast packet first from receiving socket, incoming broadcast packets were not received by it, even though they appeared in Wireshark (destination of packets was 255.255.255.255).
I thought that it looks like firewall is messing with incoming packets (unless some kind of UDP hole is opened first by outgoing packet - even though I haven't heard before that UDP hole punching applies to broadcast packets somehow), so I went to services and disabled Windows firewall service altogether. This changed nothing.
However, after trying everything else, I re-enabled the firewall service, and tried to run the program again. This time, firewall prompt appeared asking me whether I want to allow MyProgram.vshost.exe process (I was debugging in Visual Studio) through firewall, I accepted it, and voila - everything worked! Incoming packets were being received now!
You are OK, they have something wired in the code which causes the problem. (I haven't read the article, just copy pasted)
It always works from the local machine, but from a remote machine it will fail for some reason.
To fix this:
In the Broadcst.cs they broadcast twice. once for the localhost and then for the target IP adress (iep2). simply remove the
sock.SendTo(data, iep1);
and it should work.
No idea why.
I am using C# sockets to have a connection between a device (client) and a computer (server).
All is working good except for the fact that I am trying to avoid the user to enter the IP address and port number wherein to connect to on the device. Instead I want a listbox or drop down list of all IP addresses with listening sockets. Just wondering if there's a way to get all the IP addresses and port numbers of hosts with listening (socket)?
Thanks for any help! :)
What you're asking to do is called a port scan. It basically involves testing each IP address in a range and each port and reporting the success of that attempt. It's slow and it will cause a lot of alarms if there is any kind of threat monitoring on the network because port scanning is one of the ways attackers try to find network vulnerabilities.
So in short, this is a bad idea and probably won't be responsive enough for you to use for this purpose. Instead what you might consider is using a central server as a "directory" that each server would register with.
Or you can send out a broadcast on your subnet and wait for servers to respond. This is how some of the peer networking works in Windows for example. Note that this assumes you are the developer of the server as well and you can add in the logic necessary for the server to listen for broadcasts.
By using UDP broadcast, you can send out the data to the Server which are listening at the fixed port. The following is the working example.
foreach (IPAddress ip in allLocalNetworkAddresses.AddressList)
{
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//Allow sending broadcast messages
client.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.Broadcast, 1);
//Create endpoint, broadcast.
IPEndPoint AllEndPoint = new IPEndPoint(IPAddress.Broadcast, Port);
byte[] sendData = Encoding.ASCII.GetBytes("1");
//Send message to everyone on this network
client.SendTo(sendData, AllEndPoint);
Console.Write("Client send '1' to " + AllEndPoint.ToString() +
Environment.NewLine);
I have an application that sends broadcast messages and listens for response packets. Below is the code snippet.
m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
m_socket.Bind(new IPEndPoint(IPAddress.Any, 2000));
m_socket.BeginSendTo(
buffer,
0,
buffer.Length,
SocketFlags.None,
new IPEndPoint(IPAddress.Broadcast, 2000),
Callback),
null
);
When I run the application the broadcast message was not being sent. On my machine I have three network adapters. One is my local network adapter and other two are VMWare network virtual adapters. When I run my application I can see (using wireshark network capture) that the broadcast message is being sent from one of the VMWare network adapters.
I would like to modify the code so that the broadcast message will be sent from all network adapters on the pc. What is the best way to do that?
You can use the following to get all your IP Addresses (and a lot more). So you can iterate through the list and bind (like Jon B said) to the specific IP you want when you send out your multicast.
foreach (var i in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces())
foreach (var ua in i.GetIPProperties().UnicastAddresses)
Console.WriteLine(ua.Address);
When you call Bind(), you are setting the local IP end point. Instead of using IPAddress.Any, use the IP address of the NIC that you want to send from. You'll need to do this separately for each NIC.
Check this http://salaam.codeplex.com/
I and my friend developed a class library called Salaam. download the source code or use the binaries (.dll) to use it.
You can use IPAddress.Any when constructing the tcpListener. This will bind the tcp listener to all interfaces