下載地址:https://files.cnblogs.com/files/xixixing/ConsoleApp.zipbootstrap
建立控制檯應用程序:ConsoleApp服務器
MySession.cssession
using SuperSocket.SocketBase; using SuperSocket.SocketBase.Protocol; using System; namespace ConsoleApp { /// <summary> /// 用來發送和接收客戶端信息,一個客戶端至關於一個session(會話)。 /// 自定義鏈接類MySession,繼承AppSession,並傳入到AppSession /// </summary> public class MySession : AppSession<MySession> { /// <summary> /// 新鏈接 /// </summary> //protected override void OnSessionStarted() //{ // this.Send("\r\nHello User"); //} /// <summary> /// 未知的請求 /// </summary> /// <param name="requestInfo"></param> protected override void HandleUnknownRequest(StringRequestInfo requestInfo) { //this.Send("\r\nUnknown Request"); } /// <summary> /// 捕捉異常並輸出 /// </summary> /// <param name="e"></param> protected override void HandleException(Exception e) { //this.Send("\r\nException: {0}", e.Message); } /// <summary> /// 鏈接關閉 /// </summary> /// <param name="reason"></param> protected override void OnSessionClosed(CloseReason reason) { base.OnSessionClosed(reason); } } }
MyServer.csapp
using SuperSocket.SocketBase; using System; namespace ConsoleApp { /// <summary> /// 自定義服務器類MyServer,繼承AppServer,並傳入自定義鏈接類MySession /// </summary> public class MyServer : AppServer<MySession> { protected override void OnStarted() { base.OnStarted(); //Console.WriteLine("服務器已啓動"); } /// <summary> /// 輸出新鏈接信息 /// </summary> /// <param name="session"></param> protected override void OnNewSessionConnected(MySession session) { base.OnNewSessionConnected(session); //輸出客戶端IP地址 Console.Write("\r\n" + session.LocalEndPoint.Address.ToString() + ":鏈接"); } /// <summary> /// 輸出斷開鏈接信息 /// </summary> /// <param name="session"></param> /// <param name="reason"></param> protected override void OnSessionClosed(MySession session, CloseReason reason) { base.OnSessionClosed(session, reason); //輸出客戶端IP地址 Console.Write("\r\n" + session.LocalEndPoint.Address.ToString() + ":斷開鏈接"); } protected override void OnStopped() { base.OnStopped(); Console.WriteLine("服務器已中止"); } } }
Program.cside
using System; using SuperSocket.SocketBase; using SuperSocket.SocketEngine; namespace ConsoleApp { class Program { static void Main(string[] args) { var bootstrap = BootstrapFactory.CreateBootstrap(); if (!bootstrap.Initialize()) //啓動SuperSocket { Console.WriteLine("初始化失敗!"); Console.ReadKey(); return; } var result = bootstrap.Start(); Console.WriteLine("服務正在啓動: {0}!", result); if (result == StartResult.Failed) { Console.WriteLine("服務啓動失敗!"); Console.ReadKey(); return; } Console.WriteLine("服務啓動成功,請按'E'中止服務!"); while (Console.ReadKey().KeyChar != 'E') { Console.WriteLine(); continue; } //中止服務 bootstrap.Stop(); Console.WriteLine("服務已中止!"); Console.ReadKey(); } } }
MyKey.cs,寫了兩個Key:HELLO、READY,響應的結果都是1234this
using SuperSocket.SocketBase.Command; using SuperSocket.SocketBase.Protocol; namespace ConsoleApp { public class HELLO : CommandBase<MySession, StringRequestInfo> { /// <summary> /// 自定義執行命令方法,注意傳入的變量session類型爲MySession /// </summary> /// <param name="session"></param> /// <param name="requestInfo"></param> public override void ExecuteCommand(MySession session, StringRequestInfo requestInfo) { session.Send("1234"); } } public class READY : CommandBase<MySession, StringRequestInfo> { public override void ExecuteCommand(MySession session, StringRequestInfo requestInfo) { session.Send("1234"); } } }
App.config編碼
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <!--log 日誌記錄--> <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/> <!--SocketEngine--> <section name="superSocket" type="SuperSocket.SocketEngine.Configuration.SocketServiceConfig, SuperSocket.SocketEngine"/> </configSections> <!--服務信息描述,在window服務模式下的名稱標識--> <appSettings> <add key="ServiceName" value="SupperSocketService"/> <add key="ServiceDescription" value="黃昏前黎明後的Socket程序"/> </appSettings> <!--SuperSocket服務配置信息 serverType是項目的服務,如我自定義的MyServer--> <!--name: 實例名稱 textEncoding 編碼方式"gb2312","utf-8" 默認是acii serverType: 實例運行的AppServer類型。逗號左邊的是你自定義的server在項目中的位置,逗號右邊是項目名 ip: 服務器ip port: 偵聽端口--> <superSocket> <servers> <server name="ConsoleApp" textEncoding="gb2312" serverType="ConsoleApp.MyServer,ConsoleApp" ip="Any" port="2000"> <!--ip="192.168.0.40"--> </server> </servers> </superSocket> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration>