-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprinter.php
More file actions
41 lines (37 loc) · 1.28 KB
/
printer.php
File metadata and controls
41 lines (37 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
$cht_string = '我要用中文';
$cht_string = iconv(mb_detect_encoding($cht_string), 'UTF-8', $cht_string);
$cht_big5 = iconv(mb_detect_encoding($cht_string), 'BIG-5', $cht_string);
// 192.168.1.14:80 網路印表機 IP
$host = '192.168.1.14';
$port = 80; //default listening port for printer
$message = ' Hello, world'.$cht_string.$cht_big5.' ';
// create socket
// 0, IP
// SOL_TCP = specify protocol of TCP, UDP, FTP ...
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!$socket) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
socket_close($socket);
die("Could not create socket: [$errorcode] $errormsg\n");
}
// connect to server
$result = socket_connect($socket, $host, $port);
if (!$result) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
socket_close($socket);
die("Could not connect to server: [$errorcode] $errormsg\n");
}
// send string to server
$socket_wrt = socket_write($socket, $message, strlen($message));
if (!$socket_wrt) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
socket_close($socket);
die("Could not send data to server: [$errorcode] $errormsg\n");
}
echo 'Reply From Server:'.$result;
// close socket
socket_close($socket);