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 project “SocketServiceCore”, and "Common" (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/57008#1123145

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

namespace EchoService
{
    public class EchoSession : AppSession<EchoSession>
    {

        protected override void OnClosed()
        {
            
        }

        public override void SayWelcome()
        {
            SendResponse("Welcome to EchoServer!");
        }

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

EchoServer.cs
http://supersocket.codeplex.com/SourceControl/changeset/view/57008#1123146

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

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

    }
}



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

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

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

        public void Execute(EchoSession session, CommandInfo commandData)
        {
            session.SendResponse(commandData.Param);
        }

        #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/57023#1267358

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<configSections>
		<section name="loggingConfiguration"
type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging" />
		<section name="socketServer"
type="SuperSocket.SocketServiceCore.Configuration.SocketServiceConfig, SuperSocket.SocketServiceCore"/>
	</configSections>
	<appSettings>
    <add key="ServiceName" value="EchoService"/>
  </appSettings>
	<socketServer>
		<servers>
		  <server name="Default Echo Server" serviceName="EchoService" ip="Any" port="911" mode="Async" />
		</servers>
		<services>
		  <service serviceName="EchoService" type="EchoService.EchoServer, EchoService" />
		</services>
	</socketServer>	
	<loggingConfiguration configSource="config\logging.config" />
</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")


The QuickStart's code has been included into source code of SuperSocket:

Last edited Oct 29, 2010 at 3:50 PM by kerryjiang, version 3

Comments

No comments yet.