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.4   6.5   7.0   7.1   7.2   7.3   7.4   7.5   8.0   8.1    Statistic  


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

Component Version macOS Windows Server FileMaker Cloud FileMaker iOS SDK
Socket 3.1 Yes Yes Yes Yes Yes

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


Parameter Description Example value
IPorDomain The IP or domain. For a dommain name, the DNS server is queried to find an IP address. ""
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.


Returns socket ID for other functions or error message.


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.


Connect to MonkeybreadSoftware website:

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

Connect to localhost website:

$sock = MBS( "Socket.Connect"; ""; 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]
    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"]
    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.DropMembership

Feedback: Report problem or ask question.

MBS FileMaker tutorial videos