SuperSocket配置UDP服务器
零、需求
两个设备局域网联机,需要用广播自动搜寻,而SuperSocket1.6的默认AppServer使用的是TCP,但只有UDP才支持广播。
一、解决
1. 方案一:可以通过配置 “App.config” 文件,再使用 “BootstrapFactory” 来启动UDP服务器。(这个是参考了C#SuperSocket的搭建–通过配置启动 的)
编写自己的 “Session” 类,我这边随便写写
using SuperSocket. SocketBase;
using SuperSocket. SocketBase. Protocol;
using System;
using System. Collections. Generic;
using System. Linq;
using System. Text;
using System. Threading. Tasks;
namespace SuperSocketTest. src. Service. UDPServer
{
public class MySession : AppSession < MySession>
{
protected override void OnSessionStarted ( )
{
this . Send ( "Welcome to SuperSocket Telnet Server" ) ;
}
protected override void HandleUnknownRequest ( StringRequestInfo requestInfo)
{
this . Send ( "Unknow request" ) ;
}
protected override void HandleException ( Exception e)
{
this . Send ( "Application error: {0}" , e. Message) ;
}
protected override void OnSessionClosed ( CloseReason reason)
{
base . OnSessionClosed ( reason) ;
}
}
}
接着按自己需求定义自己APPServer,这边我也随便写写(自定义的AppServer,在继承AppServer是的Session泛型,记得要更改为我们自定义的Session,这边我的是MySession)
using SuperSocket. SocketBase;
using SuperSocket. SocketBase. Config;
using SuperSocket. SocketBase. Protocol;
using System;
using System. Collections. Generic;
using System. Linq;
using System. Text;
using System. Threading. Tasks;
namespace SuperSocketTest. src. Service. UDPServer
{
class MyServer : AppServer < MySession>
{
public MyServer ( )
: base ( new CommandLineReceiveFilterFactory ( Encoding. Default, new BasicRequestInfoParser ( ":" , "," ) ) )
{
}
protected override bool Setup ( IRootConfig rootConfig, IServerConfig config)
{
return base . Setup ( rootConfig, config) ;
}
protected override void OnStopped ( )
{
base . OnStopped ( ) ;
}
}
}
SuperSocket 中的命令设计出来是为了处理来自客户端的请求的, 它在业务逻辑处理之中起到了很重要的作用。所以我们写一个自己的命令类。(后面好像处理不了,我也不知道怎么回事)
using SuperSocket. SocketBase. Command;
using SuperSocket. SocketBase. Protocol;
using System;
using System. Collections. Generic;
using System. Linq;
using System. Text;
using System. Threading. Tasks;
namespace SuperSocketTest. src. Service. UDPServer
{
public class MyCommand : CommandBase < MySession, StringRequestInfo>
{
public override string Name
{
get
{
return "001" ;
}
}
public override void ExecuteCommand ( MySession session, StringRequestInfo requestInfo)
{
session. Send ( "Hello,I'm UDP server! The parameter you send is " + requestInfo. Body) ;
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
< configuration>
< configSections>
< section name = " superSocket"
type = " SuperSocket.SocketEngine.Configuration.SocketServiceConfig, SuperSocket.SocketEngine" />
</ configSections>
< superSocket>
< servers>
< server name = " SSTUDP"
serverType = " SuperSocketTest.src.Service.UDPServer.MyServer,SuperSocketTest"
ip = " Any" port = " 2020" mode = " Udp" >
</ server>
</ servers>
</ superSocket>
< startup>
< supportedRuntime version = " v4.0" sku = " .NETFramework,Version=v4.5" />
</ startup>
</ configuration>
using SuperSocket. SocketBase;
using SuperSocket. SocketEngine;
using System;
using System. Collections. Generic;
using System. Linq;
namespace SuperSocketTest
{
class Program
{
static void Main ( string [ ] args)
{
Console. WriteLine ( "按任意键以启动服务器!" ) ;
Console. ReadKey ( ) ;
Console. WriteLine ( ) ;
var bootstrap = BootstrapFactory. CreateBootstrap ( ) ;
if ( ! bootstrap. Initialize ( ) )
{
Console. WriteLine ( "初始化失败!请检查配置文件!" ) ;
Console. ReadKey ( ) ;
return ;
}
var result = bootstrap. Start ( ) ;
Console. WriteLine ( "启动结果: {0}!" , result) ;
if ( result == StartResult. Failed)
{
Console. WriteLine ( "启动失败!" ) ;
Console. ReadKey ( ) ;
return ;
}
Console. WriteLine ( "按 ‘q’ 键停止服务器。" ) ;
while ( Console. ReadKey ( ) . KeyChar != 'q' )
{
Console. WriteLine ( ) ;
continue ;
}
Console. WriteLine ( ) ;
bootstrap. Stop ( ) ;
Console. WriteLine ( "服务器已停止!" ) ;
Console. ReadKey ( ) ;
}
}
}
结果:
– 成功,这边使用的自定义的命令行格式!
2. 方案二:通过代码的方式启动,这样简单些,但是文档中没有给出方法,在查阅源码后发现,设置启动模式可以在“AppServer”类的“Setup”方法中设置。
using SuperSocket. SocketBase;
using SuperSocket. SocketBase. Config;
using SuperSocket. SocketBase. Protocol;
using SuperSocket. SocketEngine;
using System;
using System. Collections. Generic;
using System. Linq;
using System. Text;
using System. Threading. Tasks;
namespace SuperSocketTest
{
class Program
{
static int clientCount = 0 ;
static void Main ( string [ ] args)
{
Console. WriteLine ( "按任意键以启动服务器!" ) ;
Console. ReadKey ( ) ;
Console. WriteLine ( ) ;
var appServer = new AppServer ( ) ;
if ( ! appServer. Setup ( new ServerConfig
{
Ip = "Any" ,
Port = 2020 ,
Mode = SocketMode. Udp
} ) )
{
Console. WriteLine ( "配置服务器失败!" ) ;
Console. ReadKey ( ) ;
return ;
}
Console. WriteLine ( ) ;
if ( ! appServer. Start ( ) )
{
Console. WriteLine ( "启动失败!" ) ;
Console. ReadKey ( ) ;
return ;
}
Console. WriteLine ( "服务器启动成功,按 ‘q’ 键停止服务器!" ) ;
appServer. NewSessionConnected + = new SessionHandler < AppSession > ( appServer_NewSessionConnected) ;
appServer. NewRequestReceived + = new RequestHandler < AppSession , StringRequestInfo > ( appServer_NewRequestReceived) ;
while ( Console. ReadKey ( ) . KeyChar != 'q' )
{
Console. WriteLine ( ) ;
continue ;
}
appServer. Stop ( ) ;
Console. WriteLine ( "服务器已停止!" ) ;
Console. ReadKey ( ) ;
}
private static void appServer_NewSessionConnected ( AppSession session)
{
clientCount++ ;
session. Send ( "Hello,you are the " + clientCount + "th connected client!" ) ;
}
private static void appServer_NewRequestReceived ( AppSession session, StringRequestInfo requestInfo)
{
switch ( requestInfo. Key. ToUpper ( ) )
{
case ( "001" ) : / / 同样添加一条命令,更多命令的使用请查阅文档
session. Send ( "Hello,I'm UDP server! The parameter you send is " + requestInfo. Body) ;
break ;
}
}
}
}
结果: – 成功!
二、总结
可以通过配置和代码来启动服务器。 代码在AppServer.Setup函数下配置。 文件配置在App.config文件里配置,再通过Bootstrap启动服务器。 对于TCP方式同样适用,只需要设置mode的值为Tcp即可。 SuperSocket很棒!怎么三年前没有发现这个宝藏呢! UDP测试 V1软件:UDP收发器 Gitee
谢谢支持