first commit
This commit is contained in:
@@ -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>
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user