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

Socket.Connect

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 } )

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

Measure from a specific scale:

# Open port
Set Variable [ $sock ; Value: MBS("Socket.Connect"; Scale::IP, Scale::Port) ]
If [ MBS("IsError") ]
    Show Custom Dialog [ "Failed to open socket" ; $sock ]
    Exit Script [ Text Result: ]
End If
# Send reset command
Set Variable [ $r ; Value: MBS("Socket.Write"; $sock; "@") ]
Set Variable [ $r ; Value: MBS("Socket.WriteByte"; $sock; 13) ]
Set Variable [ $r ; Value: MBS("Socket.WriteByte"; $sock; 10) ]
# Disable keyboard
Set Variable [ $r ; Value: MBS("Socket.Write"; $sock; "K 3") ]
Set Variable [ $r ; Value: MBS("Socket.WriteByte"; $sock; 13) ]
Set Variable [ $r ; Value: MBS("Socket.WriteByte"; $sock; 10) ]
# Set scale to zero
Set Variable [ $r ; Value: MBS("Socket.Write"; $sock; "Z") ]
Set Variable [ $r ; Value: MBS("Socket.WriteByte"; $sock; 13) ]
Set Variable [ $r ; Value: MBS("Socket.WriteByte"; $sock; 10) ]
# Set to measure in gramms
Set Variable [ $r ; Value: MBS("Socket.Write"; $sock; "M21 1 0") ]
Set Variable [ $r ; Value: MBS("Socket.WriteByte"; $sock; 13) ]
Set Variable [ $r ; Value: MBS("Socket.WriteByte"; $sock; 10) ]
Set Variable [ $r ; Value: MBS("Socket.Write"; $sock; "M21 0 0") ]
Set Variable [ $r ; Value: MBS("Socket.WriteByte"; $sock; 13) ]
Set Variable [ $r ; Value: MBS("Socket.WriteByte"; $sock; 10) ]
# Send expected weight and tolerance
Set Variable [ $r ; Value: MBS("Socket.Write"; $sock; "PMW REL "& Scale::WeightExpected & " " & Scale::Tolerance & " " & Scale::Tolerance & " g") ]
Set Variable [ $r ; Value: MBS("Socket.WriteByte"; $sock; 13) ]
Set Variable [ $r ; Value: MBS("Socket.WriteByte"; $sock; 10) ]
Set Variable [ $Counter ; Value: 1 ]
Set Variable [ $finished ; Value: 0 ]
Loop
    Loop
        # Wait for Answer
        Set Variable [ $data ; Value: "" ]
        Loop
            Pause/Resume Script [ Duration (seconds): ,01 ]
            Set Variable [ $newdata ; Value: MBS("Socket.Read"; $sock; 1000) ]
            If [ MBS("IsError") = 0 ]
                Set Variable [ $data ; Value: $data & $newData ]
            End If
            Exit Loop If [ Position($Data; "¶"; 1; 1)>0 ]
        End Loop
        Set Variable [ $data ; Value: MBS("String.ReplaceNewline"; $data; 1) ]
        If [ ValueCount ( $data ) = 1 ]
            Set Variable [ $data ; Value: GetValue($data; 1) ]
        End If
        # Process Answer
        If [ $data = "K A" ]
            # Answer from keyboard turn off
            # okay
        Else If [ $data = "PMW A" ]
            # Answer PMW
            # okay
        Else If [ $data = "K C 4" ]
            # Return pressed on scale
            Set Variable [ $r ; Value: MBS("Socket.Write"; $sock; "S") ]
            Set Variable [ $r ; Value: MBS("Socket.WriteByte"; $sock; 13) ]
            Set Variable [ $r ; Value: MBS("Socket.WriteByte"; $sock; 10) ]
            # Wait for Answer
            Set Variable [ $data ; Value: "" ]
            Loop
                Pause/Resume Script [ Duration (seconds): ,01 ]
                Set Variable [ $newdata ; Value: MBS("Socket.Read"; $sock; 1000) ]
                If [ MBS("IsError") = 0 ]
                    Set Variable [ $data ; Value: $data & $newData ]
                End If
                Exit Loop If [ Position($Data; "¶"; 1; 1)>0 ]
            End Loop
            Set Variable [ $data ; Value: MBS("String.ReplaceNewline"; $data; 1) ]
            If [ ValueCount ( $data ) = 1 ]
                Set Variable [ $data ; Value: GetValue($data; 1) ]
            End If
            # Process Answer
            If [ Left($Data; 3) = "S S" ]
                Set Variable [ $Data ; Value: Substitute($Data; ","; ".") ]
                Set Variable [ $Data ; Value: Substitute($Data; "."; ",") ]
                Set Variable [ $Weight ; Value: GetAsNumber( Middle($Data; 4; 20)) ]
                If [ Position ( $data; "kg"; 1;1) >0 ]
                    Set Variable [ $Weight ; Value: $Weight * 1000 ]
                End If
                Set Field [ Scale::Status ; "Weight read: " & $Weight ]
                Set Field [ Scale::WeightGemessen ; $Weight ]
                If [ ($Weight >= Scale::WeightExpected - Scale::Tolerance) and ($Weight <= Scale::WeightExpected + Scale::Tolerance) ]
                    Set Variable [ $finished ; Value: 1 ]
                    Show Custom Dialog [ "Thanks" ]
                    Exit Loop If [ 1 ]
                Else
                    Show Custom Dialog [ "Please measure again" ]
                End If
            End If
            Exit Loop If [ $finished ]
        End If
        Exit Loop If [ $finished ]
    End Loop
    Exit Loop If [ $finished ]
End Loop
# Enable keyboard
Set Variable [ $r ; Value: MBS("Socket.Write"; $sock; "K 2") ]
Set Variable [ $r ; Value: MBS("Socket.WriteByte"; $sock; 13) ]
Set Variable [ $r ; Value: MBS("Socket.WriteByte"; $sock; 10) ]
# Reset scale
Set Variable [ $r ; Value: MBS("Socket.Write"; $sock; "@") ]
Set Variable [ $r ; Value: MBS("Socket.WriteByte"; $sock; 13) ]
Set Variable [ $r ; Value: MBS("Socket.WriteByte"; $sock; 10) ]
# short wait to let commands run to device
Pause/Resume Script [ Duration (seconds): ,01 ]
# close port
Set Variable [ $r ; Value: MBS("Socket.Close"; $sock) ]

See also

Example Databases


Socket.CloseAll   -   Socket.DropMembership

Feedback: Report problem or ask question.




Links
MBS FileMaker tutorial videos