作者: 江大鱼 发表于 2010-06-11 10:23 原文链接 阅读: 26 评论: 0
This guide will introduce how to create a simple command line socket application using SupperSocket application framework.
About SuperSocket: http://www.cnblogs.com/jzywh/archive/2010/06/09/supersocket.html
1. Create a new project named “EchoService” in a new empty solution
2. Add the project “SocketServiceCore”(which is belong to SuperSocket) into the solution, and then let the project “EchoService” reference it
3. Add session class and server class
EchoSession.cs
EchoSession
<
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
> using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.SocketServiceCore;
namespace EchoService
{
public class EchoSession : AppSession < EchoSession >
{
private SocketContext m_Context;
protected override void OnClosed()
{
}
protected override void OnInit()
{
m_Context = new SocketContext();
}
public override void SayWelcome()
{
SendResponse( " Welcome to EchoServer! " );
}
public override void HandleExceptionalError(Exception e)
{
SendResponse( " Server side error occurred! " );
}
public override SocketContext Context
{
get { return m_Context; }
}
}
}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
> using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.SocketServiceCore;
namespace EchoService
{
public class EchoSession : AppSession < EchoSession >
{
private SocketContext m_Context;
protected override void OnClosed()
{
}
protected override void OnInit()
{
m_Context = new SocketContext();
}
public override void SayWelcome()
{
SendResponse( " Welcome to EchoServer! " );
}
public override void HandleExceptionalError(Exception e)
{
SendResponse( " Server side error occurred! " );
}
public override SocketContext Context
{
get { return m_Context; }
}
}
}
EchoServer.cs
EchoServer
<
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
> using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.SocketServiceCore;
namespace EchoService
{
public class EchoServer : AppServer < EchoSession >
{
public override bool IsReady
{
get { return true ; }
}
}
}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
> using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.SocketServiceCore;
namespace EchoService
{
public class EchoServer : AppServer < EchoSession >
{
public override bool IsReady
{
get { return true ; }
}
}
}
4. Add command class
ECHO.cs
ECHO
<
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
> using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.SocketServiceCore.Command;
namespace EchoService.Command
{
public class ECHO : ICommand < EchoSession >
{
#region ICommand
Members
public void Execute(EchoSession session, CommandInfo commandData)
{
session.SendResponse(commandData.Param);
}
#endregion
}
}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
> using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.SocketServiceCore.Command;
namespace EchoService.Command
{
public class ECHO : ICommand < EchoSession >
{
#region ICommand
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
<
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
> 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>
<add name="Default Echo Server" serviceName="EchoService" ip="Any" port="911" mode="Async" />
servers>
<services>
<add serviceName="EchoService" baseAssembly="EchoService.EchoServer, EchoService" />
services>
socketServer>
<loggingConfiguration name="Logging Application Block" tracingEnabled="true"
defaultCategory="" logWarningsWhenNoCategoriesMatch="true">
//Log configuration
loggingConfiguration>
configuration>
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
> 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>
<add name="Default Echo Server" serviceName="EchoService" ip="Any" port="911" mode="Async" />
servers>
<services>
<add serviceName="EchoService" baseAssembly="EchoService.EchoServer, EchoService" />
services>
socketServer>
<loggingConfiguration name="Logging Application Block" tracingEnabled="true"
defaultCategory="" logWarningsWhenNoCategoriesMatch="true">
//Log configuration
loggingConfiguration>
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")
You can check out the QuickStart's code from the address below:
http://supersocket.codeplex.com/SourceControl/changeset/changes/53902
最新新闻:
· 6个强大的社会化网络搜索引擎(2010-06-12 08:58)
· 马雪征:把联想的经验带入TPG(2010-06-12 08:54)
· 苹果如何基业长青(2010-06-12 08:52)
· 腾讯:平台优势催动“利润发生器”(2010-06-12 08:52)
· Google News测试新版页面(2010-06-12 08:50)
编辑推荐:谈谈IT软件开发工程师的基本功