|
2 | 2 | # Socket |
3 | 3 |
|
4 | 4 | ## This is a layer for client and server socket connections. |
5 | | - |
6 | | -### PHP extension which have to be installed before to work: |
7 | | -* [Yaml](https://www.php.net/manual/en/book.yaml.php) |
8 | | -* [Sockets](https://www.php.net/manual/en/book.sockets.php) |
9 | | - |
10 | | -### Four actions you have to make: |
11 | | -* Configure your configuration file. |
12 | | -* Create client.php or server.php file |
13 | | -* Call methods |
14 | | -* Run from CLI or extends |
15 | | - |
16 | | -### Examples of TCP and UNIX sockets: |
17 | | - |
18 | | -#### 1) Create configuration file. File can be **ONLY** with yaml(yml) or json extensions |
19 | | - |
20 | | -##### * TCP socket configuration using YAML |
21 | | -``` |
22 | | -settings: |
23 | | - socket_type: tcp |
24 | | - domain: AF_INET |
25 | | - type: SOCK_STREAM |
26 | | - protocol: SOL_TCP |
27 | | - backlog: 1 |
28 | | - host: '127.0.0.1' |
29 | | - port: '8001' |
30 | | - read_length: 2048 |
31 | | - read_flag: MSG_WAITALL |
32 | | -``` |
33 | | - |
34 | | -##### * UNIX socket configuration using YAML |
35 | | -``` |
36 | | -settings: |
37 | | - socket_type: unix |
38 | | - domain: AF_UNIX |
39 | | - type: SOCK_STREAM |
40 | | - protocol: 0 |
41 | | - backlog: 1 |
42 | | - socket_file: socket.sock |
43 | | - read_length: 2048 |
44 | | - read_flag: MSG_WAITALL |
45 | | -``` |
46 | | - |
47 | | -#### 2) Create client and server file handlers. |
48 | | - |
49 | | -##### server.php |
50 | | -``` |
51 | | -<?php |
52 | | -
|
53 | | -require_once 'vendor/autoload.php'; |
54 | | -
|
55 | | -use Qonsillium\QonsilliumSocket; |
56 | | -use Qonsillium\ServerSocket; |
57 | | -
|
58 | | -$server = new QonsilliumSocket('config.yaml'); |
59 | | -$server->runServer(function(ServerSocket $socket) { |
60 | | - echo $socket->send('Hello from server!'); |
61 | | -}); |
62 | | -``` |
63 | | - |
64 | | -##### client.php |
65 | | -``` |
66 | | -<?php |
67 | | -
|
68 | | -require_once 'vendor/autoload.php'; |
69 | | -
|
70 | | -use Qonsillium\QonsilliumSocket; |
71 | | -use Qonsillium\ClientSocket; |
72 | | -
|
73 | | -$server = new QonsilliumSocket('config.yaml'); |
74 | | -$server->runClient(function(ClinetSocket $socket) { |
75 | | - echo $socket->send('Hello from client!'); |
76 | | -}); |
77 | | -``` |
78 | | - |
79 | | -#### 3) Run from CLI |
80 | | -``` |
81 | | - john@doe:/workdir/$ php server.php |
82 | | - john@doe:/workdir/$ php client.php |
83 | | -``` |
84 | | - |
85 | | -## List of available configuration vars: |
86 | | -#### 1) sock_type - Type of socket which will be created. |
87 | | -* tcp |
88 | | -* unix |
89 | | - |
90 | | -#### 2) domain - Specifies the protocol family |
91 | | -* AF_INET |
92 | | -* AF_INET6 |
93 | | -* AF_UNIX |
94 | | - |
95 | | -#### 3) type - Selects the type of communication |
96 | | -* SOCK_STREAM |
97 | | -* SOCK_DGRAM |
98 | | -* SOCK_SEQPACKET |
99 | | -* SOCK_RAW |
100 | | -* SOCK_RDM |
101 | | - |
102 | | -#### 3) protocol - Sets the specific protocol within the specified domain to be used when communicating on the returned socket |
103 | | -* SOL_TCP |
104 | | -* SOL_UDP |
105 | | -* 0 (when use unix) |
106 | | - |
107 | | -#### 4) backlog - A maximum of backlog incoming connections will be queued for processing |
108 | | -* Number of incoming backlogs (1 or 2 etc.) |
109 | | - |
110 | | -#### 5) host - Host name which where will be accepted or created connection. Can be used when if socket is of the AF_INET family |
111 | | -* 127.0.0.1 |
112 | | - |
113 | | -#### 6) port - The port parameter is only used when binding an AF_INET socket, and designates the port on which to listen for connections. |
114 | | -* from 1024 to 65535 |
115 | | - |
116 | | -#### 7) socket_file - If the socket is of the AF_UNIX family, the address is the path of a Unix-domain socket. You can create own socket file but you must make that you set correct path and this file have read and write rights. Or you can set random name in work dir and here will be created socket file |
117 | | -* /tmp/socket.sock |
118 | | -* socket.sock |
119 | | - |
120 | | -#### 8) read_length - The maximum number of bytes read is specified by the length parameter. |
121 | | -* 2048, 4096 etc |
122 | | - |
123 | | -#### 9) read_flag - The flags responded for reading status. |
124 | | -* MSG_OOB |
125 | | -* MSG_PEEK |
126 | | -* MSG_WAITALL |
127 | | -* MSG_DONTWAIT |
128 | | - |
129 | | -###### For details you can refer to the [PHP](https://php.net) documentation |
| 5 | +#### [Detailed documentation](http://socket-docs.epizy.com) |
0 commit comments