Click here to Skip to main content
15,892,927 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 216.8K   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;
using System.Text;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential)]
public struct SecurityAttributes
{
    public uint nLegnth;//Specifies the size, in bytes, of this structure. Set this value to the size of the SECURITY_ATTRIBUTES structure.
    public int lpSecurityDescriptor;//Pointer to a security descriptor for the object that controls the sharing of it. If NULL is specified for this member, the object is assigned the default security descriptor of the calling process.
    public bool bInheritHandle;//Specifies whether the returned handle is inherited when a new process is created. If this member is TRUE, the new process inherits the handle.
}

public class Semaphore
{
    public const uint INFINITE = 0xFFFFFFFF;
    public const int WAIT_ABANDONED = 128;
    public const int WAIT_OBJECT_0 = 0;
    public const int WAIT_TIMEOUT = 258;

    private const uint STANDARD_RIGHTS_REQUIRED=0x000F0000;
    public enum DESIRED_ACCESS:uint
    {
        SYNCHRONIZE=0x00100000,// Windows NT/2000/XP: Enables use of the semaphore handle in any of the wait functions to wait for the semaphore's state to be signaled. 
        SEMAPHORE_MODIFY_STATE=0x0002, // Enables use of the semaphore handle in the ReleaseSemaphore function to modify the semaphore's count. 
        SEMAPHORE_ALL_ACCESS=STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE|0x3// Specifies all possible access flags for the semaphore object. 
    }

    [DllImport("kernel32",EntryPoint="CreateSemaphore",SetLastError=true,CharSet=CharSet.Unicode)]
    public static extern uint CreateSemaphore(
        ref SecurityAttributes lpSemaphoreAttributes, // SD //If lpSemaphoreAttributes is null, the semaphore gets a default security descriptor
        int lInitialCount,                          // initial count
        int lMaximumCount,                          // maximum count
        string lpName                               // object name
        );
    public static uint CreateSemaphore(
        int lInitialCount,                          // initial count
        int lMaximumCount,                          // maximum count
        string lpName                               // object name
        )
    {
        SecurityAttributes sa=new SecurityAttributes();
        return CreateSemaphore(ref sa,lInitialCount,lMaximumCount,lpName);
    }

    [DllImport("kernel32",EntryPoint="OpenSemaphore",SetLastError=true,CharSet=CharSet.Unicode)]
    public static extern uint OpenSemaphore(
                                         uint dwDesiredAccess,  // access
                                         bool bInheritHandle,    // inheritance option
                                         string lpName          // object name
                                         );

    [DllImport("kernel32",EntryPoint="ReleaseSemaphore",SetLastError=true,CharSet=CharSet.Unicode)]
    public static extern int ReleaseSemaphore(
        UInt32 hSemaphore,       // handle to semaphore
        Int32 lReleaseCount,      // count increment amount
        ref UInt32 lpPreviousCount   // previous count
        );

    public static int ReleaseSemaphore(
        uint hSemaphore,       // handle to semaphore
        int lReleaseCount)      // count increment amount
    {
        UInt32 lpPreviousCount=0;
        return ReleaseSemaphore(hSemaphore,lReleaseCount,ref lpPreviousCount);
    }

    [DllImport("kernel32",EntryPoint="CloseHandle",SetLastError=true,CharSet=CharSet.Unicode)]
    public static extern int CloseHandle(uint hHandle);

    [DllImport("kernel32",EntryPoint="WaitForSingleObject",SetLastError=true,CharSet=CharSet.Unicode)]
    public static extern uint WaitForSingleObject(uint hHandle, uint dwMilliseconds);
}

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