[C#.NET][TCP Socket] 利用 Socket 同步方法 控制 Alien Reader

  • 5499
  • 0
  • C#
  • 2013-08-16

[C#.NET][TCP Socket] 利用 Socket 同步方法 控制 Alien Reader

Alien Reader 提供了 TCP/IP 的通訊介面,所以我們可以用 Socket Class 來對它下達命令,首先要先對它進行連線

Step1.初始化 Socket 執行個體:

Step2.調用 Socket.Connect 方法:

Step3.調用 Socket.Receive 方法,接收 Socket.Connect 的結果:

程式碼如下:

 


public class SynchronousSocketClient
{
    private Socket _socketCliect = null;

    private Encoding _encode = Encoding.ASCII;

    public Encoding Encode
    {
        get { return _encode; }
        set { _encode = value; }
    }

    public string Connect(string IP, int Port)
    {
        var ipAddress = IPAddress.Parse(IP);
        var iPEndPoint = new IPEndPoint(ipAddress, 23);

        //Create a TCP/IP  socket.
        this._socketCliect = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        EndPoint endPoint = (EndPoint)iPEndPoint;

        this._socketCliect.Connect(endPoint);
        var receive = Receive();
        var user = this.Send("alien\r\n");
        var password = this.Send("password\r\n");
        return receive;
    }
}

 

Receive 方法是依照 Alien Reader Server 所回傳的特性進行處理,當調用 Socket.Connect 方法後,Alien Reader 會先回傳第一階段的資料,然後才會再回傳第二階段的資料

image

 

如果想學習 Socket,千萬別傻傻的照抄這個方法。

 

 


public string Receive()
{
    if (!this._socketCliect.Connected)
    {
        return null;
    }

    var tempTimeOut = this._socketCliect.ReceiveTimeout;
    this._socketCliect.ReceiveTimeout = 1000;

    var sb = new StringBuilder();
    var buffer = new byte[1024];

    while (true)
    {
        var socketError = new SocketError();
        var receiveCount = this._socketCliect.Receive(buffer, 0, buffer.Length, SocketFlags.None, out socketError);
        Thread.Sleep(100);
        if (receiveCount == 0 || socketError != SocketError.Success)
        {
            break;
        }
        else
        {
            var receive = this.Encode.GetString(buffer, 0, receiveCount);
            sb.Append(receive);
            if (this._socketCliect.Available == 0)
            {
                break;
            }
        }
    }

    this._socketCliect.ReceiveTimeout = tempTimeOut;
    var result = sb.ToString();
    Console.WriteLine(result);
    return result;
}

 

當 Client 端進行調用 Connect 時,所回傳的結果

image

 

PS.很明顯的我們需要一個帳號,只要你的公司有購買 Alien Reader 你就能得到它

 

Step4.調用 Socket.Send 方法:

Step5.調用 Socket.Receive 方法,接收 Socket.Send 的結果:

 

利用下方法送出帳號/密碼,在此不便列出。


public string Send(string Data)
{
    if (!this._socketCliect.Connected)
    {
        return null;
    }

    while (true)
    {
        SocketError error = new SocketError();
        var sendData = this.Encode.GetBytes(Data);
        this._socketCliect.Send(sendData, 0, sendData.Length, SocketFlags.None, out error);
        if (error == SocketError.Success)
        {
            break;
        }
    }

    return Receive();
}

 

登入 Alien Reader 的結果

image

 

登入成功後,便可調用上述的 Send 方法下達手冊上的命令,例如,Get TagList

image

 

 

若有謬誤,煩請告知,新手發帖請多包涵


Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET

Image result for microsoft+mvp+logo