TCP-Client Operations

<< Click to Display Table of Contents >>

Navigation:  3. Script Language > Internet and Network > TCP-Client Commands >

TCP-Client Operations

TCP.Open

Previous Top Next


SPR Script Language

 

TCP.Open

Opens a TCP client connection to a specified server address and port.

 

Intention

 

The TCP.Open command allows your SPR script to initiate a TCP/IP connection as a client to a server. You specify the server's IP address ($$ADDR), the port number ($$PORT), and an optional timeout ($$MST) for the connection attempt. If successful, a numeric socket handle is pushed onto the stack, which is used for subsequent TCP.Send and TCP.Recv operations. This enables communication with network services or custom servers like the MCP Server.

 

Syntax

 

TCP.Open|$$ADDR|$$PORT[|$$MST]

 

Parameter Explanation

 

P1 - $$ADDR - (Variable, String, Required)

The IP address or hostname of the server to connect to (e.g., "127.0.0.1" or "localhost").

 

P2 - $$PORT - (Variable, Numeric, Required)

The port number on the server to connect to (e.g., "80" for HTTP, "5556" for MCP Server).

 

P3 - $$MST - (Variable, Numeric, Optional)

The timeout for the connection attempt in milliseconds. If omitted, a default of 60000ms (60 seconds) is used.

 

Return Value (on Stack)

 

The command pushes a numeric socket handle onto the stack. This handle is used for subsequent TCP operations. Returns 0 if the connection fails (check HTP.GetErr for details).

 

Examples

 

'***********************************

' TCP.Open - Sample 1: Connect to MCP Server

'***********************************

TCP.Open|127.0.0.1|5556|5000

POP.$$HND ' Get the socket handle

JIZ.$$HND|Error_Handler ' Check for connection failure

'

'***********************************

' TCP.Open - Sample 2: Connect to a Web Server

'***********************************

TCP.Open|example.com|80

POP.$$WEB_HND

JIZ.$$WEB_HND|Error_Handler

 

Remarks

 

- This command initiates an outbound TCP connection. Use TCP.Close to close the connection when no longer needed.

- The returned handle is a PowerBASIC file number (like those used with OPEN or PRINT #).

- Errors are reported via HTP.GetErr if the connection fails.

 

See also:

 

TCP.Close

TCP.Send

TCP.Recv

HTP.MCPStatus

HTP.Request