Showing posts with label broadcasting. Show all posts
Showing posts with label broadcasting. Show all posts

Thursday, December 16, 2010

Network Broadcasting with C# further

This post is related to the previously written post Network Broadcasting with C#.

Consider the Broadcst.cs and RecvBroadcst.cs programs which were provided in Network Broadcasting with C#. In this post those two programs have slightly modified to achieve a special target described in the next paragraph.

Suppose a peer to peer environment. When a peer connected with the network it continually listens to one specific port and as well sends a broadcasting message in every 5 seconds to the same specific port to inform it is connected. That broadcasting message will be listened through the modified RecvBroadcst.cs program and it will also store the ip of the sender in an Arraylist.Broadcast message is sent through modified Broadcst.cs program. Program.cs is used to run the two threads.


RecvBroadcst.cs

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Collections;

class RecvBroadcst
{
    public void init()
    {

        while (true)
        {
            Socket sock = new Socket(AddressFamily.InterNetwork,
                    SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
            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());

            string ip = ep.ToString();

            string[] array = ip.Split(':');

            ArrayList list = new ArrayList();

            Console.WriteLine("sender ip {0} is added to the table!", array[0]);

            list.Add(array[0]);

            sock.Close();

        }
    }
}

Broadcst.cs

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

class Broadcst
{
    public void init()
    {

        while (true)
        {
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
                         ProtocolType.Udp);
            IPEndPoint iep1 = new IPEndPoint(IPAddress.Broadcast, 9050);
            string hostname = Dns.GetHostName();
            byte[] data = Encoding.ASCII.GetBytes(hostname);
            sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
            sock.SendTo(data, iep1);
                       
            sock.Close();
            Thread.Sleep(5000);
            
        }
    }
}



Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Threading;


    public class Program
    {
        static void Main(string[] args)
        {
           
            Thread RecvBraodcast = new Thread(new RecvBroadcst().init);
            Thread Broadcst = new Thread(new Broadcst().init);

            RecvBraodcast.Start();
            Broadcst.Start();
      
            

        }
    }

           

           


Run these codes in visual studio.

output

Tuesday, December 14, 2010

Network Broadcasting with C#

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.

Broadcst.cs

The Broadcst program creates two separate IPEndPoint objects, one using the IPAddress.Broadcast address and one using a local broadcast address 192.168.1.255. Next, the local hostname of the device is determined using the standard Dns GetHostName() method, and it is sent out in two separate broadcast messages.

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class Broadcst
{
  public static void Main()
  {
   Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
                ProtocolType.Udp);
   IPEndPoint iep1 = new IPEndPoint(IPAddress.Broadcast, 9050);
   IPEndPoint iep2 = new IPEndPoint(IPAddress.Parse("192.168.1.255"), 9050);
   string hostname = Dns.GetHostName();
   byte[] data = Encoding.ASCII.GetBytes(hostname);
   sock.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.Broadcast, 1);
   sock.SendTo(data, iep1);
   sock.SendTo(data, iep2);
   sock.Close();
  }
}


RecvBroadcst.cs

The RecvBroadcst program is a simple UDP program that creates a socket and binds it to all network interfaces on the device, using port 9050, the same port that the Broadcst program uses to send its broadcast messages. After binding the socket to the UDP port, the program attempts to receive two messages sent to the port and displays the message and the sending host address information.

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class RecvBroadcst
{
  public static void Main()
  {
   Socket sock = new Socket(AddressFamily.InterNetwork,
           SocketType.Dgram, ProtocolType.Udp);
   IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
   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();
  }
}


For compilation either can use visual studio or visual studio command prompt. Here have used visual studio command prompt. Open up two Visual Studio Command prompts, one for the RecvBroadcst.cs and other one for the Broadcst.cs

Compile and run:

Broadcst

csc Broadcst.cs
Broadcst.exe

RecvBroadcst

csc RecvBroadcst.cs
RecvBroadcst.exe

output