This project is read-only.

Multiple server instances, and interactions among them

SuperSocket support running multiple server instances with different listening ip and port in the same program, by adding two server nodes in the configuration file:
<?xml version="1.0" encoding="utf-8" ?>
        <section name="socketServer" type="SuperSocket.SocketEngine.Configuration.SocketServiceConfig, SuperSocket.SocketEngine"/>
        <add key="ServiceName" value="SuperSocket"/>
            <server name="ServerA"
                    ip="Any" port="911" mode="Async">
            <server name="ServerB"
                    ip="Any" port="912" mode="Async">
            <service name="MyAppServerA"
                     type="SuperSocket.QuickStart.MultipleAppServer.MyAppServerA, SuperSocket.QuickStart.MultipleAppServer" />
            <service name="MyAppServerB"
                     type="SuperSocket.QuickStart.MultipleAppServer.MyAppServerB, SuperSocket.QuickStart.MultipleAppServer" />
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />

In some cases, interaction between two server instances is required. For example, client C1 of server A want to send message to client C2 of server B, so you need to forwarding the message through server B to the client C2 after server A receive the command from C1.

In OnStartup method of MyAppServerA, you can used method GetServerByName (string name) of SocketServerManager to get the server instance by name:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.SocketBase;
using SuperSocket.SocketEngine; 

namespace SuperSocket.QuickStart.MultipleAppServer
    public class MyAppServerA : AppServer
        private IDespatchServer m_DespatchServer; 

        protected override void OnStartup()
            m_DespatchServer = SocketServerManager.GetServerByName("ServerB") as IDespatchServer;

        internal void DespatchMessage(string targetSessionKey, string message)
            m_DespatchServer.DispatchMessage(targetSessionKey, message);

    interface IDespatchServer
        void DispatchMessage(string sessionKey, string message);

    public class MyAppServerB : AppServer, IDespatchServer
        public void DispatchMessage(string sessionKey, string message)
            var session = GetAppSessionByIndentityKey(sessionKey);

            if (session == null)


In this way, you can send message to clients of server B from server A by calling server B's method.

Please checkout the full sample code from MultipleAppServer project of QuickStart in source code of SuperSocket.

Last edited Nov 23, 2011 at 1:34 AM by kerryjiang, version 4


No comments yet.