|
-
Jan 26th, 2012, 09:06 AM
#1
Thread Starter
Lively Member
UDP Broadcasting
Afternoon,
I've just got into using sockets and found myself a little stuck so hopefully someone can point me in the right direction.
I am in the process of building a remote monitoring app for the office environment and want to add in a feature which will either generate a popup of customer text on the target machine or to all users who are currently using the client application.
Now my first ideas were either Net Send or MSG.exe but both of these are off the table so I've now looked into broadcasting over UDP and I'll happily admit I'm a bit of a novice so apologies if I've got anything wrong.
I have been looking at a pretty helpful site to get me started http://codeidol.com/csharp/csharp-ne...-Broadcasting/ and below is the code I have for both sending and receiving.
Sending
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);
IPEndPoint iep2 = new IPEndPoint(IPAddress.Parse("152.144.49.116"), 8080);
string hostname = Dns.GetHostName();
byte[] data = Encoding.ASCII.GetBytes(hostname);
sock.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.Broadcast, 1);
sock.SendTo(data, iep2);
sock.Close();
}
}
}
Receiving
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Socket sock = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
IPEndPoint iep = new IPEndPoint(IPAddress.Any, 8080);
sock.Bind(iep);
EndPoint ep = (EndPoint)iep;
Console.WriteLine("Ready to receive...");
byte[] data = new byte[1024];
int recv = sock.ReceiveFrom(data, ref ep);
string stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine("received: {0} from: {1}",
stringData, ep.ToString());
data = new byte[1024];
recv = sock.ReceiveFrom(data, ref ep);
stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine("received: {0} from: {1}",
stringData, ep.ToString());
sock.Close();
}
}
}
The trouble I have is that when I open the code on the target machine I'm testing on within Visual C# Express, the console app just sits there saying "Ready to receive..." and nothing else happens regardless of when I launch the send code. Now I figured that there would be firewall issues with my network so I opted for port 80, is this the best way to achieve this?
Now I've followed the example I read in the link pretty much to the letter so I doubt I have many problems if any with the code but I'm a little stuck where abouts to go from here.
Also, this example is in a console application, what would be the best practice for moving it over to a Winform application? When does the application check the port for any incoming data?
Edit: I should also add that I have checked and confirmed the right IP address of the target machine and that they are both on the same Subnet.
I realise I'm asking a lot but any help or nudge in the right direction would be greatly appreciated!
Many thanks,
Last edited by Madcat1981; Jan 6th, 2016 at 10:07 AM.
Reason: additional info
-
Jan 26th, 2012, 09:57 AM
#2
Re: UDP Broadcasting
 Originally Posted by Madcat1981
...someone can point me in the wrong direction.
...
LOL I'm sure anyone can do that...
In regards to your question, did you know that routers don't forward UDP broadcasts outside your local network? UDP broadcast works only on local network.
-
Jan 26th, 2012, 10:07 AM
#3
Thread Starter
Lively Member
Re: UDP Broadcasting
 Originally Posted by CVMichael
LOL  I'm sure anyone can do that...
In regards to your question, did you know that routers don't forward UDP broadcasts outside your local network? UDP broadcast works only on local network.
Haha, fallen at the first hurdle! 
Hmm, that could be the cause of my problems. What could be a good solution for what I'm trying to achieve?
Last edited by Madcat1981; Jan 6th, 2016 at 10:06 AM.
-
Jan 26th, 2012, 10:12 AM
#4
Re: UDP Broadcasting
I think what you need is Multicast, but I just know the "term", I have no clue how to do it 
I think the way it works is you have to give winsock all IP's you want to send the UDP, and then basically it sends the packets to only those IPs. Kind of like looping through the IPs and sending the packets to them, but I think when you Multicast, it sends only one packet but it's reaching all the IPs you give it to.
Again... I am not sure about this...
-
Jan 26th, 2012, 10:29 AM
#5
Thread Starter
Lively Member
Re: UDP Broadcasting
Many thanks, I shall have a look into Multicasting.
I have just now installed Wireshark to analyse the data coming in on UDP and apparently from what I can see it has come through successfully but not sure what it means where it's telling me the source port is different.
This is the output I got: (I changed the port to 8080 from 80 just to test)
User Datagram Protocol, Src Port: 4756 (4756), Dst Port: http-alt (8080)
Now if I run it again I get a different Source Port which is confusing. Is there any reason why it would change?
Last edited by Madcat1981; Jan 6th, 2016 at 10:06 AM.
-
Jan 27th, 2012, 04:53 PM
#6
Lively Member
Re: UDP Broadcasting
I can help you on that topic. Michael is correct in saying Broadcasting only effects your own subnet and does not broadcast to any other subnets. Also you cannot simply multicast to any Ip, you need to construct a certain IP based on the subnet you are targeting.
 Originally Posted by Madcat1981
