Socket.Read
-----------

Reads bytes from socket and returns them as text.

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

MBS( "Socket.Read"; SocketID; Length { ; Encoding } ) 

**MBS**( **"Socket.Read";** /\* Reads bytes from socket and returns them as text. \*/  
**$SocketID**; /\* The socket ID received by [Socket.Connect](SocketConnect.md) function.e.g. $sock \*/   
**$Length**; /\* Maximum number of bytes to read.e.g. 10 \*/   
**$Encoding**) /\* Optional; The text encoding for result.   
Default is native. This function can also handle UTF-16 as well as UTF-16LE and UTF-16BE for little/big endian byte order.e.g. UTF8 \*/ 

### Parameters

| Parameter | Description | Example | Flags |
|---|---|---|---|
| SocketID | The socket ID received by [Socket.Connect](SocketConnect.md) function. | $sock |  |
| Length | Maximum number of bytes to read. | 10 |  |
| Encoding | The text encoding for result.    Default is native. This function can also handle UTF-16 as well as UTF-16LE and UTF-16BE for little/big endian byte order.   Possible encoding names: ANSI, ISO-8859-1, Latin1, Mac, Native, UTF-8, DOS, Hex, Base64 or Windows. More listed in the [FAQ](https://www.monkeybreadsoftware.com/filemaker/faq.shtml). | UTF8 | Optional |

### Result

Returns text or error message.

### Description

Reads bytes from socket and returns them as text.  
The text returned may be shorter than length bytes.  
  
The buffer size of the socket is managed by the OS and can be several megabytes big if needed.  
To read the whole input buffer, use [Socket.ReadAll](SocketReadAll.md). To look into buffer without removing text, please use [Socket.Peek](SocketPeek.md) function instead.  
  
Please leave some time between [Socket.Write](SocketWrite.md) calls and a subsequent reading of the results with a read function, so the other side has time to process your request. A script pause of a second or two may help.  
### Examples

Read up to 8 bytes and decode them from Windows text encoding into FileMaker:

 $value = MBS ( "Socket.Read"; $sock ; 8; "windows" )  
Connect to a HTTP Server and wait for answer:

 Set Variable \[$r ; Value:MBS ("Trace")\]  
Set Variable \[$sock ; Value:MBS ("[Socket.Connect](SocketConnect.md)"; TCP Socket::DomainOrIP ; 80)\]  
If \[MBS ("IsError")\]  
 Show Custom Dialog \["Error"; $sock \]  
 Exit Script \[\]  
End If  
Set Variable \[$request ; Value:"GET "&amp;TCP Socket::Path &amp;" HTTP/1.0 ¶Host: "&amp;TCP Socket::DomainOrIP &amp;"¶¶"\]  
Set Variable \[$request ; Value:MBS ("[Text.ReplaceNewLine](TextReplaceNewLine.md)"; $request ; 2)\]  
Set Field \[TCP Socket::RequestUsed ; $request \]  
Set Variable \[$r ; Value:MBS ("[Socket.Write](SocketWrite.md)"; $sock ; $request )\]  
Pause/Resume Script \[Duration (seconds): 1\]  
Set Variable \[$data ; Value:MBS ("Socket.Read"; $sock ; 1000)\]  
Set Variable \[$data ; Value:MBS ("[Text.ReplaceNewLine](TextReplaceNewLine.md)"; $data ; 1)\]  
Set Field \[TCP Socket::Result ; $data \]  
Set Variable \[$r ; Value:MBS ("[Socket.Close](SocketClose.md)"; $sock )\]  
Read data and put in a new record:

 Set Variable \[$r ; Value:MBS ("Socket.Read"; $$sock ; 1500; "UTF-8")\]  
If \[MBS ("IsError")\]  
 Show Custom Dialog \["Error Reading"; $r \]  
Else If \[Length($r ) = 0\]  
 Show Custom Dialog \["Error Reading"; "No data available."\]  
Else  
 New Record/Request  
 Set Field \[UDP Broadcast::Message Received; $r \]  
 Set Field \[UDP Broadcast::Sender ; MBS ("[Socket.LastMessageIP](SocketLastMessageIP.md)"; $$sock )\]  
 Commit Records/Requests \[No dialog\]  
End If  
### See also

- [Socket.LastMessageIP](SocketLastMessageIP.md)
- [Socket.Peek](SocketPeek.md)
- [Socket.PeekAllHex](SocketPeekAllHex.md)
- [Socket.ReadAll](SocketReadAll.md)
- [Socket.ReadAllHex](SocketReadAllHex.md)
- [Socket.ReadHex](SocketReadHex.md)
- [Socket.ReadLine](SocketReadLine.md)
- [Socket.SetDataAvailableEvaluate](SocketSetDataAvailableEvaluate.md)
- [Socket.WriteHex](SocketWriteHex.md)
- [Trace](Trace.md)

### Release notes

- **Version 10.4**
    - Improved [Socket](https://www.mbsplugins.eu/component_Socket.shtml) functions to better return errors. [Socket.AvailableBytes](https://www.mbsplugins.eu/SocketAvailableBytes.shtml), [Socket.Write](https://www.mbsplugins.eu/SocketWrite.shtml) or [Socket.Read](https://www.mbsplugins.eu/SocketRead.shtml) functions now return an error when socket is disconnected or otherwise broken.

### Example Databases

- [Network/HTTP Server hosted](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Network/HTTP%20Server%20hosted.shtml#2ScriptAnchor_)
- [Network/HTTP Server local](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Network/HTTP%20Server%20local.shtml#2ScriptAnchor_)
- [Network/Socket Test/Socket Test](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Network/Socket%20Test/Socket%20Test.shtml#2ScriptAnchor_)
- [Network/SSL/TCP Socket SSL](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Network/SSL/TCP%20Socket%20SSL.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#2ScriptAnchor_)
- [Network/TCP Send and Receive/TCP Sender](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Network/TCP%20Send%20and%20Receive/TCP%20Sender.shtml#2ScriptAnchor_)
- [Network/TCP Socket](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Network/TCP%20Socket.shtml#1ScriptAnchor_)
- [Network/UDP/UDP Broadcast](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Network/UDP/UDP%20Broadcast.shtml#3ScriptAnchor_)
- [Network/UDP/UDP Receiver](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Network/UDP/UDP%20Receiver.shtml#2ScriptAnchor_)

### Blog Entries

- [MBS FileMaker Plugin, version 10.4pr7](https://www.mbsplugins.de/archive/2020-09-01/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [Socket changes for MBS FileMaker Plugin](https://www.mbsplugins.de/archive/2020-08-31/Socket_changes_for_MBS_FileMak/monkeybreadsoftware_blog_filemaker)
- [MBS Filemaker Plugin, version 3.2pr3](https://www.mbsplugins.de/archive/2013-03-05/MBS_Filemaker_Plugin_version_3/monkeybreadsoftware_blog_filemaker)

This function is free to use.

Created 18th August 2014 , last changed 29th July 2022

  
[Socket.PeekHex](SocketPeekHex.md) - [Socket.ReadAll](SocketReadAll.md)

[HTML Version](SocketRead.shtml)