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

No comments :

Post a Comment