Click here to Skip to main content
15,399,212 members
Articles / Programming Languages / C#
Posted 21 Nov 2007


41 bookmarked

Broadcasting Using Socket-Oriented Approach

Rate me:
Please Sign up or sign in to vote.
3.05/5 (11 votes)
21 Nov 2007CPOL2 min read
This solution explains a socket approach to send a message (broadcast message ) to clients in the same VLAN


This solution explains a socket approach to sending a message (broadcast message) to clients in the same VLAN (Virtual Lan). If broadcasting is needed for the whole LAN (over all VLANs) in the same local area network then you have to use Remoting instead of Sockets.

How To Send Message to Connected LAN Clients Using Broadcasting

The application consists of two programs "Clfrm" and "SERVfrm." Clfrm exists on all LAN computers and SERVfrm exists on the server.

When the time is 12:00 AM the SERVfrm will send a broadCast message to all connected clients (Clfrm). The Clfrm will receive the message and run an alert form to notify the user.

Using the Code

I use the following namespace to do my job:

  • System.Net
  • System.Net.Sockets

The time was an important factor, as was having no delay when sending/receiving a message from the server to all clients. So, I used sockets instead Of remoting (remoting may cause a simple delay).

Screenshot - Muneer.jpg

The Server Code

// Timer to Check Server Time
private void timer1_Tick(object sender, System.EventArgs e)
    // Display Server Time
    label1.Text = System.DateTime.Now.ToLongTimeString(); 

The timer here is to get the server time.

if(System.DateTime.Now.Hour == 11 && System.DateTime.Now.Minute==59 && 

Check if the time is 11:59:59.

    /* Define a socket
    Dgram Socket :  Provides datagrams, which are connectionless messages
    of a fixed maximum length. 
    This type of socket is generally used for short messages, such as a name
    server or time server, since the order and reliability of message 
    delivery is not guaranteed

    AddressFamily.InterNetwork indicates that an IP version 4 addresses
    is expected when a socket connects to an end point. */ 
    Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 
        ProtocolType.Udp); // Protocol Type is UDP 
    //Get the IP Broadcast address , Use The Port 9050 
    IPEndPoint iep1 = new IPEndPoint(IPAddress.Broadcast, 9050); 
    //IP Broadcast Range using Port 9050 
    IPEndPoint iep2 = new IPEndPoint(IPAddress.Parse(""),9050);
    byte[]   data = Encoding.ASCII.GetBytes("12");    // The Data To Sent /*
    Set Socket Options  --->  SocketOptionLevel
        Member name    Description

        IP        Socket options apply only to IP sockets. 
        IPv6        Socket options apply only to IPv6 sockets. 
        Socket        Socket options apply to all sockets. 
        Tcp        Socket options apply only to TCP sockets. 
        Udp        Socket options apply only to UDP sockets. 
        //SocketOptionName.Broadcast : to enable broadcast
            SocketOptionName.Broadcast, 1);  
    sock.SendTo(data, iep1);
    sock.SendTo(data, iep2);


In the first run of the client application the firewall will show the below message. When The Clfrm runs on the client machine it will not appear in the desktop or task manager Application tab.

I put ShowInaskbar = false in 'Clfrm' which will hide the 'Clfrm' from the Task Manager Application Tab (it will appear in the process tab), and made Opacity = 2

Screenshot - Unlo.jpg

Press unlock to allow the client to use 9050 port to receive messages from the server.

The Client Code

public  string  TI ="0";
string  stringData ;

This timer will check the value of stringData

private void timer1_Tick(object sender, System.EventArgs e)
    Socket sock = new Socket(AddressFamily.InterNetwork,SocketType.Dgram, 

    The Socket.Bind method uses the Any field to indicate that a Socket
    instance must listen for client activity on all network interfaces.
    The Any field is equivalent to in dotted-quad notation.
    IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
    //Associates a Socket with a local endpoint EndPoint ep = (EndPoint)iep;

    byte[]  data = new byte[1024];
    int  recv = sock.ReceiveFrom(data, ref ep);// Receive Data 

    stringData = Encoding.ASCII.GetString(data, 0, recv);        


private void timer2_Tick(object sender, System.EventArgs e)
    if(TI == "12")
        timer2.Interval = 3000;
        TI = "0";    
        stringData = "0";                
        Process.Start(Application.StartupPath+"\\ALER.exe"); // Run ALER.exe 
        timer2.Interval =400;

The sock.Close(); will close the port 9050 for future use.

Why Using Sockets instead of Remoting

  • If you need bidirectional communication and if the data is a small fragment use sockets
  • If you want the best performance
  • When Client is waiting for notifications/events from server
  • Remoting is a higher layer on Sockets. When both client and server is under your control you can use Remoting


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Muneer Safi
Software Developer Al mouakhah lil khadamat al logesteih wa al itisal
Jordan Jordan
No Biography provided

Comments and Discussions

Generalthanx Pin
tejinder gill3-Apr-11 8:53
Membertejinder gill3-Apr-11 8:53 
GeneralRe: thanx Pin
Muneer Safi3-Apr-11 21:41
MemberMuneer Safi3-Apr-11 21:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.