Components | All | New | MacOS | Windows | Linux | iOS | ||||
Examples | Mac & Win | Server | Client | Guides | Statistic | FMM | Blog | Deprecated | Old |
Socket.Connect
Connects a TCP/IP socket to the given domain/IP on the given port.
Component | Version | macOS | Windows | Linux | Server | iOS SDK |
Socket | 3.1 | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |
Parameters
Parameter | Description | Example | Flags |
---|---|---|---|
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 | Pass 4 to connect via IPv4. Pass 6 to connect via IPv6. Default is to try whatever the DNS suggests first. |
4 | Optional |
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.com"; 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("Text.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("Text.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
- Socket.SetMulticastLoop
- Socket.SetNewConnectionEvaluate
- Socket.SetNewConnectionHandler
- Socket.SetReceiveBufferSize
- Socket.SetSendBufferSize
- Socket.SetTag
- Socket.SSL.Connect
- Socket.Write
- Socket.WriteHex
- Text.DecodeFromHex
Release notes
- Version 8.3
- Updated code for Socket.Connect, ServerSocket.SendQuery, SSH.Connect and SendCommand to use new unified socket connection code, which uses IPv4 and IPv6, queries DNS if needed and tries several IPs if needed.
- Version 7.2
- Added ProtocolPreference parameter to SSH.Connect and Socket.Connect to request IPv4 or IPv6.
Example Databases
- Network/SSL/TCP Sender SSL
- Network/SSL/TCP Socket SSL
- Network/TCP Send and Receive/TCP Sender
- Network/TCP Socket
- Network/Zebra ZPL Sample
Blog Entries
- MBS FileMaker Plugin, version 8.3pr3
- MBS FileMaker Plugin, version 7.2pr4
- MBS FileMaker Plugin, version 6.3pr4
- MBS FileMaker Plugin 5.1 for OS X/Windows
- MBS FileMaker Plugin, version 5.1pr3
This function checks for a license.
Created 18th August 2014, last changed 4th March 2023