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. Mysql text类型的最大长度

    MySQL 3种text类型的最大长度如下: TEXT 65,535 bytes ~64kb MEDIUMTEXT 16,777,215 bytes ~16Mb LONGTEXT 4,294,967, ...

  2. centos7安装mysql mariadb

    从最新版本的linux系统开始,默认的是 Mariadb而不是mysql! 使用系统自带的repos安装很简单: yum install -y mariadb mariadb-server 启动mar ...

  3. mybatis 异常 There is no getter for property named 'bizId' in 'class java.lang.Long'

    mybatis 异常 There is no getter for property named 'bizId' in 'class java.lang.Long' 当使用mybatis进行传参的时候 ...

  4. MySQL:存储过程和函数

    存储过程和函数 一.创建存储过程和函数 1.创建存储过程 语法: CREATE PROCEDURE sp_name ([proc_parameter[,...]]) [characteristic . ...

  5. 1) 上传多张图片时 ,对 $_FILES 的处理. upload ; 2)fileinput 上传多张图片. 3) 修改,删除的时候删除原来的资源,图片 update, delete , 删除 4)生成器中两个字段上传图片的时候,要修改生成器生成的代码

    1上传多张图片, 要对 $_FILES进行 重新处理. //添加 public function addCourseAlbumAction() { $CourseAlbumModel = new Co ...

  6. php优秀框架codeigniter学习系列——constants.php

    该文件位于application/config/constants.php.

  7. TDate赋值给Variant时注意的问题

    //Delphi 10.3.1 32bit, Win 7 32bit procedure TForm1.btnTimeToVariantClick(Sender: TObject); var d:TD ...

  8. JS数据的基本类型

    字符串   String 数字    Number 布尔    Boolean Null     空 Undefined Object   对象  Array 数组   json   function ...

  9. logminer使用测试库进行挖掘分析,10.2.0.5

    上一篇测试是在dg环境进行测试挖掘,但是如果客户存在一个测试库,那样使用日志挖掘的影响性更小.本篇进行测试分析. 测试环境介绍: oracle linux  5.6,vmware虚拟机,安装两套单实例 ...

  10. python2.7.9安装mysql-python模块

    我使用的系统版本是: SLES12-sp2 使用python连接Mysql数据库,需要安装mysql-python模块: 1. 首先安装pip: 从python官方网站下载get-pipe.py,执行 ...