Overview

The following document provides a quick tutorial for setting up and using Nina in a project. The tutorial will show you how to create a client and server which say “Hello” to each other.

Prerequisites

 

Steps

1. Start Visual Studio 2010 and create a new Console Application.

image

2. Copy ‘Nina.dll’ to the project folder.

image

3. Add a reference to the DLL by right-clicking the project in Visual Studio and selecting ‘Add Reference’. Navigate to the ‘Browse’ tab of the window that appears and select Nina.dll.

image

4. Create a protocol handler for the server by creating a new class named ‘ServerProtocolHandler’. The following code will be the protocol handler.

Note: Make sure to add any imports required (such as the Nina namespace).

 

    /// <summary>
    /// Protocol handler for the server
    /// </summary>
    class ServerProtocolHandler : NinaProtocolHandler
    {
        /// <summary>
        /// Called when a message is received from a client
        /// </summary>
        /// <param name="s">NinaSession from which the message was received</param>
        /// <param name="data">The data received</param>
        public override void MessageReceived(NinaSession s, byte[] data)
        {
            /* Print out received message */
            string message = Encoding.UTF8.GetString(data);
            Console.WriteLine("SERVER INFO: Received message '" + message + "'");
            /* Respond to message */
            s.Write(Encoding.UTF8.GetBytes("Hello client!"));
        }
        /// <summary>
        /// Called when a session is closed
        /// </summary>
        /// <param name="s">The session which was closed</param>
        public override void SessionClosed(NinaSession s)
        {
            Console.WriteLine("SERVER INFO: Connection closed");
        }
        /// <summary>
        /// Called when a session is opened
        /// </summary>
        /// <param name="s">The session which was opened</param>
        public override void SessionOpened(NinaSession s)
        {
            Console.WriteLine("SERVER INFO: Connection opened");
        }
    }

5. Create a protocol handler for the client by creating a new class named ‘ClientProtocolHandler’. The following code will be the protocol handler:

    /// <summary>
    /// Protocol handler for client
    /// </summary>
    class ClientProtocolHandler : NinaProtocolHandler
    {
        /// <summary>
        /// Called when a message is received from the server
        /// </summary>
        /// <param name="s">NinaSession from which the message was received</param>
        /// <param name="data">The data received</param>
        public override void MessageReceived(NinaSession s, byte[] data)
        {
            /* Print out received message */
            string message = Encoding.UTF8.GetString(data);
            Console.WriteLine("CLIENT INFO: Received message '" + message + "'");
        }
        /// <summary>
        /// Called when the session is closed
        /// </summary>
        /// <param name="s">The session which was closed</param>
        public override void SessionClosed(NinaSession s)
        {
            Console.WriteLine("CLIENT INFO: Connection closed");
        }
        /// <summary>
        /// Called when the session is opened
        /// </summary>
        /// <param name="s">The session which was opened</param>
        public override void SessionOpened(NinaSession s)
        {
            Console.WriteLine("CLIENT INFO: Connection opened");
            /* Say hello to the server */
            s.Write(Encoding.UTF8.GetBytes("Hello server!"));
        }
    }

6. Finally edit Program.cs and create your client/server in the main method.

        static void Main(string[] args)
        {
            /* Start server */
            NinaSocketAcceptor acceptor = new NinaSocketAcceptor();
            acceptor.Handler = new ServerProtocolHandler();
            acceptor.ProtocolCodec = new DelimiterCodec('\n', Encoding.UTF8);
            acceptor.Bind(7001);
            /* Sleep briefly to allow the server to start up */
            try
            {
                Thread.Sleep(2000);
            }
            catch { }
            /* Start client */
            NinaSocketConnector connector = new NinaSocketConnector();
            connector.Handler = new ClientProtocolHandler();
            connector.ProtocolCodec = new DelimiterCodec('\n', Encoding.UTF8);
            connector.Connect(NinaSocket.GetHostIp(), 7001);
        }

7. Build/Run the project and you’re done Smile

Last edited Apr 20, 2011 at 2:33 PM by tcashman, version 5

Comments

No comments yet.