NET使用SuperSocket完成TCP/IP通信

1)为什么使用SuperSocket?

性能高,易上手。有中文文档,我们可以有更多的时间用在业务逻辑上,SuperSocket有效的利用自己的协议解决粘包

2)SuperSocket的协议内容?

命令 body  列如:TestCommand 1 2 

3)怎样在Net下使用 SuperSocket?

 1)新建项目命名为SuperSocketWeb

 2)引用程序集-》NuGet工具搜索安装SuperSocket,SuperSocket.Engine两个组件

 3)下载测试工具SocketTool 官网demo存在

 4)新建链接类 TestSession,每个链接为一个Session

复制代码
using SuperSocket.SocketBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SuperSocket.SocketBase.Protocol;

namespace SuperSocket
{
    public class TestSession : AppSession<TestSession>
    {
        public int CustomID { get; set; }
        public string CustomName { get; set; }
        /// <summary>
        /// 用户连接会话
        /// </summary>
        protected override void OnSessionStarted()
        {
            Console.WriteLine("新的用户请求");
            base.OnSessionStarted();
        }

        /// <summary>
        /// 未知的用户请求命令
        /// </summary>
        /// <param name="requestInfo"></param>
        protected override void HandleUnknownRequest(StringRequestInfo requestInfo)
        {
            base.HandleUnknownRequest(requestInfo);
        }
        /// <summary>
        /// 会话关闭
        /// </summary>
        /// <param name="reason"></param>
        protected override void OnSessionClosed(CloseReason reason)
        {
            base.OnSessionClosed(reason);
        }
    }
}
复制代码

5)新建命令类TestCommand 

复制代码
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 SuperSocket
{
    public class TestCommand : CommandBase<TestSession, StringRequestInfo>
    {
        /// <summary>
        /// 命令处理类
        /// </summary>
        /// <param name="session"></param>
        /// <param name="requestInfo"></param>
        public override void ExecuteCommand(TestSession session, StringRequestInfo requestInfo)
        {
            session.CustomID = new Random().Next(10000, 99999);
            session.CustomName = "hello word";

            var key = requestInfo.Key;
            var param = requestInfo.Parameters;
            var body = requestInfo.Body;
            //返回随机数session.Send(session.CustomID.ToString());
//返回
 session.Send(body);
       } 
}
}
复制代码

6)新建服务类 TestServer 

复制代码
using SuperSocket.SocketBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SuperSocket
{
    public class TestServer : AppServer<TestSession>
    {
    }
}
复制代码

7)开启tcp/ip监听

 在Global->Application_Start中开启服务监听,最好使用日志记录监听的开启情况

复制代码
           var appServer = new TestServer();
            if (!appServer.Setup(2012)) //开启的监听端口
            {
                return;
            }
            if (!appServer.Start())
            {
                return;
            }
复制代码

4)测试demo

打开SocketTool,链接服务器ip+端口号。注意在命令结束要按回车哦,红色框的位置 

 返回表示成功,你也可以自定义自己想要的返回值

 

 源码地址:https://github.com/MrsongJl/SuperSocketWeb

原文地址:http://www.cnblogs.com/songjl/p/6907514.html

转载于:https://www.cnblogs.com/Jeely/p/10974242.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值