SUPERSOCKET.CLIENTENGINE 简单使用

江大没有给ClientEngine的Demo,一直没有找到其它的。。 自己从 websocket4net 项目中看了一些,然后写了一个简单的命令行的Client。

首先

引用 SuperSocket.ClientEngine.Core.dll和 SuperSocket.ClientEngine.Common.dll

然后

就可以使用ClientEngine了。

ClientEngine

我找了好久,只找到 AsyncTcpSession这么一个可以实例化的连接会话,那么,就用这个吧。

1
2
3
string ip = "127.0.0.1";
int port = 12345;
AsyncTcpSession client = new AsyncTcpSession(new IPEndPoint(IPAddress.Parse(ip), port));

处理连接的事件

1
2
3
4
5
6
7
8
// 连接断开事件
client.Closed += client_Closed;
// 收到服务器数据事件
client.DataReceived += client_DataReceived;
// 连接到服务器事件
client.Connected += client_Connected;
// 发生错误的处理
client.Error += client_Error;

处理函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void client_Error(object sender, ErrorEventArgs e)
{
    Console.WriteLine(e.Exception.Message);
}
 
void client_Connected(object sender, EventArgs e)
{
   Console.WriteLine("连接成功");
}
 
void client_DataReceived(object sender, DataEventArgs e)
{
    string msg = Encoding.Default.GetString(e.Data);
    Console.WriteLine(msg);
}
 
void client_Closed(object sender, EventArgs e)
{
    Console.WriteLine("连接断开");
}
 
public void Connect()
{
    client.Connect();
    string loginCmd = "LOGIN test";
    byte[] data = Encoding.Default.GetBytes(loginCmd);
    client.Send(data, 0, data.Length);
}

连接到服务器

1
client.Connect();

向服务器发送数据

1
2
3
string loginCmd = "LOGIN test\r\n";
byte[] data = Encoding.Default.GetBytes(loginCmd);
client.Send(data, 0, data.Length);

需要注意的是,因为服务器是使用的命令行协议,所以在数据末需要加上 \r\n。如果是使用其它协议,只是这里数据的结构发生变化。

如果服务器返回了数据,那么就可以在client_DataReceived函数中处理了。

具体的不怎么清楚,我也没有测试,从名称AsyncTcpSession来看,这个连接是异步的,也就是说,如果直接在 client.Connect()后执行 client.Send(xxxx) 很可能会异常,因为此时连接不一定建立了。所以,发送数据要在事件session_ConnectStarted调用后。

最后,就有了这样的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using SuperSocket.ClientEngine;
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
 
namespace hyjiacan.com
{
    public class SSClient
    {
        private AsyncTcpSession client;
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="ip">服务器IP</param>
        /// <param name="port">服务器端口</param>
        public SSClient(string ip, int port)
        {
            client = new AsyncTcpSession(new IPEndPoint(IPAddress.Parse(ip), port));
            // 连接断开事件
            client.Closed += client_Closed;
            // 收到服务器数据事件
            client.DataReceived += client_DataReceived;
            // 连接到服务器事件
            client.Connected += client_Connected;
            // 发生错误的处理
            client.Error += client_Error;
        }
        void client_Error(object sender, ErrorEventArgs e)
        {
            Console.WriteLine(e.Exception.Message);
        }
 
        void client_Connected(object sender, EventArgs e)
        {
            Console.WriteLine("连接成功");
        }
 
        void client_DataReceived(object sender, DataEventArgs e)
        {
            string msg = Encoding.Default.GetString(e.Data);
            Console.WriteLine(msg);
        }
 
        void client_Closed(object sender, EventArgs e)
        {
            Console.WriteLine("连接断开");
        }
 
        /// <summary>
        /// 连接到服务器
        /// </summary>
        public void Connect()
        {
            client.Connect();
        }
 
        /// <summary>
        /// 向服务器发命令行协议的数据
        /// </summary>
        /// <param name="key">命令名称</param>
        /// <param name="data">数据</param>
        public void SendCommand(string key, string data)
        {
            if (client.IsConnected)
            {
                byte[] arr = Encoding.Default.GetBytes(string.Format("{0} {1}", key, data));
                client.Send(arr, 0, arr.Length);
            }
            else
            {
                throw new InvalidOperationException("未建立连接");
            }
        }
    }
}
using SuperSocket.ClientEngine;
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
 
namespace hyjiacan.com
{
    public class SSClient
    {
        private AsyncTcpSession client;
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="ip">服务器IP</param>
        /// <param name="port">服务器端口</param>
        public SSClient(string ip, int port)
        {
            client = new AsyncTcpSession(new IPEndPoint(IPAddress.Parse(ip), port));
            // 连接断开事件
            client.Closed += client_Closed;
            // 收到服务器数据事件
            client.DataReceived += client_DataReceived;
            // 连接到服务器事件
            client.Connected += client_Connected;
            // 发生错误的处理
            client.Error += client_Error;
        }
        void client_Error(object sender, ErrorEventArgs e)
        {
            Console.WriteLine(e.Exception.Message);
        }
 
        void client_Connected(object sender, EventArgs e)
        {
            Console.WriteLine("连接成功");
        }
 
        void client_DataReceived(object sender, DataEventArgs e)
        {
            string msg = Encoding.Default.GetString(e.Data);
            Console.WriteLine(msg);
        }
 
        void client_Closed(object sender, EventArgs e)
        {
            Console.WriteLine("连接断开");
        }
 
        /// <summary>
        /// 连接到服务器
        /// </summary>
        public void Connect()
        {
            client.Connect();
        }
 
