Contents
TCP
Overview
TCP is a Dyalog cover class for Asynchronous TCP/IP Communication using the .Net TcpClient object. It is using the methods WriteAsync and ReadAsync available with .Net 4.5 for asynchronous communication that will not block the calling thread during the communication process. It is used to requests data from an Internet resource using TCP (Transmission Control Protocol) over IP (Internet Protocol) like for home automation, data acquisition and process control. Real world programming of TCP/IP communication is difficult and this link is a good source of information.
Typical Usage
Instantiating the class
tcp ← ⎕NEW TCP
Connecting to a Remote Host
The TCP protocol establishes a connection with a remote endpoint and then uses that connection to send and receive data packets. TCP is responsible for ensuring that data packets are sent to the endpoint and assembled in the correct order when they arrive. The Connect method is used to establish this connection. In the class you can specify the buffer size, the port time-out, the switching delay as fields (see their individual comments in the class for more information).
r ← tcp.Connect(ip port) ⍝ ip = IP Address where to send the bytes in characters ('192.168.1.1') or numbers (192 168 1 1) ⍝ port = Port Number to send the bytes (0 - 65535). ⍝ r[1] = 1 for Success, 0 for Failure ⍝ r[2] = Literal Error if Failure
Sending the Data
The method SendReceive is used to send the data as numbers from 0 to 255 (bytes). The response contains the data received. To convert characters to bytes use ⎕UCS to do the conversion. This method if successful will continuously reuse the same TcpClient object.
r ← {retries}tcp.SendReceive bytes ⍝ bytes = numbers from 0 to 255 ⍝ retries = Optional number of times to retry the communication if no data is received ⍝ r[1] = 1 for Success, 0 for Failure ⍝ r[2] = Response if Success, Literal Error if Failure
Closing the Connection
The method Close is used to close the connection.
r ← tcp.Close ⍝ r[1] = 1 for Success, 0 for Failure ⍝ r[2] = Literal Error if Failure
Doing it all
The method ConnectSendReceiveClose will do all the methods necessary to Connect, Send, Receive and Close the socket as one operation. This method will use a new TcpClient object each time.
r ← {retries}tcp.ConnectSendReceiveClose(bytes ip port) ⍝ bytes = numbers from 0 to 255 ⍝ ip = IP Address where to send the bytes in characters ('192.168.1.1') or numbers (192 168 1 1) ⍝ port = Port Number to send the bytes (0 - 65535). ⍝ retries = Optional number of times to retry the communication if no data is received ⍝ r[1] = 1 for Success, 0 for Failure ⍝ r[2] = Literal Error if Failure
How to install TCP in your workspace
Download TCP.v1.0.txt
- Do a Select all (Ctrl+A) and a copy (Ctrl+C).
In your workspace execute )ed ○ TCP
- Paste (Ctrl+V) the text into the Dyalog editor
- Press Escape and ')save' your workspace
Version Information
Version 1.0 January 2016, Pierre Gilbert
Original author: |
Pierre Gilbert |
Responsible: |
|
Email: |
<apgil AT SPAMFREE videotron DOT ca> |