Click here to Skip to main content
15,886,362 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;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

    public class XML_access
    {
        /// <summary>
        /// simple sample of call
        /// XMLSerializeObject("samplei.xml",i,typeof(OrderedItem));
        /// sample of call for ArrayList
        /// XMLSerializeObject("sampleal.xml",(OrderedItem[])al.ToArray(typeof(OrderedItem)),typeof(OrderedItem[]));
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="obj"></param>
        /// <param name="typeof_object"></param>
        public static void XMLSerializeObject(string filename,object obj,System.Type typeof_object)
        {
            Stream fs=null;
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof_object);
                // Create an XmlTextWriter using a FileStream.
                fs = new FileStream(filename, FileMode.Create,System.IO.FileAccess.ReadWrite );
                XmlWriter writer = new XmlTextWriter(fs,System.Text.Encoding.Unicode);
                // Serialize using the XmlTextWriter.
                serializer.Serialize(writer, obj);
                writer.Close();
                fs.Close();
            }
            catch(Exception e)
            {
                if (fs!=null)
                    fs.Close();
                System.Windows.Forms.MessageBox.Show( e.Message,"Error",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Error);
            }
        }
        /// <summary>
        /// simple sample of call
        /// OrderedItem i=(OrderedItem)XMLDeserializeObject("simple.xml",typeof(OrderedItem));
        /// sample of call for ArrayList
        /// System.Collections.ArrayList al=new System.Collections.ArrayList((OrderedItem[])XMLDeserializeObject("sampleal.xml",typeof(OrderedItem[])));
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="typeof_object"></param>
        /// <returns></returns>
        public static object XMLDeserializeObject(string filename,System.Type typeof_object)
        {   
            // Create an instance of the XmlSerializer specifying type and namespace.
            XmlSerializer serializer = new XmlSerializer(typeof_object);
            // A FileStream is needed to read the XML document.
            FileStream fs = new FileStream(filename, FileMode.Open);
            XmlReader reader = new XmlTextReader(fs);
            // Declare an object variable of the type to be deserialized.
            object obj;
            // Use the Deserialize method to restore the object's state.
            obj = serializer.Deserialize(reader);
            reader.Close();
            fs.Close();
            return obj;
        }
    }

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