SuperSocket Quick Start

This guide will introduce how to create a simple command line socket application using SupperSocket application framework.

1. Create a new project named “EchoService” in a new empty solution
2. Add the projects “Common”, and "SocketBase" (which are belong to SuperSocket) into the solution, and then let the project “EchoService” reference them
3. Add session class and server class
EchoSession.cs
http://supersocket.codeplex.com/SourceControl/changeset/view/62405#1286433

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.SocketBase;

namespace EchoService
{
    public class EchoSession : AppSession<EchoSession>
    {
        public override void StartSession()
        {
            SendResponse("Welcome to EchoServer!");
        }

        public override void HandleExceptionalError(Exception e)
        {
            SendResponse("Server side error occurred!");
        }
    }
}

EchoServer.cs
http://supersocket.codeplex.com/SourceControl/changeset/view/62405#1286478

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.SocketBase;

namespace EchoService
{
    public class EchoServer : AppServer<EchoSession>
    {

    }
}



4. Add command class
ECHO.cs
http://supersocket.codeplex.com/SourceControl/changeset/view/62405#1286382

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.SocketBase.Command;

namespace EchoService.Command
{
    public class ECHO : StringCommandBase<EchoSession>
    {
        #region CommandBase<EchoSession> Members

        public override void ExecuteCommand(EchoSession session, StringCommandInfo commandInfo)
        {
            session.SendResponse(commandInfo.Data);
        }

        #endregion
    }
}


5. Build the project “EchoService” and then copy outputted assemblies to the output dir of the project “SocketService” (provided by SupperSocket, build it in advance please)

6. Update app.config of SocketService to use EchoService
SuperSocket.SocketService.exe.config
http://supersocket.codeplex.com/SourceControl/changeset/view/62405#1286366

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="socketServer" type="SuperSocket.SocketEngine.Configuration.SocketServiceConfig, SuperSocket.SocketEngine" />
  </configSections>
  <appSettings>
    <add key="ServiceName" value="EchoService" />
  </appSettings>
  <connectionStrings/>
  <socketServer>
    <servers>
      <server name="Default Echo Server" serviceName="EchoService" ip="Any" port="911" mode="Async" />
    </servers>
    <services>
      <service name="EchoService" type="EchoService.EchoServer, EchoService" />
    </services>
  </socketServer>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
  </startup>
</configuration>


Please note the "socketServer" node in above code.

7. Click “RunServer.bat” and then enter “start” to start the server, then you can test the echo server by telnet (you also can click "InstallService.bat" to intsall the server application as windows service, the service name is defined in appSetting of app.config with the key "ServiceName")


More samples in QuickStart have been included into source code of SuperSocket:
http://supersocket.codeplex.com/releases/view/59310

Last edited Feb 9, 2011 at 8:58 AM by kerryjiang, version 6

Comments

No comments yet.