Click here to Skip to main content
15,897,704 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 217.4K   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.IO;

public class file_access
{

    public static string read(string file_name)
    {
        try
        {
            string        data;

            FileStream fs=new FileStream(file_name,FileMode.Open ,FileAccess.Read,FileShare.ReadWrite );
            StreamReader sr=new StreamReader(fs,System.Text.Encoding.Default);
            data=sr.ReadToEnd();
            sr.Close();
            fs.Close();
            return data;
        }
        catch(Exception e)
        {
            System.Windows.Forms.MessageBox.Show(e.Message,"Error",
                System.Windows.Forms.MessageBoxButtons.OK,
                System.Windows.Forms.MessageBoxIcon.Error);
            return "";
        }
    }

    public static byte[] read_binary(string file_name)
    {
        try
        {
            FileStream    fs;
            long        file_size;

            fs = new FileStream(file_name, FileMode.Open ,FileAccess.Read,FileShare.ReadWrite );
            file_size = fs.Length;
          
            byte[] buffer = new byte[(int)file_size];
            fs.Read(buffer, 0, (int)file_size);
            fs.Close();
            return buffer;
        }
        catch(Exception e)
        {
            System.Windows.Forms.MessageBox.Show(e.Message,"Error",
                System.Windows.Forms.MessageBoxButtons.OK,
                System.Windows.Forms.MessageBoxIcon.Error);
            return null;
        }
    }

    public static void write_binary(string file_name,byte[] buffer)
    {
        try
        {
            write_binary(file_name,buffer,false);
        }
        catch(Exception e)
        {
            System.Windows.Forms.MessageBox.Show(e.Message,"Error",
                System.Windows.Forms.MessageBoxButtons.OK,
                System.Windows.Forms.MessageBoxIcon.Error);
        }
    }

    public static void write_binary(string file_name,byte[] buffer,bool append)
    {
        try
        {
            FileStream        fs;

            if (append)
            {
                fs=new FileStream(file_name,FileMode.Append ,FileAccess.Write,FileShare.ReadWrite);
            }
            else
            {
                if (File.Exists(file_name)) File.Delete(file_name);
                fs=new FileStream(file_name,FileMode.OpenOrCreate ,FileAccess.Write,FileShare.ReadWrite);
            }
            fs.Write(buffer,0,buffer.Length);
            fs.Close();
        }
        catch(Exception e)
        {
            System.Windows.Forms.MessageBox.Show(e.Message,"Error",
                System.Windows.Forms.MessageBoxButtons.OK,
                System.Windows.Forms.MessageBoxIcon.Error);
        }
    }

    public static void write_binary_at(string file_name,byte[] buffer,long position)
    {
        try
        {
            FileStream        fs;

            fs=new FileStream(file_name,FileMode.OpenOrCreate ,FileAccess.Write,FileShare.ReadWrite);
            fs.Seek(position,System.IO.SeekOrigin.Begin);
            fs.Write(buffer,0,buffer.Length);
            fs.Close();
        }
        catch(Exception e)
        {
            System.Windows.Forms.MessageBox.Show(e.Message,"Error",
                System.Windows.Forms.MessageBoxButtons.OK,
                System.Windows.Forms.MessageBoxIcon.Error);
        }
    }


    public static void write(string file_name,string buffer)
    {
        try
        {
            write(file_name,buffer,false);
        }
        catch(Exception e)
        {
            System.Windows.Forms.MessageBox.Show(e.Message,"Error",
                System.Windows.Forms.MessageBoxButtons.OK,
                System.Windows.Forms.MessageBoxIcon.Error);
        }
    }

    public static void write(string file_name,string buffer,bool append)
    {
        try
        {
            FileStream        fs;

            if (append)
            {
                fs=new FileStream(file_name,FileMode.Append ,FileAccess.Write,FileShare.ReadWrite);
            }
            else
            {
                if (File.Exists(file_name)) File.Delete(file_name);
                fs=new FileStream(file_name,FileMode.OpenOrCreate ,FileAccess.Write,FileShare.ReadWrite);
            }
            StreamWriter sw=new StreamWriter(fs);
            sw.Write(buffer);
            sw.Close();
            fs.Close();
        }
        catch(Exception e)
        {
            System.Windows.Forms.MessageBox.Show(e.Message,"Error",
                System.Windows.Forms.MessageBoxButtons.OK,
                System.Windows.Forms.MessageBoxIcon.Error);
        }
    }
}

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