Click here to Skip to main content
15,886,830 members
Articles / Programming Languages / C#

Network Stuff (easy socket v3)

Rate me:
Please Sign up or sign in to vote.
4.87/5 (52 votes)
2 Jun 2005 215.9K   6.1K   137  
A class with event handlers for TCP, UDP or ICMP sockets; includes ping, traceroute, whois, ARP, and IPHelper functions and raw packets forging/ capturing.
/*
Copyright (C) 2004 Jacquelin POTIER <jacquelin.potier@free.fr>
Dynamic aspect ratio code Copyright (C) 2004 Jacquelin POTIER <jacquelin.potier@free.fr>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
using System;
namespace easy_socket
{
    /// <summary>
    /// Summary description for wake_on_lan.
    /// </summary>
    public class Cwake_on_lan
    {
        private System.Threading.AutoResetEvent evt_error=new System.Threading.AutoResetEvent(false);
        private System.Threading.AutoResetEvent evt_success=new System.Threading.AutoResetEvent(false);

        private void Socket_Data_Error_EventHandler(easy_socket.udp.Socket_Data sender, easy_socket.udp.EventArgs_Exception e)
        {
            System.Windows.Forms.MessageBox.Show("Error sending WOL packet:\r\n"+e.exception.Message,"Error",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Error);
            this.evt_error.Set();
        }
        private void Socket_Data_Send_Completed_EventHandler(easy_socket.udp.Socket_Data sender, EventArgs e)
        {
            System.Windows.Forms.MessageBox.Show("WOL packet successfully sent","Information",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Information);
            this.evt_success.Set();
        }

        public void wake_on_lan(string mac_addr,string ip,int port)
        {
            mac_addr=mac_addr.Replace("-","");
            mac_addr=mac_addr.Replace(".","");
            mac_addr=mac_addr.Replace(":","");

            easy_socket.udp.Socket_Data s=new easy_socket.udp.Socket_Data(ip,port);
            s.event_Socket_Data_Error+=new easy_socket.udp.Socket_Data_Error_EventHandler(Socket_Data_Error_EventHandler);
            s.event_Socket_Data_Send_Completed+=new easy_socket.udp.Socket_Data_Send_Completed_EventHandler(Socket_Data_Send_Completed_EventHandler);
            s.allow_broadcast=true;
/*
 * AMD Publication# 20213
If the IEEE address for a particular node on the network
was 11h 22h 33h 44h 55h 66h, then the LAN controller
would be scanning for the data sequence (assuming an
Ethernet Frame):
DESTINATION SOURCE MISC FF FF FF FF FF
FF 11 22 33 44 55 66 11 22 33 44 55 66 11 22 33 44
55 66 11 22 33 44 55 66 11 22 33 44 55 66 11 22 33
44 55 66 11 22 33 44 55 66 11 22 33 44 55 66 11 22
33 44 55 66 11 22 33 44 55 66 11 22 33 44 55 66 11
22 33 44 55 66 11 22 33 44 55 66 11 22 33 44 55 66
11 22 33 44 55 66 11 22 33 44 55 66 MISC CRC 
*/
            byte[] bytes=new byte[6+6*16];
            //first 6 bytes should be 0xff
            for(int cpt=0;cpt<6;cpt++)
                bytes[cpt]=0xff;
            //now repeate MAC 16 times
            for(int cpt=0;cpt<16;cpt++)
            {
                for(int cpt2=0;cpt2<6;cpt2++)
                {
                    bytes[6+cpt*6+cpt2]=byte.Parse(mac_addr.Substring(2*cpt2,2),
                                        System.Globalization.NumberStyles.HexNumber);
                }
            }
            s.send(bytes);
            // wait for multiple handles (error and send completed)
            System.Threading.WaitHandle[] waithandles=new System.Threading.WaitHandle[2];
            waithandles[0]=evt_success;
            waithandles[1]=evt_error;
            System.Threading.WaitHandle.WaitAny(waithandles,10000,true);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
France France

Comments and Discussions