SUPERSOCKET 客户端
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 客户端的更多相关文章
- C#网络编程技术SuperSocket实战项目演练
一.SuperSocket课程介绍 1.1.本期<C#网络编程技术SuperSocket实战项目演练>课程阿笨给大家带来三个基于SuperSocket通讯组件的实战项目演示实例: ● 基于 ...
- 超详细的TCP、Sokcket和SuperSocket与TCP入门指导
前言 本文主要介绍TCP.Sokcket和SuperSocket的基础使用. 创建实例模式的SuperSocket服务 首先创建控制台项目,然后Nuget添加引用SuperSocket.Engine. ...
- 基于SuperSocket的IIS主动推送消息给android客户端
在上一篇文章<基于mina框架的GPS设备与服务器之间的交互>中,提到之前一直使用superwebsocket框架做为IIS和APP通信的媒介,经常出现无法通信的问题,必须一天几次的手动回 ...
- SuperSocket与Netty之实现protobuf协议,包括服务端和客户端
今天准备给大家介绍一个c#服务器框架(SuperSocket)和一个c#客户端框架(SuperSocket.ClientEngine).这两个框架的作者是园区里面的江大渔. 首先感谢他的无私开源贡献. ...
- SuperSocket入门(一)-Telnet服务器和客户端请求处理
本文的控制台项目是根据SuperSocket官方Telnet示例代码进行调试的,官方示例代码:Telnet示例. 开始我的第一个Telnet控制台项目之旅: 创建控制台项目:打开vs程序,文件=> ...
- 基于开源SuperSocket实现客户端和服务端通信项目实战
一.课程介绍 本期带给大家分享的是基于SuperSocket的项目实战,阿笨在实际工作中遇到的真实业务场景,请跟随阿笨的视角去如何实现打通B/S与C/S网络通讯,如果您对本期的<基于开源Supe ...
- SuperSocket 服务器管理器客户端
SuperSocket 服务器管理器当前有两种类型的客户端, Silverlight客户端和WPF客户端.这两种客户端的代码都在源代码中的"Management"目录,你可以自行编 ...
- SuperSocket主动从服务器端推送数据到客户端
关键字: 主动推送, 推送数据, 客户端推送, 获取Session, 发送数据, 回话快照 通过Session对象发送数据到客户端 前面已经说过,AppSession 代表了一个逻辑的 socke ...
- SuperSocket 学习笔记-客户端
客户端: 定义 private AsyncTcpSession client; 初始化 client = new AsyncTcpSession(); client.Connected += Clie ...
随机推荐
- confirm消息对话框
function rec(){ var mymessage= confirm("你是女孩?") ; if(mymessage==true) { document.write(&qu ...
- Log4j 日志记录
关于Log4j的简单示例 <!--手动配置log4j.properties文件内容:-->1 #level:debug/info/warn/error log4j.rootLogger=O ...
- Oracle数据库三种备份方案
Oracle数据库有三种标准的备份方法,它们分别是导出/导入(EXP/IMP).热备份和冷备份.导出备件是一种逻辑备份,冷备份和热备份是物理备份. 一. 导出/导入(Export/Import) 利用 ...
- 《uniGUI for cBuilder入门到精通》新书预定
<uniGUI for cBuilder入门到精通>火热预定中,从零开始带你入瓮带你飞,手把手教你如何快速安装,开发和部署一个web系统,前十名用户售价暂定100元,后续价格每本200元, ...
- ubantu创建python虚拟环境
安装虚拟环境的命令如下: sudo pip install virtualenv sudo pip install virtualenvwrapper 创建虚拟环境的命令如下: mkvirtualen ...
- python flask 小项目
0 开始之前 网上看了很多教程,都不是很满意,因此自己写一个大型教程,从入门到做出一个比较完整的博客.此次教程不是直接把整个博客直接代码整理出来然后运行一遍就完事,我会从flask的各个模块讲起.所以 ...
- LVM逻辑卷
LVM逻辑卷 一.LVM逻辑卷概述 1.LVM的作用: 扩充磁盘:不动数据,在使用状态,将磁盘容量变大. 能把多个物理的磁盘整合成一张大的虚拟的磁盘,比如:有3个5G的磁盘,能把它们整合成一个15G的 ...
- Spring Boot 揭秘与实战(四) 配置文件篇 - 有哪些很棒的特性
文章目录 1. 使用属性文件2. YAML文件 1.1. 自定义属性 1.2. 参数引用 1.3. 随机数属性 1.4. application-{profile}.properties参数加载 3. ...
- 【一题多解】Python 字符串逆序
https://blog.csdn.net/seetheworld518/article/details/46756639 https://blog.csdn.net/together_cz/arti ...
- m3u8编码视频webgl、threejs渲染视频纹理demo
<!DOCTYPE html> <html> <head> <meta charset=utf-8 /> <title>fz-live< ...