SUPERSOCKET.CLIENTENGINE 简单使用

首先

引用 SuperSocket.ClientEngine.Core.dll和 SuperSocket.ClientEngine.Common.dll服务器

而后

就能够使用ClientEngine了。session

ClientEngine

我找了很久,只找到 AsyncTcpSession这么一个能够实例化的链接会话,那么,就用这个吧。异步

string ip = "127.0.0.1";
int port = 12345;
AsyncTcpSession 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;

处理函数socket

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);
}

链接到服务器函数

client.Connect();

向服务器发送数据

 

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

须要注意的是,由于服务器是使用的命令行协议,因此在数据末须要加上 \r\n。若是是使用其它协议,只是这里数据的结构发生变化。测试

若是服务器返回了数据,那么就能够在client_DataReceived函数中处理了。spa

具体的不怎么清楚,我也没有测试,从名称AsyncTcpSession来看,这个链接是异步的,也就是说,若是直接在client.Connect()后执行 client.Send(xxxx) 极可能会异常,由于此时链接不必定创建了。因此,发送数据要在事件session_ConnectStarted调用后。命令行

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

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("未创建链接");
            }
        }
    }
}

 

 http://www.hyjiacan.com/supersocket-chat-telnet/
相关文章
相关标签/搜索