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/
相關文章
相關標籤/搜索