Broadcast

  • Thread starter Thread starter GTi
  • Start date Start date
G

GTi

I need a code snippet for sending a message to all computers located on
the same IP subnet.
This is only a simple message like; "Here I am"
I'm developing a server/client application.
Instead of configuration of each clients where the server is located I
want the server to broadcast a simple message that the server is
present.
This solution must work on a workgroup and domain even if the clients
are on different domain/workgroup.
Any ideas?
 
Hello GTi,

G> Nice :-)
G> But what about recieving this messages on the client?

On windows platform (since NT) you've got it automatically if Messaging service
is started

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
Michael said:
Hello GTi,

G> Nice :-)
G> But what about recieving this messages on the client?

On windows platform (since NT) you've got it automatically if Messaging service
is started

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Ahh - The Messanger service.
Well - that was not what I was thinking about.
I was more thinking about "low level" programming in C# with winsock
and UDP.
Like
http://www.c-sharpcorner.com//Code/2002/April/UDPMulticasting.asp
But I need more samples about this.
 
Most of the samples I have found the target (server) is specified with
the IP address or hostname.
But I don't know where the target is, so I want to broadcast a message
to all IP adresses on the local subnet. If any of computer on the local
subnet IS the server it will answer back with UDP message. The
computers can be on different domain or workgroup. Target is Windows.

SERVER:
The server is located on any computer on the local subnet listen for a
UDP message on a specified address (port 10000). If it recieves a
"HELLO" message it will broadcast a message to the subnet saying "HERE
I AM" (on port 10001). In that way the client and all other clients
know where the server is located.

CLIENTS:
Clients can be started on any computer located in a subnet. This client
don't know if and where the server is located so it begins sending a
UDP message to all computers on a subnet (on port 10000). It also
listen for UDP messages (on port 10001). When it recieves a message on
port 10001 it know where the server is located, even if it have not
asked about it.

The UDP port 10000 is for the server - listen for clients messages
The UDP port 10001 is for the clients - listen for server messages.
 
Hello GTi,

as I understand you don't want to write any client app to install in on user?
In that case the only way is to use messanger service

Else you are free to use any IPC approaches - sockets, Pipes, Remoting, SOAP,
dcom

G> Most of the samples I have found the target (server) is specified
G> with
G> the IP address or hostname.
G> But I don't know where the target is, so I want to broadcast a
G> message
G> to all IP adresses on the local subnet. If any of computer on the
G> local
G> subnet IS the server it will answer back with UDP message. The
G> computers can be on different domain or workgroup. Target is Windows.
G> SERVER:
G> The server is located on any computer on the local subnet listen for
G> a
G> UDP message on a specified address (port 10000). If it recieves a
G> "HELLO" message it will broadcast a message to the subnet saying
G> "HERE
G> I AM" (on port 10001). In that way the client and all other clients
G> know where the server is located.
G> CLIENTS:
G> Clients can be started on any computer located in a subnet. This
G> client
G> don't know if and where the server is located so it begins sending a
G> UDP message to all computers on a subnet (on port 10000). It also
G> listen for UDP messages (on port 10001). When it recieves a message
G> on
G> port 10001 it know where the server is located, even if it have not
G> asked about it.
G> The UDP port 10000 is for the server - listen for clients messages
G> The UDP port 10001 is for the clients - listen for server messages.
G>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsch
 
I have now found out how I can do it.
Just to show what my solution is.
(no comments is available . this is just a raw working program)


using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

class Program
{
public static bool done=false;

static void Main(string[] args)
{
Thread listenThread = new Thread(new
ThreadStart(Program.DoListen));
Thread serverThread = new Thread(new
ThreadStart(Program.DoServer));
listenThread.Start();
serverThread.Start();

Console.ReadKey();
done = true;
}


/// <summary>
///
/// </summary>
public static void DoListen()
{
bool done = false;
UdpClient listener = new UdpClient(11000);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, 11000);

//Console.WriteLine("CLIENT: Waiting for broadcast");
while(!done)
{
try
{
byte[] bytes = listener.Receive(ref groupEP);
Console.WriteLine("CLIENT: From {0} - {1}\n",
groupEP.ToString(), Encoding.ASCII.GetString(bytes, 0, bytes.Length));
} catch(Exception e) { Console.WriteLine("CLIENT:
"+e.ToString()); }
}
listener.Close();
}


/// <summary>
///
/// </summary>
public static void DoServer()
{
String strHostName = Dns.GetHostName();
//Console.WriteLine("SERVER: Local Machine's Host Name: " +
strHostName);
IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
IPAddress[] addr = ipEntry.AddressList;

while(!done)
{
for(int i = 0; i < addr.Length; i++)
{
string IPAdd = addr.ToString();
string[] IPAddresses = IPAdd.Split(new Char[] { '.' });
string IPAddr1 = IPAddresses[0];
string IPAddr2 = IPAddresses[1];
string IPAddr3 = IPAddresses[2];
// Console.WriteLine("SERVER: IP Address {0} ", IPAdd);
for(int a = 1; a <= 4; a++)
{
string IP = "255.255.255.255";
if(a == 2) IP = IPAddr1 + ".255.255.255";
if(a == 3) IP = IPAddr1 + "." + IPAddr2 + ".255.255";
if(a == 4) IP = IPAddr1 + "." + IPAddr2 + "." + IPAddr3 +
".255";
try
{
Socket s = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
IPAddress broadcast = IPAddress.Parse(IP);
byte[] sendbuf = Encoding.ASCII.GetBytes("HELLO FROM " +
IPAdd + " subnet " + IP);
IPEndPoint ep = new IPEndPoint(broadcast, 11000);
s.SendTo(sendbuf, ep);
s.Close();
Thread.Sleep(1000);
} catch { };
}
}
}
}




}
 
Back
Top