TCP/IP Networking

TCP/IP support is integrated into VTS. To use this feature you will need to have a TCP/IP stack, which is supplied by many network vendors. All TCP/IP functions in VTS are performed using socket streams that act like serial connections between two programs. VTS can act as both a socket server and a socket client.

The main functions used to handle TCP/IP are ClientSocket, ServerSocket, SocketServerStart, SocketServerEnd, SocketWait, SRead, SWrite, BlockWrite and TCPIPReset.

Below is an example of a Client and a Server that will create a connection and pass the string "Hello World", followed by a number representing the number of seconds since midnight.

(Client)

 

[
    Graphics Module { Contains user graphics }; 
    Calculations Module { Contains user calculations }; 
    WinTitle = "Socket Test - Client Side" { Window title }; 
    SocketHandle; 
    Client; 
    Server; 
    Data; 
]
Main [
    Window( 0, 0 { Upper left corner },
    800, 600 { View area }, 
    800, 600 { Virtual area }, 
    Graphics() { Start user graphics }, 
    {5432109876543210} 
    0b0010000000110011, WinTitle, 0, 1); 
]
<
{===================== System\Graphics =======================}
{ This module handles all of the graphics for the application }
{=============================================================}
Graphics
Init [
    If 1 Screen1; 
    [ 
          Client = ClientSocket(0, "Richard", 20000, 1024, 1024, 1);
    ]    
]
Screen1 [
    If TimeOut(!Valid(Client), 2) Init;
    If TimeOut(ValueType(Client) <> 8,2) Error;
    If GetStreamLength(Client) > 0 || MatchKeys(2, "r");
    [ 
          SRead(Client,
          Concat("%", Concat(GetStreamLength(Client), "c")),
          Data); 
    ] 
    If TimeOut(1, 1);
    [ 
          SWrite(Client,
          "%s", Concat(" Hello World ", Time(Seconds(), 3)));
    ] 
    If MatchKeys(1, " ");
    [ 
          SWrite(Client, "%s", Concat(" Hello World ", Seconds()));
    ] 
    ZText(10, 150, Data, 15, 0);
    ZText(200, 100, Cond(Valid(Client),"Connected","Not Connected"),
    10, 0); 
    ZText(200, 110, Concat("ErrorCode : ", Client), 10, 0);
    ZText(200, 120, Concat("Type : ", ValueType(Client)), 10, 0);
    If WindowClose(Self());
    [ 
          CloseStream(Client);
          Slay(Self(), 1);
    ] 
]
Error [
    ZText(100, 130, Concat("ErrorCode : ", Client), 10, 0);
]
{ End of System\Graphics }
>
Server
[
    Graphics Module { Contains user graphics }; 
    Calculations Module { Contains user calculations }; 
    WinTitle = "Socket Test - Server Side" { Window title }; 
    SocketHandle; 
    Client; 
    Server; 
    Data; 
    Attribs0; 
    Attribs1; 
]
Main [
    Window(0, 0 { Upper left corner },
    800, 600 { View area }, 
    800, 600 { Virtual area }, 
    Graphics() { Start user graphics }, 
    {5432109876543210} 
    0b0010000000110011, WinTitle, 0, 1); 
]
<
{===================== System\Graphics =======================}
{ This module handles all of the graphics for the application }
{=============================================================}
Graphics
Init [
    If 1 Wait ; 
    [ 
          SocketHandle = SocketServerStart(0, 20000, 1024, 1024, 1);
    ] 
]
Wait [
    If SocketWait(SocketHandle) Main;
    [ 
          Server = ServerSocket(SocketHandle);
    ] 
]
Main [
    If GetStreamLength(Server) > 0 || MatchKeys(2, "r");
    [ 
          SRead(Server, Concat("%", Concat(GetStreamLength(Server),
          "c")), Data); 
          SWrite(Server, "%s", Data);
    ] 
    If WindowClose(Self);
    [ 
          CloseStream(Server);
          SocketServerEnd(SocketHandle);
          Slay(Self(), 1);
    ] 
    ZText(0, 50, Data, 15, 0);
    ZText(0, 100, Cond(Valid(Server), "Connected","Not Connected"),
    10, 0); 
]
{ End of System\Graphics }
>