Many thanks, I shall have a look into Multicasting.
I have just now installed Wireshark to analyse the data coming in on UDP and apparently from what I can see it has come through successfully but not sure what it means where it's telling me the source port is different.
This is the output I got: (I changed the port to 8080 from 80 just to test)
User Datagram Protocol, Src Port: 4756 (4756), Dst Port: http-alt (8080)
Now if I run it again I get a different Source Port which is confusing. Is there any reason why it would change?
Guy
The reason for this is because UDP is conection-less, you do not need to bind the Server to a certain port as no client should be able to connect to the server anyway. Instead, Windows simply assigns you an available port for your program to use, regardless of the destination port.
I will get back with a walk through in a second 
EDIT: Just realised the original link you posted for where you found out about Broadcasting had stolen info from my blog -.- Sending a email to them now
-jD
Last edited by jduncanator; Jan 27th, 2012 at 11:33 PM.
Reason: Copyright Infringment :S
-
Jan 27th, 2012, 05:19 PM
#7
Lively Member
Re: UDP Broadcasting
What Is Broadcasting?
IP broadcasting is used by network devices to send a single packet of information that can be accessible by every
device on the network. Because TCP communication requires that two devices have a dedicated connection, it is not
possible to send broadcast messages in a strictly TCP environment. Instead, UDP packets must be used because
that protocol has the capability of sending messages without a specific connection being defined.
Local versus Global Broadcasts
Broadcast messages contain a special destination IP address. The IP address format allows for two types of
broadcast message addresses: local broadcasts and global broadcasts.
Network programmers use the local broadcast address to send a broadcast message destined for all devices on a
particular subnet. The idea is to localize a broadcast message so that other networks are not affected by the
broadcast.
An IP address is divided into two parts, a network address and
a host address. The standard network address part makes up the first part of the local broadcast address, and all 1s
are used for the host part of the address (which is the decimal value 255 in the address octet). This is demonstrated
in the image below. Thus, for the class B network 192.168.0.0, using a subnet mask of 255.255.0.0, the local broadcast
address would be 192.168.255.255.

