tags:

views:

60

answers:

2

Ok in order to broadcast, I have created a socket:

 notifySock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

And to send the hostname of my computer to all other computers connected to the same lan, I am using the send(Byte[] buffer) method:

 notifySock.Send(hostBuffer);

hostBuffer contains the hostname of my computer.

However because I am using a 'datagram' socket-type do I need to format the data I need to send. If possible please provide the code that I must put in between the two lines of code I have entered to create a socket and send the data.

+1  A: 

For broadcast from a user application, UDP is typically used. You need to design a suitable protocol, i.e. a way to format the information you want to send into the UDP packet.

unwind
ok I got that. Am using a datagram socketType for which UDP works fine.I have a small doubt now.Between creating the socket of type datagram and sending the data, what is the code I need to write to broadcast the message.Creating an IPEndPoint of type broadcast is fine but how do I use it??
Avik
A: 

In your example you haven't specified who you are sending to. You need something like:

UdpClient notifySock = new UdpClient(endPoint);  
notifySock.Send(buffer, buffer.Length, new IPEndPoint(IPAddress.Broadcast, 1234));

For the other hosts on your LAN to receive that they have to be listening on UDP port 1234.

sipwiz
Got that.. The new problem here is that there is a receive method within the UDPClient class.. However one of its parameters is a reference variable of type IPEndPoint..But if I need to hear a broadcast message, I do not need that..What is the method I must call to receive a broadcasted message.
Avik