1<?php
2/**
3 * This file is part of escpos-php: PHP receipt printer library for use with
4 * ESC/POS-compatible thermal and impact printers.
5 *
6 * Copyright (c) 2014-18 Michael Billington < michael.billington@gmail.com >,
7 * incorporating modifications by others. See CONTRIBUTORS.md for a full list.
8 *
9 * This software is distributed under the terms of the MIT license. See LICENSE.md
10 * for details.
11 */
12
13namespace Mike42\Escpos\PrintConnectors;
14
15use Exception;
16
17/**
18 * PrintConnector for directly opening a network socket to a printer to send it commands.
19 */
20class NetworkPrintConnector extends FilePrintConnector
21{
22    /**
23     * Construct a new NetworkPrintConnector
24     *
25     * @param string $ip IP address or hostname to use.
26     * @param string $port The port number to connect on.
27     * @param string $timeout The connection timeout, in seconds.
28     * @throws Exception Where the socket cannot be opened.
29     */
30    public function __construct($ip, $port = "9100", $timeout = false)
31    {
32        // Default to 60 if default_socket_timeout isn't defined in the ini
33        $defaultSocketTimeout = ini_get("default_socket_timeout") ?: 60;
34        $timeout = $timeout ?: $defaultSocketTimeout;
35
36        $this -> fp = @fsockopen($ip, $port, $errno, $errstr, $timeout);
37        if ($this -> fp === false) {
38            throw new Exception("Cannot initialise NetworkPrintConnector: " . $errstr);
39        }
40    }
41}
42