Topics   All   Mac OS X (Only)   Windows (Only)   Linux (Only, Not)   iOS (Only, Not)  
Components   Crossplatform Mac & Win   Server (Not)   Client   Old   Guides   Examples
New in version: 6.0   6.1   6.2   6.3   6.4   6.5   7.0   7.1   7.2   7.3    Statistic  

Socket.Connect

Connects a TCP/IP socket to the given domain/IP on the given port.

Component: Socket
Version: 3.1
Mac OS X: Yes
Windows: Yes
Server: Yes
FileMaker Cloud: Yes

MBS( "Socket.Connect"; IPorDomain; Port; Timeout { ; ProtocolPreference } )

Parameters

Parameter Description Example value
IPorDomain The IP or domain. For a dommain name, the DNS server is queried to find an IP address. "127.0.0.1"
Port The desired network port to connect to. 80
Timeout The connection timeout in seconds. Default depends on operation system and can be e.g. 90 seconds. 5
ProtocolPreference Optional
Pass 4 to connect via IPv4.
Pass 6 to connect via IPv6.
Default is to try whatever the DNS suggests first.
4

Result

Returns socket ID for other functions or error message.

Description

Connects a TCP/IP socket to the given domain/IP on the given port.
You need to call Socket.Close later to release memory used for this socket.

Version 6.3 now supports passing IPv4, iPv6, or a host name.

Examples

Connect to MonkeybreadSoftware website:

$sock = MBS( "Socket.Connect"; "monkeybreadsoftware.de"; 80 )

Connect to localhost website:

$sock = MBS( "Socket.Connect"; "127.0.0.1"; 80 )

Connect if needed and send something:

# connect if needed
If [IsEmpty($$sock)]
    Set Variable [$$sock; Value:MBS("Socket.Connect"; TCP Sender::TargetIP; TCP Sender::Port)]
    If [MBS("IsError")]
        Set Field [TCP Sender::Result; "Failed to create socket: " & $$sock]
        Exit Script []
    End If
    Set Variable [$r; Value:MBS("Socket.SetDataAvailableHandler"; $$sock; Get(FileName); "DataAvailable")]
    Set Variable [$r; Value:MBS("Socket.SetErrorHandler"; $$sock; Get(FileName); "SocketError")]
End If
# send some data
Set Variable [$r; Value:MBS("Socket.Write"; $$sock; TCP Sender::Message; "UTF-8")]
If [GetAsNumber($r) = 0]
    Set Field [TCP Sender::Result; "Failed to Send: "&$r]
Else
    Set Field [TCP Sender::Result; "Sent "&$r&" Bytes."]
End If

Connect to IP and send command and wait for answer:

Set Variable [$sock; Value:MBS("Socket.Connect";test::IP; 16128)]
If [MBS("IsError")]
    Show Custom Dialog ["Failed to connect"]
Else
    Set Variable [$write; Value:MBS("Socket.WriteHex"; $sock; test::query)]
    Pause/Resume Script [Duration (seconds): 1]
    Set Field [test::response; MBS("Socket.ReadHex"; $sock; 20)]
    Show Custom Dialog ["Hex: " & test::response & ¶ & "Text: " & MBS( "Text.DecodeFromHex"; test::response)]
    Set Variable [$sock; Value:MBS("Socket.Close"; $sock)]
End If

See also

Example Databases


Socket.CloseAll   -   Socket.GetBroadcast

Feedback: Report problem or ask question.




Links
MBS Xojo tutorial videos