The IP network and host address parts of a local broadcast address
Similarly, if the subnet is further divided using a subnet mask of 255.255.255.0, each subnet would have its own local
broadcast address. The subnet 192.168.1.0 would have a broadcast address of 192.168.1.255, and so on up to the
subnet 192.168.254.0, which would have the broadcast address 192.168.254.255.
The global broadcast was originally intended to allow a device to send a packet to all devices on an internetwork. It
uses all 1s in the IP address, creating an address of 255.255.255.255. This special broadcast address indicates that
the destination of the packet is all devices on all networks accessible from the device sending the message.
The vast size and burgeoning popularity of the Internet, of course, meant that this behavior had to be modified, otherwise, it would be easy for a rogue programmer to create a stream of global broadcasts that would propagate to
all networks on the Internet, effectively clogging the worldwide system with bogus broadcasts and stopping normal
traffic. To eliminate this possibility, routers do not send global IP broadcasts to other networks unless specifically
configured to do so (which is practically never). Instead, they silently ignore global broadcasts, effectively turning
them into local broadcasts that are seen only on the local network over which they were sent.
What Is Multicasting?
Broadcasting is an excellent way to send information to all devices on a subnet, but it does have a drawback: the broadcast packets are restricted to the local subnet. IP multicasting was devised to allow an application to send a single packet to a select subset of devices both on the local subnet and across network boundaries. This feature allows an application to join a multicast group to participate in a wide-area conference.
Just like broadcasting, IP multicasting uses special IP addresses. The IP multicasting scheme uses a particular range of IP addresses to designate different multicast groups. Each multicast group consists of a group of devices listening to the same IP address. As packets are sent out destined for the multicast group address, each device listening to the address receives them.
The IP address range 224.0.0.1 through 239.255.255.255 represents multicast groups. According to Internet Request For Comments (RFC) 3171, the groups are divided as shown in the below list.
IP Multicast Address Assignments:
Range Assignment
224.0.0.0-224.0.0.255 -Local network control block
224.0.1.0-224.0.1.255 -Internetwork control block
224.0.2.0-224.0.255.0 -AD-HOC block
224.1.0.0-224.1.255.255 -ST multicast groups
224.2.0.0-224.2.255.255 -SDP/SAP block
224.252.0.0-224.255.255.255 -DIS transient block
225.0.0.0-231.255.255.255 -Reserved
232.0.0.0-232.255.255.255 -Source,specific multicast block
233.0.0.0-233.255.255.255 -GLOP block
234.0.0.0-238.255.255.255 -Reserved
239.0.0.0-239.255.255.255 -Administratively scoped block
Within each of these address blocks, individual IP addresses are assigned to specific projects. For example, addresses 224.0.0.1 and 224.0.0.2 are reserved for routers to communicate multicast group information between themselves. You should avoid using multicast group addresses that occur within these blocks.
Multicast Techniques
There are two techniques used to control multicast sessions:
• A peer-to-peer technique, in which all clients can send messages to all other clients in the group
• A central server that sends messages to group clients
Peer-to-Peer Technique
In a peer-to-peer multicast group (image below), all of the clients in the multicast group have equal rights in the group. Any client in the group has the capability to exchange messages with any other client in the group.

In a peer-to-peer multicast group, all clients can exchange messages with any client in the group.
The IP system supports peer-to-peer multicast groups by allowing any device on the network to accept and send packets destined for the multicast group IP address. By default, there are no restrictions on which clients can join a multicast group. Some implementations use encryption to prevent unauthorized clients from interpreting the data received in the multicast group, but there is still no way to block the clients receipt of the data.
Central Server
The other multicast system employs a central server, a single device on the network that controls all multicast group activity. An individual client wanting to join the multicast group must ask permission from the central server. If the central server denies the client access to that multicast group, no multicast packets will be forwarded to the requesting client. This technique is shown in the image below:

The central server setup for controlling multicast groups
Note
The central server multicast group
system is not supported by IP.
Currently, Asynchronous Transfer
Mode (ATM) networks are the only
networks capable of supporting central
server multicast groups.
Sending Multicast Packets through Routers
Although multicast packets can be passed across network boundaries, making this happen requires some effort on the part of the network routers. By default, most routers do not pass multicast packets through to other subnets. If a router passed every received multicast packet to every interface, it would put the network at risk of being flooded with multicast packets. Instead, a system has been developed to allow selective forwarding of multicast packets.
The Internet Group Management Protocol (IGMP) was developed to aid in notifying routers when multicast packets should to be passed to various subnets. When a network device wants to join a multicast group, it sends an IGMP packet to the local router on its subnet. The IGMP packet registers the network device and the multicast group address from which that device must receive messages. This enables the router to know that it must forward any received multicast messages for that group to the subnet of the specified network device. The image below demonstrates the principle of multicast group registration.

