Socket.Listen
-------------

Binds the socket to the port and starts listening.

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [Socket](component_Socket.md) | [3.1](newinversion31.md) | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |

MBS( "Socket.Listen"; SocketID; Port { ; backlog } ) 

**MBS**( **"Socket.Listen";** /\* Binds the socket to the port and starts listening. \*/  
**$SocketID**; /\* The socket ID received by [Socket.Connect](SocketConnect.md) function.e.g. $sock \*/   
**$Port**; /\* The port number to listen at.  
If port is zero, we pick a random free port number, which you can query with [Socket.LocalPort](SocketLocalPort.md) function.e.g. 8000 \*/   
**$backlog**) /\* Optional; The back log for connection.  
How many pending connections can be accepted on this socket. Once you call [Socket.AcceptNewConnection](SocketAcceptNewConnection.md), an entry is removed here.  
Default is 3.e.g. 3 \*/ 

### Parameters

| Parameter | Description | Example | Flags |
|---|---|---|---|
| SocketID | The socket ID received by [Socket.Connect](SocketConnect.md) function. | $sock |  |
| Port | The port number to listen at.   If port is zero, we pick a random free port number, which you can query with [Socket.LocalPort](SocketLocalPort.md) function. | 8000 |  |
| backlog | The back log for connection.   How many pending connections can be accepted on this socket. Once you call [Socket.AcceptNewConnection](SocketAcceptNewConnection.md), an entry is removed here.   Default is 3. | 3 | Optional      Added in version **9.1**. |

### Result

Returns OK or error message.

### Description

Binds the socket to the port and starts listening.  
For UDP Socket, the socket starts receiving packets.  
For TCP Socket, the socket can accept connections which causes a script to be called. You set the script name with [Socket.SetNewConnectionHandler](SocketSetNewConnectionHandler.md). In the script you call [Socket.AcceptNewConnection](SocketAcceptNewConnection.md) to get a new socket for the new connection.  
  
Be aware that FileMaker Server does not allow the plugin to operate if no script is running. So if you use sockets there, make sure a script is running or paused while you expect an answer coming in.  
### Examples

Listen to a given Port:

 Set Variable \[$r ; Value:MBS ("Socket.Listen"; $$sock ; 12345)\]  
Create TCP Socket listening on a port:

 Set Variable \[$$sock ; Value:MBS ("[Socket.NewTCPSocket](SocketNewTCPSocket.md)")\]  
If \[MBS ("IsError")\]  
 Show Custom Dialog \["Failed to create Socket"; $$sock \]  
 Exit Script \[\]  
End If  
Set Variable \[$r ; Value:MBS ("[Socket.SetNewConnectionHandler](SocketSetNewConnectionHandler.md)"; $$sock ; Get(FileName); "NewConnection")\]  
Set Variable \[$r ; Value:MBS ("[Socket.SetErrorHandler](SocketSetErrorHandler.md)"; $$sock ; Get(FileName); "SocketError")\]  
Set Variable \[$r ; Value:MBS ("Socket.Listen"; $$sock ; TCP Receiver::ListenPort )\]  
If \[MBS ("IsError")\]  
 Show Custom Dialog \["Failed to create Socket"; $$sock \]  
 Exit Script \[\]  
End If  
### See also

- [IsError](IsError.md)
- [Socket.AcceptNewConnection](SocketAcceptNewConnection.md)
- [Socket.Connect](SocketConnect.md)
- [Socket.IsListening](SocketIsListening.md)
- [Socket.List](SocketList.md)
- [Socket.LocalPort](SocketLocalPort.md)
- [Socket.NewTCPSocket](SocketNewTCPSocket.md)
- [Socket.NewUDPSocket](SocketNewUDPSocket.md)
- [Socket.SetErrorHandler](SocketSetErrorHandler.md)
- [Socket.SetNewConnectionHandler](SocketSetNewConnectionHandler.md)

### Release notes

- **Version 9.1**
    - Added backlog parameter for [Socket.Listen](http://www.mbsplugins.eu/SocketListen.shtml).

### Example Databases

- [Network/HTTP Server hosted](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Network/HTTP%20Server%20hosted.shtml#1ScriptAnchor_)
- [Network/HTTP Server local](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Network/HTTP%20Server%20local.shtml#1ScriptAnchor_)
- [Network/Socket Test/Socket Test](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Network/Socket%20Test/Socket%20Test.shtml#1ScriptAnchor_)
- [Network/TCP Send and Receive/TCP Receiver](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Network/TCP%20Send%20and%20Receive/TCP%20Receiver.shtml#1ScriptAnchor_)
- [Network/UDP/UDP Broadcast](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Network/UDP/UDP%20Broadcast.shtml#2ScriptAnchor_)
- [Network/UDP/UDP Receiver](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Network/UDP/UDP%20Receiver.shtml#1ScriptAnchor_)

### Blog Entries

- [Neues MBS FileMaker Plugin 9.1](https://www.mbsplugins.de/archive/2019-03-12/Neues_MBS_FileMaker_Plugin_91/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin 9.1 - More than 5700 Functions In One Plugin](https://www.mbsplugins.de/archive/2019-03-12/MBS_FileMaker_Plugin_91_-_More/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 9.1pr6](https://www.mbsplugins.de/archive/2019-03-06/MBS_FileMaker_Plugin_version_9/monkeybreadsoftware_blog_filemaker)

This function checks for a license.

Created 18th August 2014 , last changed 10th January 2023

  
[Socket.List](SocketList.md) - [Socket.LocalIP](SocketLocalIP.md)

[HTML Version](SocketListen.shtml)