Socket packet and port test tool development (source code)

Source: Internet
Author: User
Tags string format log4net

Http://www.cnblogs.com/productivity/archive/2013/01/26/2878047.html


Because they often do socket development, often to debug and maintain a number of server-side and client communications, packet data, etc., online tools are simple function, with the bad, so always want to write one of their own.

Socket test tool has downloaded 822 times Socket Test tool source has been downloaded 894 times

Not busy at the end of the year, finally wrote a, provided to everyone to use, the source code can be casually used and modified, welcome to give more advice, make this tool easier to use, convenient socket programming development mermaid. The main functions are as follows:

1. Set up socket test server and test client, and send or receive message data to other side, support automatic sending and automatic reply, support UDP and TCP;

2. Input IP address and port parameter data for local XML serialization, the next time automatically open. (This is what I need, not every time to input a variety of IP address port);

3. Receive or send the message data, can be stored directly in the log file, facilitate off-line analysis.

4. Server-side, you can view the access to the various connection information;

5. Support for ASCII and 16 data send and receive display.

Because the interface should support both TCP and UDP data communication, two interfaces Iserver and IClient are written, which facilitates the separation of interface and communication layer.

Public interface Iserver
    {
        //Initialize
        void Init (string serverip, int port);
        Sends data
        void Send (String Connid, byte[], int length) to a connection from the server side;
        Monitor
        int Listen ();
        Get the current connection
        list<iconnection> getconnectionlist ();
        Socket event
        Receivedhandler ondatareceived;
        Event Socketerrorhandler Onsocketerror;

        void Close ();
    }

In order to save the parameter data, a Socketinfo class is constructed, corresponding to the client and server side, which is then serialized into an XML file.

The serialized code is as follows:

[Serializable]
    public class Socketinfo
    {public
        string Name {get; set;}
        Server-side or client-type public
        string type {get; set;}
        16 in binary format or ASCII public
        string format {get; set;}

        public string ServerIP {get; set;}

        public int Port {get; set;}
        TCP or UDP public
        string Protocol {get; set;}
        Message data Public
        string: {Get;set}
        Whether to automatically send or receive data public
        Boolean Isauto {get;set;}

        Public Socketinfo ()
        {
            Format = "AscII";
            Protocol = "TCP";
            Port = 8890;
            ServerIP = "127.0.0.1";
            data = "Please input test date";
        }
    

public class Myserializer {public static void serialize<t> (T value, String xmlfilename)
            {if (value = = null) {return;
            } XmlSerializer Serializer = new XmlSerializer (typeof (T));
            XmlWriterSettings settings = new XmlWriterSettings (); Settings.
            Encoding = new UnicodeEncoding (false, false); Settings.
            Indent = false; Settings.
            Omitxmldeclaration = false; FileStream fs = new FileStream (XMLfileName, FileMode.

            OpenOrCreate); Serializer.
            Serialize (fs, value); Fs.
        Close (); public static T deserialize<t> (string xmlfilename) {if (string).
            IsNullOrEmpty (XMLfileName)) {return default (T);
            } XmlSerializer Serializer = new XmlSerializer (typeof (T)); XmlSerializer serializer = new XmlSerializer (typeof (ARraylist));
            XmlReaderSettings settings = new XmlReaderSettings ();

            Settings.
            FileStream fs = null;

                try {fs = new FileStream (XMLfileName, FileMode.Open); Deserialize the content of the XML file to a contacts array//utilizing XMLReader XMLR
                Eader reader = new XmlTextReader (FS); t contacts = (t) serializer.

                Deserialize (reader);
            return contacts;
            The catch (FileNotFoundException) {/Do No if the file does not exists finally {if (fs!= null) fs.
            Close ();
        return default (T); }
    }

The client's message and server-side message data are stored in the Client.log and Server.log two files. The main is to use the Log4net configuration implementation.

<log4net> <root> <level value= ' all '/> T;appender-ref ref= "Rollingfileappender"/> </root> <appender name= "Clientlogfileappender" Typ E= "Log4net. Appender.rollingfileappender "> <param name=" File "value=" Client.log "/> <lockingmodel t Ype= "Log4net. Appender.fileappender+minimallock "/> <appendtofile value=" true "/> <rollingstyle
            E= "Size"/> <maxsizerollbackups value= "3"/> <maximumfilesize value= "2MB"/> <staticlogfilename value= "true"/> <layout type= "log4net. Layout.patternlayout "> <conversionpattern value=" record time:%date log:%message%newline "/> & lt;/layout> </appender> <logger name= "Sockettool.clientform" > <level value= "DEBUG"/> 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.