Network devices registering multicast group memberships on a router
As shown in the image above, when each network device registers its intention to receive packets for the multicast group, the router must forward any received multicast packets to each interface that contains a registered host. Interfaces that do not contain registered hosts do not need to receive the multicast packet.
Similarly, when a network host leaves the multicast group, another IGMP packet is sent to the router, notifying it that the host no longer needs to have packets for that multicast group forwarded. When the last network host on a specific router interface leaves the group, the router can stop forwarding multicast packets to that interface.
Last edited by jduncanator; Jan 27th, 2012 at 05:50 PM.
Reason: merged replies into "Background info" thread
-
Jan 27th, 2012, 05:48 PM
#8
Lively Member
Re: UDP Broadcasting
C# Socket Multicasting
The Socket class supports IP multicasting by using the SetSocketOption() method of the socket, but in a different way than with broadcasting.
There are two socket options that can be used for multicasting:
· Adding the socket to a multicast group
· Removing the socket from a multicast group
Both of these options are defined by IP-level Socket option names, AddMembership and DropMembership. The value parameter of the SetSocketOption() method is not a normal data value. Instead, it uses the
MulticastOption class.
The MulticastOption class defines the multicast group that the socket will be added to or removed from. These two constructors are used for the MulticastOption class:
Code:
MulticastOption(IPAddress)
MulticastOption(IPAddress,IPAddress)
The firsts constructor format defines the IP multicast address used in the SetSocketOption. The default behavior of this constructor is to allow all interfaces on the system to be affected by the SetSocketOption() method. If you want to limit the action to an individual network interface on the system, the second constructor allows specification of an
IPAddress value to represent the interface.
For example, if you wanted to add a socket to the multicast group 224.100.0.1, you would use the following statement:
Code:
sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership,
new MulticastOption(IPAddress.Parse("224.100.0.1"));
This sets the socket to join the multicast group for all interfaces on the system.
Receiving Multicast Packets
The MultiRecv sets a socket to receive multicast packets destined for the 224.100.0.1 multicast group.
CSharp Code:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class MultiRecv
{
public static void Main()
{
Socket sock = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
Console.WriteLine("Ready to receive ");
IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
EndPoint ep = (EndPoint)iep;
sock.Bind(iep);
sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption IPAddress.Parse("224.100.0.1")));
byte[] data = new byte[1024];
int recv = sock.ReceiveFrom(data, ref ep);
string stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine("received: {0} from: {1}", stringData, ep.ToString());
sock.Close();
}
}
The MultiRecv program creates a UDP socket in the normal way, using the standard Socket class methods. After being added to the multicast group, the socket blocks on a ReceiveFrom() method call, waiting for data to arrive.
Some Cautions about Multicast Sockets
There are two very important points to remember about using multicast sockets:
First, the SetSocketOption() method call must be made after the Bind() method call for the socket. This enables the multicast group to be set for a specific IPEndPoint address on the socket.
Second, once the socket has been added to a multicast group, the ReceiveFrom() method will accept packets destined for the following:
· The IPEndPoint address and port specified in the Bind() method
· The multicast group IP address specified in the MulticastOption constructor and the port specified in the IPEndPoint object
· Broadcast packets for the specified IPEndPoint port
Many novice network programmers forget the second item, especially. When a ReceiveFrom() method is performed, you are not guaranteed to receive packets destined just for the multicast group. So be careful of extraneous packets sent from other sources. You will not be able to easily distinguish those packets.
Sending Multicast Packets
Sending multicast packets is easy. Nothing special must be done to send the packets to members in the multicast
group; you just specify the multicast group IP address as the destination address of the packet.
CSharp Code:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class MultiSend
{
public static void Main()
{
Socket server = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
IPEndPoint iep = new IPEndPoint(IPAddress.Parse("224.100.0.1"), 9050);
byte[] data = Encoding.ASCII.GetBytes("This is a test message");
server.SendTo(data, iep);
server.Close();
}
}
Last edited by jduncanator; Jan 27th, 2012 at 06:04 PM.
-
Jan 27th, 2012, 05:52 PM
#9
Re: UDP Broadcasting
Would not it be easier if you just copy & paste the links from wherever you got this info?
-
Jan 27th, 2012, 05:53 PM
#10
Lively Member
Re: UDP Broadcasting
 Originally Posted by CVMichael