        /// <summary>
        /// 向服务器发命令行协议的数据
        /// </summary>
        /// <param name="key">命令名称</param>
        /// <param name="data">数据</param>
        public void SendCommand(string key, string data)
        {
            if (client.IsConnected)
            {
                byte[] arr = Encoding.Default.GetBytes(string.Format("{0} {1}", key, data));
                client.Send(arr, 0, arr.Length);
            }
            else
            {
                throw new InvalidOperationException("未建立连接");
            }
        }
    }
}

SUPERSOCKET 客户端的更多相关文章

  1. C#网络编程技术SuperSocket实战项目演练

    一.SuperSocket课程介绍 1.1.本期<C#网络编程技术SuperSocket实战项目演练>课程阿笨给大家带来三个基于SuperSocket通讯组件的实战项目演示实例: ● 基于 ...

  2. 超详细的TCP、Sokcket和SuperSocket与TCP入门指导

    前言 本文主要介绍TCP.Sokcket和SuperSocket的基础使用. 创建实例模式的SuperSocket服务 首先创建控制台项目,然后Nuget添加引用SuperSocket.Engine. ...

  3. 基于SuperSocket的IIS主动推送消息给android客户端

    在上一篇文章<基于mina框架的GPS设备与服务器之间的交互>中,提到之前一直使用superwebsocket框架做为IIS和APP通信的媒介,经常出现无法通信的问题,必须一天几次的手动回 ...

  4. SuperSocket与Netty之实现protobuf协议,包括服务端和客户端

    今天准备给大家介绍一个c#服务器框架(SuperSocket)和一个c#客户端框架(SuperSocket.ClientEngine).这两个框架的作者是园区里面的江大渔. 首先感谢他的无私开源贡献. ...

  5. SuperSocket入门(一)-Telnet服务器和客户端请求处理

    本文的控制台项目是根据SuperSocket官方Telnet示例代码进行调试的,官方示例代码:Telnet示例. 开始我的第一个Telnet控制台项目之旅: 创建控制台项目:打开vs程序,文件=> ...

  6. 基于开源SuperSocket实现客户端和服务端通信项目实战

    一.课程介绍 本期带给大家分享的是基于SuperSocket的项目实战,阿笨在实际工作中遇到的真实业务场景,请跟随阿笨的视角去如何实现打通B/S与C/S网络通讯,如果您对本期的<基于开源Supe ...

  7. SuperSocket 服务器管理器客户端

    SuperSocket 服务器管理器当前有两种类型的客户端, Silverlight客户端和WPF客户端.这两种客户端的代码都在源代码中的"Management"目录,你可以自行编 ...

  8. SuperSocket主动从服务器端推送数据到客户端

    关键字: 主动推送, 推送数据, 客户端推送, 获取Session, 发送数据, 回话快照 通过Session对象发送数据到客户端   前面已经说过,AppSession 代表了一个逻辑的 socke ...

  9. SuperSocket 学习笔记-客户端

    客户端: 定义 private AsyncTcpSession client; 初始化 client = new AsyncTcpSession(); client.Connected += Clie ...

随机推荐

  1. centos7创建docker tomcat镜像

    1 准备宿主系统 准备一个 CentOS 7操作系统,具体要求如下: 必须是 64 位操作系统 建议内核在 3.8 以上 通过以下命令查看您的 CentOS 内核: 1 # uname -r 2 安装 ...

  2. 第二节 java流程控制(循环结构)

     1.for循环 for(初始化表达式;循环条件表达式;循环后的操作表达式){ 执行语句 } 2.while循环 while(条件表达式){ 执行语句 } while循环特点是只有条件满足才会执行我们 ...

  3. Ionic2开发环境搭建、项目创建调试与Android应用的打包、优化

    Ionic2开发环境搭建.项目创建调试与Android应用的打包.优化. windows下ionic2开发环境配置步骤如下: 下载node.js环境,稳定版本:v6.9.5 下载android stu ...

  4. 牛客oi测试赛 二 B 路径数量

    题目描述 给出一个 n * n 的邻接矩阵A. A是一个01矩阵 . A[i][j]=1表示i号点和j号点之间有长度为1的边直接相连. 求出从 1 号点 到 n 号点长度为k的路径的数目. 输入描述: ...

  5. 用Filter实现图片防盗链

    首先继承自FilterAttribute类同时实现IActionFilter接口,代码如下: //// <summary> /// 防盗链Filter. /// </summary& ...

  6. oracle sequence

    代码块 方法一: (1)删除序列; (2)重新创建: 这个方法比较简单粗暴. drop sequence sequence_name; create sequence sequence_name mi ...

  7. jetty域证书更新

    服务器:centos6.6 1.从正确的.pfx文件中导出.pem认证文件 #openssl pkcs12 -in example.pfx -nodes -out server.pem pfx文件可以 ...

  8. TCCSuperPlayerView让Delphi支持app视频播放!

    今天ChinaCock发布了新版,完美支持视频播放!新版本中,发布了新的控件TCCSuperPlayerView,以支持视频播放. 这是一个可视控件,拖放到Form上,调整好大小与位置,就可以调用他的 ...

  9. python scrapy 数据处理时间格式转换

    def show(self,response): # print(response.url) title = response.xpath('//main/div/div/div/div/h1/tex ...

  10. 某关于数位DP的一节课后的感受

    题目 求给定区间[x,y]中满足下列条件的整数个数,这个数恰好等于k个互不相等的B的整数次幂之和 Input 15 20 2 2 Out 17 18 20 示例:17=24+20 18=24+21 2 ...