Differences between revisions 3 and 4
Revision 3 as of 2015-12-29 22:05:27
Size: 2732
Comment: UNDER CONSTRUCTION
Revision 4 as of 2016-01-09 23:22:08
Size: 3829
Comment: Version 1.0
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
|| UNDER CONSTRUCTION ||
Line 5: Line 4:
'''TCP''' is a Dyalog cover class for Asynchronous TCP/IP Communication using the .Net [[https://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient%28v=vs.110%29.aspx|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. '''TCP''' is a Dyalog cover class for Asynchronous TCP/IP Communication using the .Net [[https://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient%28v=vs.110%29.aspx|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 [[http://blog.stephencleary.com/2009/04/tcpip-net-sockets-faq.html|link]] is a good source of information.
Line 12: Line 11:
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). 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).
Line 23: Line 22:
The method `Send` 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. 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.
Line 25: Line 24:
     r ← tcp.Send bytes ⍝ bytes = numbers from 0 to 255      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
Line 34: Line 36:

     ⍝ 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
Line 48: Line 63:
 Version 1.0 December 2016, Pierre Gilbert  Version 1.0 January 2016, Pierre Gilbert
Line 57: Line 72:
See also: [[netCOM]] [[netUDP]]

----

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

  1. Download TCP.v1.0.txt

  2. Do a Select all (Ctrl+A) and a copy (Ctrl+C).
  3. In your workspace execute )ed ○ TCP

  4. Paste (Ctrl+V) the text into the Dyalog editor
  5. Press Escape and ')save' your workspace

Version Information

 Version 1.0 January 2016, Pierre Gilbert

Original author:

Pierre Gilbert

Responsible:

PierreGilbert

Email:

<apgil AT SPAMFREE videotron DOT ca>


See also: netCOM netUDP


CategoryDyalog - CategoryDyalogDotNet - CategoryDotNet

netTCP (last edited 2016-01-09 23:22:08 by PierreGilbert)