first commit
This commit is contained in:
commit
e4aee5039b
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
BroadcastReciever/obj
|
||||||
|
BroadcastReciever/bin
|
||||||
|
BroadcastSender/bin
|
||||||
|
BroadcastSender/obj
|
||||||
|
.vs
|
||||||
37
Broadcast-Test.sln
Normal file
37
Broadcast-Test.sln
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio Version 16
|
||||||
|
VisualStudioVersion = 16.0.31112.23
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BroadcastReciever", "BroadcastReciever\BroadcastReciever.csproj", "{F1563BFC-C69B-48EF-87B9-F3A1C53EE230}"
|
||||||
|
EndProject
|
||||||
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Utils", "..\Utils\Utils\Utils\Utils.csproj", "{84041817-4DEE-43F9-BC28-D92F1430FCA7}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BroadcastSender", "BroadcastSender\BroadcastSender.csproj", "{E154AD71-176A-4A0E-9C42-EA944E088985}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{F1563BFC-C69B-48EF-87B9-F3A1C53EE230}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{F1563BFC-C69B-48EF-87B9-F3A1C53EE230}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{F1563BFC-C69B-48EF-87B9-F3A1C53EE230}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{F1563BFC-C69B-48EF-87B9-F3A1C53EE230}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{84041817-4DEE-43F9-BC28-D92F1430FCA7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{84041817-4DEE-43F9-BC28-D92F1430FCA7}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{84041817-4DEE-43F9-BC28-D92F1430FCA7}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{84041817-4DEE-43F9-BC28-D92F1430FCA7}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{E154AD71-176A-4A0E-9C42-EA944E088985}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{E154AD71-176A-4A0E-9C42-EA944E088985}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{E154AD71-176A-4A0E-9C42-EA944E088985}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{E154AD71-176A-4A0E-9C42-EA944E088985}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
|
SolutionGuid = {B7DF5524-371E-42E4-88F1-63331B4B32FC}
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
||||||
12
BroadcastReciever/BroadcastReciever.csproj
Normal file
12
BroadcastReciever/BroadcastReciever.csproj
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net5.0</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\..\Utils\Utils\Utils\Utils.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
95
BroadcastReciever/Program.cs
Normal file
95
BroadcastReciever/Program.cs
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
using BlubbFish.Utils;
|
||||||
|
|
||||||
|
namespace BroadcastReciever {
|
||||||
|
class Program {
|
||||||
|
private readonly IPAddress s_mcastAddress;
|
||||||
|
private readonly Int32 s_mcastPort;
|
||||||
|
private Socket s_mcastSocket;
|
||||||
|
private MulticastOption s_mcastOption;
|
||||||
|
|
||||||
|
public Program(String[] args) {
|
||||||
|
CmdArgs.Instance.SetArguments(new Dictionary<String, CmdArgs.VaildArguments>() {
|
||||||
|
{ "adapter", new CmdArgs.VaildArguments(CmdArgs.ArgLength.Touple, true) },
|
||||||
|
{ "multicast", new CmdArgs.VaildArguments(CmdArgs.ArgLength.Touple, false, "224.0.24.1") }
|
||||||
|
}, args);
|
||||||
|
if(!CmdArgs.Instance.HasAllRequiredArguments()) {
|
||||||
|
Console.Error.WriteLine(CmdArgs.Instance.GetUsageList("BroadcastReciever.exe"));
|
||||||
|
_ = Console.ReadLine();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.s_mcastAddress = IPAddress.Parse(CmdArgs.Instance.GetArgumentData("multicast"));
|
||||||
|
this.s_mcastPort = 11000;
|
||||||
|
|
||||||
|
// Start a multicast group.
|
||||||
|
this.StartMulticast();
|
||||||
|
|
||||||
|
// Display MulticastOption properties.
|
||||||
|
this.MulticastOptionProperties();
|
||||||
|
|
||||||
|
// Receive broadcast messages.
|
||||||
|
this.ReceiveBroadcastMessages();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void StartMulticast() {
|
||||||
|
try {
|
||||||
|
this.s_mcastSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
||||||
|
|
||||||
|
IPAddress localIPAddr = IPAddress.Parse(CmdArgs.Instance.GetArgumentData("adapter"));
|
||||||
|
|
||||||
|
//IPAddress localIP = IPAddress.Any;
|
||||||
|
EndPoint localEP = new IPEndPoint(localIPAddr, this.s_mcastPort);
|
||||||
|
|
||||||
|
this.s_mcastSocket.Bind(localEP);
|
||||||
|
|
||||||
|
|
||||||
|
// Define a MulticastOption object specifying the multicast group
|
||||||
|
// address and the local IPAddress.
|
||||||
|
// The multicast group address is the same as the address used by the server.
|
||||||
|
this.s_mcastOption = new MulticastOption(this.s_mcastAddress, localIPAddr);
|
||||||
|
|
||||||
|
this.s_mcastSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, this.s_mcastOption);
|
||||||
|
} catch(Exception e) {
|
||||||
|
Console.WriteLine(e.ToString());
|
||||||
|
_ = Console.ReadLine();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void MulticastOptionProperties() {
|
||||||
|
Console.WriteLine("Current multicast group is: " + this.s_mcastOption.Group);
|
||||||
|
Console.WriteLine("Current multicast local address is: " + this.s_mcastOption.LocalAddress);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ReceiveBroadcastMessages() {
|
||||||
|
Boolean done = false;
|
||||||
|
Byte[] bytes = new Byte[100];
|
||||||
|
IPEndPoint groupEP = new(this.s_mcastAddress, this.s_mcastPort);
|
||||||
|
EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
|
||||||
|
|
||||||
|
try {
|
||||||
|
Console.WriteLine("Waiting for multicast packets.......");
|
||||||
|
Console.WriteLine("Enter ^C to terminate.");
|
||||||
|
while(!done) {
|
||||||
|
Int32 length = this.s_mcastSocket.ReceiveFrom(bytes, ref remoteEP);
|
||||||
|
Console.WriteLine("Received broadcast from \"{0}\" [{1}]: {2}", groupEP.ToString(), length, Encoding.ASCII.GetString(bytes, 0, bytes.Length));
|
||||||
|
}
|
||||||
|
|
||||||
|
this.s_mcastSocket.Close();
|
||||||
|
} catch(Exception e) {
|
||||||
|
Console.WriteLine(e.ToString());
|
||||||
|
_ = Console.ReadLine();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void Main(String[] args) => _ = new Program(args);
|
||||||
|
}
|
||||||
|
}
|
||||||
12
BroadcastSender/BroadcastSender.csproj
Normal file
12
BroadcastSender/BroadcastSender.csproj
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net5.0</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\..\Utils\Utils\Utils\Utils.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
106
BroadcastSender/Program.cs
Normal file
106
BroadcastSender/Program.cs
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
using BlubbFish.Utils;
|
||||||
|
|
||||||
|
namespace BroadcastSender {
|
||||||
|
class Program {
|
||||||
|
private readonly IPAddress s_mcastAddress;
|
||||||
|
private readonly Int32 s_mcastPort;
|
||||||
|
private Socket s_mcastSocket;
|
||||||
|
private MulticastOption s_mcastOption;
|
||||||
|
|
||||||
|
public Program(String[] args) {
|
||||||
|
CmdArgs.Instance.SetArguments(new Dictionary<String, CmdArgs.VaildArguments>() {
|
||||||
|
{ "adapter", new CmdArgs.VaildArguments(CmdArgs.ArgLength.Touple, true) },
|
||||||
|
{ "multicast", new CmdArgs.VaildArguments(CmdArgs.ArgLength.Touple, false, "224.0.24.1") },
|
||||||
|
{ "count", new CmdArgs.VaildArguments(CmdArgs.ArgLength.Touple, false, "100") },
|
||||||
|
{ "wait", new CmdArgs.VaildArguments(CmdArgs.ArgLength.Touple, false, "500") }
|
||||||
|
}, args);
|
||||||
|
if(!CmdArgs.Instance.HasAllRequiredArguments()) {
|
||||||
|
Console.Error.WriteLine(CmdArgs.Instance.GetUsageList("BroadcastReciever.exe"));
|
||||||
|
_ = Console.ReadLine();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize the multicast address group and multicast port.
|
||||||
|
// Both address and port are selected from the allowed sets as
|
||||||
|
// defined in the related RFC documents. These are the same
|
||||||
|
// as the values used by the sender.
|
||||||
|
this.s_mcastAddress = IPAddress.Parse(CmdArgs.Instance.GetArgumentData("multicast"));
|
||||||
|
this.s_mcastPort = 11000;
|
||||||
|
|
||||||
|
// Join the listener multicast group.
|
||||||
|
this.JoinMulticastGroup();
|
||||||
|
|
||||||
|
// Display MulticastOption properties.
|
||||||
|
this.MulticastOptionProperties();
|
||||||
|
|
||||||
|
// Broadcast the message to the listener.
|
||||||
|
this.BroadcastMessage("Hello multicast listener.");
|
||||||
|
}
|
||||||
|
|
||||||
|
void JoinMulticastGroup() {
|
||||||
|
try {
|
||||||
|
// Create a multicast socket.
|
||||||
|
this.s_mcastSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp) {
|
||||||
|
Ttl = 32
|
||||||
|
};
|
||||||
|
|
||||||
|
// Get the local IP address used by the listener and the sender to
|
||||||
|
// exchange multicast messages.
|
||||||
|
IPAddress localIPAddr = IPAddress.Parse(CmdArgs.Instance.GetArgumentData("adapter"));
|
||||||
|
|
||||||
|
// Create an IPEndPoint object.
|
||||||
|
// Bind this endpoint to the multicast socket.
|
||||||
|
this.s_mcastSocket.Bind(new IPEndPoint(localIPAddr, 0));
|
||||||
|
|
||||||
|
// Define a MulticastOption object specifying the multicast group
|
||||||
|
// address and the local IP address.
|
||||||
|
// The multicast group address is the same as the address used by the listener.
|
||||||
|
this.s_mcastOption = new MulticastOption(this.s_mcastAddress, localIPAddr);
|
||||||
|
|
||||||
|
this.s_mcastSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, this.s_mcastOption);
|
||||||
|
this.s_mcastSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 64);
|
||||||
|
} catch(Exception e) {
|
||||||
|
Console.WriteLine("\n" + e.ToString());
|
||||||
|
_ = Console.ReadLine();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void BroadcastMessage(String message) {
|
||||||
|
IPEndPoint endPoint;
|
||||||
|
|
||||||
|
try {
|
||||||
|
//Send multicast packets to the listener.
|
||||||
|
for(Int32 i = 0; i < Int32.Parse(CmdArgs.Instance.GetArgumentData("count")); i++) {
|
||||||
|
|
||||||
|
endPoint = new IPEndPoint(this.s_mcastAddress, this.s_mcastPort);
|
||||||
|
_ = this.s_mcastSocket.SendTo(Encoding.ASCII.GetBytes(message+" "+i), endPoint);
|
||||||
|
Console.WriteLine("Multicast data sent "+i+".....");
|
||||||
|
Thread.Sleep(Int32.Parse(CmdArgs.Instance.GetArgumentData("wait")));
|
||||||
|
}
|
||||||
|
} catch(Exception e) {
|
||||||
|
Console.WriteLine("\n" + e.ToString());
|
||||||
|
_ = Console.ReadLine();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.s_mcastSocket.Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void MulticastOptionProperties() {
|
||||||
|
Console.WriteLine("Current multicast group is: " + this.s_mcastOption.Group);
|
||||||
|
Console.WriteLine("Current multicast local address is: " + this.s_mcastOption.LocalAddress);
|
||||||
|
Console.WriteLine("Current multicast ttl: " + this.s_mcastSocket.GetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive));
|
||||||
|
Console.WriteLine("Current ttl: " + this.s_mcastSocket.Ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void Main(String[] args) => _ = new Program(args);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user