Would not it be easier if you just copy & paste the links from wherever you got this info?
Um, that info is something i wrote on my blog AGES ago, and my blog currently is not online.
-
Jan 27th, 2012, 06:19 PM
#11
Lively Member
Re: UDP Broadcasting
If you need this in a more applied format, here is what I think you would need (code wise) from the info you supplied in the opening thread. I suggest using the encrypted version (scroll down to the bottom of this post) as I could simply write a program that binds itself to every multicast address and listen in on your conversation. With DES Encryption, all I would see is a bunch of garbled Printable and Un-Printable ASCII characters, even then, I doubt anyone would get that far as it the DES Decrypter needs to know EXACTLY how many bytes of "text" were sent, this is easily solved with the solution below (send a 4 bit INT declaring how long the message is and then initiate a byte array with that size and copy the message from the network byte array to the new byte array, then pass it to the decrypter) otherwise the decrypter would throw an error, crash or refuse to return a valid string (the crashing happened to me alot in the writing of this code, took me close to 2 hours to find out why. In the end it was a novice mistake -.-)
Multicast "Chat" - No Encryption:
CSharp Code:
/* Multicast chat. (C) Copyright Joshua Duncan 2012 Version 1.0 - 28/01/2012 This code is released under the Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0 This Copyright Block must remain in all versions of the source code. Also any use of any part of my code must give due credit to me (Joshua Duncan). */ using System; using System.Drawing; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Windows.Forms; class MulticastChat : Form { TextBox newText; ListBox results; Socket sock; Thread receiver; /* You can change the IP to anything between 224.3.0.0 to 224.252.0.0 Also feel free to change the port. Just remember to change the IP at both Line 27 and at Line 62, And the port at Line 27 and 59. */ IPEndPoint multiep = new IPEndPoint(IPAddress.Parse("224.100.0.196"), 80); public MulticastChat() { Text = "Multicast Remote Chat"; Size = new Size(400, 380); Label label1 = new Label(); label1.Parent = this; label1.Text = "Enter text to send:"; label1.AutoSize = true; label1.Location = new Point(10, 30); newText = new TextBox(); newText.Parent = this; newText.Size = new Size(200, 2 * Font.Height); newText.Location = new Point(10, 55); results = new ListBox(); results.Parent = this; results.Location = new Point(10, 85); results.Size = new Size(360, 18 * Font.Height); Button sendit = new Button(); sendit.Parent = this; sendit.Text = "Send"; sendit.Location = new Point(220,52); sendit.Size = new Size(5 * Font.Height, 2 * Font.Height); sendit.Click += new EventHandler(ButtonSendOnClick); Button closeit = new Button(); closeit.Parent = this; closeit.Text = "Close"; closeit.Location = new Point(290, 52); closeit.Size = new Size(5 * Font.Height, 2 * Font.Height); closeit.Click += new EventHandler(ButtonCloseOnClick); sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint iep = new IPEndPoint(IPAddress.Any, 80); sock.Bind(iep); sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("224.100.0.196"))); receiver = new Thread(new ThreadStart(packetReceive)); receiver.IsBackground = true; receiver.Start(); } void ButtonSendOnClick(object obj, EventArgs ea) { byte[] message = Encoding.ASCII.GetBytes(newText.Text); newText.Clear(); sock.SendTo(message, SocketFlags.None, multiep); } void ButtonCloseOnClick(object obj, EventArgs ea) { receiver.Abort(); sock.Close(); Close(); } void packetReceive() { EndPoint ep = (EndPoint)multiep; byte[] data = new byte[1024]; string stringData; int recv; while (true) { recv = sock.ReceiveFrom(data, ref ep); stringData = Encoding.ASCII.GetString(data, 0, recv); results.Items.Add("from " + ep.ToString() + ": " + stringData); } } public static void Main() { Application.Run(new MulticastChat()); } }
This will give you a WinForms app you can use to send and receive Multicast packets. As said above, because this is peer-to-peer and anyone could "drop in" on your multicast connection, I suggest implementing some sort of Packet Encryption. Simply start this program on two seperate computers and it should work.
EDIT: Do not attempt to run the program on the same computer as it trys to bind two UDP sockets to the same port. It also doesn't seem to throw an errorr ... /:
Multicast "Chat" - DES Packet Encryption
CSharp Code:
/* Multicast chat with DES Packet Encryption. (C) Copyright Joshua Duncan 2012 Version 1.0 - 28/01/2012 This code is released under the Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0 This Copyright Block must remain in all versions of the source code. Also any use of any part of my code must give due credit to me (Joshua Duncan). */ using System; using System.Drawing; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Windows.Forms; using System.IO; using System.Security; using System.Security.Cryptography; class MulticastChat : Form { TextBox newText; RichTextBox results; Socket sock; Thread receiver; /* You can change the IP to anything between 224.3.0.0 to 224.252.0.0 Also feel free to change the port. Just remember to change the IP at both Line 31 and at Line 66, And the port at Line 31 and 63. */ IPEndPoint multiep = new IPEndPoint(IPAddress.Parse("224.100.23.196"), 80); public MulticastChat() { Text = "Multicast Remote Chat"; Size = new Size(400, 380); Label label1 = new Label(); label1.Parent = this; label1.Text = "Enter text to send:"; label1.AutoSize = true; label1.Location = new Point(10, 30); newText = new TextBox(); newText.Parent = this; newText.Size = new Size(200, 2 * Font.Height); newText.Location = new Point(10, 55); results = new RichTextBox(); results.Parent = this; results.Location = new Point(10, 85); results.Size = new Size(360, 18 * Font.Height); Button sendit = new Button(); sendit.Parent = this; sendit.Text = "Send"; sendit.Location = new Point(220,52); sendit.Size = new Size(5 * Font.Height, 2 * Font.Height); sendit.Click += new EventHandler(ButtonSendOnClick); Button closeit = new Button(); closeit.Parent = this; closeit.Text = "Close"; closeit.Location = new Point(290, 52); closeit.Size = new Size(5 * Font.Height, 2 * Font.Height); closeit.Click += new EventHandler(ButtonCloseOnClick); sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint iep = new IPEndPoint(IPAddress.Any, 80); sock.Bind(iep); sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("224.100.23.196"))); receiver = new Thread(new ThreadStart(packetReceive)); receiver.IsBackground = true; receiver.Start(); } void ButtonSendOnClick(object obj, EventArgs ea) { string encode = Encrypt(newText.Text); byte[] message = Encoding.UTF8.GetBytes(encode); int memsize = (int)message.Length; byte[] size = BitConverter.GetBytes(memsize); byte[] send = new byte[4+memsize]; Array.Copy(size, 0, send, 0, 4); Array.Copy(message, 0, send, 4, message.Length); newText.Clear(); sock.SendTo(send, SocketFlags.None, multiep); } void ButtonCloseOnClick(object obj, EventArgs ea) { receiver.Abort(); sock.Close(); Close(); } void packetReceive() { EndPoint ep = (EndPoint)multiep; byte[] data = new byte[2048]; string stringData; int recv; while (true) { recv = sock.ReceiveFrom(data, ref ep); byte[] memsize = new byte[4]; Array.Copy(data, 0, memsize, 0, 4); int size = BitConverter.ToInt32(memsize, 0); byte[] message = new byte[size]; Array.Copy(data, 4, message, 0, size); stringData = Encoding.UTF8.GetString(message, 0, size); stringData = Decrypt(stringData); results.AppendText("from " + ep.ToString() + ": " + stringData + Environment.NewLine); } } public static string Encrypt(string originalString) { byte[] Key = ASCIIEncoding.ASCII.GetBytes("password"); DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider(); MemoryStream memoryStream = new MemoryStream(); CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(Key, Key), CryptoStreamMode.Write); StreamWriter writer = new StreamWriter(cryptoStream); writer.Write(originalString); writer.Flush(); cryptoStream.FlushFinalBlock(); writer.Flush(); return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length); } public static string Decrypt(string cryptedString) { byte[] Key = ASCIIEncoding.ASCII.GetBytes("password"); DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider(); MemoryStream memoryStream = new MemoryStream (Convert.FromBase64String(cryptedString)); CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateDecryptor(Key, Key), CryptoStreamMode.Read); StreamReader reader = new StreamReader(cryptoStream); return reader.ReadToEnd(); } public static void Main() { Application.Run(new MulticastChat()); } }
http://compilr.com/project/51269 - http://pastebin.com/m18cvf3T
-jD
Last edited by jduncanator; Jan 30th, 2012 at 06:23 PM.
Reason: Added code overview and encryption info, Added Encrypted Packets Version, Added Missing Colon off of class declaration
-
Jan 30th, 2012, 05:44 AM
#12
Thread Starter
Lively Member
Re: UDP Broadcasting
Hi jduncanator,
I think it's fair to say you know a fair bit about Broadcasting and Multicasting 
Cannot thank you enough for taking out the time to explain the methodology behind both systems, much appreciated.
I did still, however, bump into some problems. On the following line I was getting a Thread exception
Code:
results.Items.Add("from " + ep.ToString() + ": " + stringData);
All I did was switch it out to just display via a messagebox as that's all I'll need for this particular project. But when I ran it again on 2 separate machines I only received a messagebox on the machine which sent the string.
Using WireShark I have analysed the UDP information on the second "receiving" PC and can see the information but the application never registers it.
Where did I go wrong?
Many thanks,
Last edited by Madcat1981; Jan 6th, 2016 at 10:06 AM.
-
Jan 30th, 2012, 06:23 PM
#13
Lively Member
Re: UDP Broadcasting
Are they on the same network, or on different networks (EG. over the inet) Do you mind supplying the edited code to see exactly where you changed it?
-jD
If I helped you, don't forget to Rate my Post 
-
Jan 30th, 2012, 06:25 PM
#14
Thread Starter
Lively Member
Re: UDP Broadcasting
Yep, both on the same network. In fact they sit right next to each other.
Last edited by Madcat1981; Jan 6th, 2016 at 10:06 AM.
-
Jan 30th, 2012, 08:34 PM
#15
Lively Member
Re: UDP Broadcasting
Ok,
Well then, you got the code for em?
Make sure the firewall isn't blocking them.
Last edited by jduncanator; Jan 30th, 2012 at 08:42 PM.
If I helped you, don't forget to Rate my Post 
-
Jan 30th, 2012, 08:36 PM
#16
Lively Member
Re: UDP Broadcasting
You should have replaced: results.AppendText("from " + ep.ToString() + ": " + stringData + Environment.NewLine);
With MessageBox.Show("from " + ep.ToString() + ": " + stringData);
If I helped you, don't forget to Rate my Post 
-
Jan 31st, 2012, 04:14 AM
#17
Thread Starter
Lively Member
Re: UDP Broadcasting
Hi jduncanator,
Thanks for your continued help with this.
I have looked at any firewall policies in effect and have seen that a range for higher end ports is open for all traffic so now I'm using 1026 but still no joy.
Codewise, i'm using exactly what you provide bar the change to MessageBox.
When using port analyser (Wireshark in my case) I can clearly show the UDP information so surely that should mean it's getting through, correct?
EDIT: One thing I'll add is that I'm doing this in design. Does it require being published in order to completely function?
Last edited by Madcat1981; Jan 6th, 2016 at 10:05 AM.
Reason: extra info
-
Jan 31st, 2012, 05:06 AM
#18
Lively Member
Re: UDP Broadcasting
No it doesn't... Hmmm,
Are you using the one with Encryption?
-jD
If I helped you, don't forget to Rate my Post 
-
Jan 31st, 2012, 05:13 AM
#19
Thread Starter
Lively Member
Re: UDP Broadcasting
Not yet, I thought I'd go through the non-encrypted one first to understand it more.
-
Jan 31st, 2012, 05:13 AM
#20
Lively Member
Re: UDP Broadcasting
Look, I have a big day tommoz, Gotta get some shut eye, I will post my prject file and my exe later.
if you go here and click the download button: http://compilr.com/project/51269 (picture of a cloude with a down arrow) you can download my EXE.
-jD
Last edited by jduncanator; Jan 31st, 2012 at 05:17 AM.
If I helped you, don't forget to Rate my Post 
-
Jan 31st, 2012, 05:15 AM
#21
Thread Starter
Lively Member
Re: UDP Broadcasting
No need to apologise, your life comes first. Just appreciative of the help!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|