[Developer Shed Network] Server Side - PHP - Socket Programming With PHP
Socket programming with php
-
Upload
elizabeth-smith -
Category
Technology
-
view
10.536 -
download
5
Transcript of Socket programming with php
![Page 1: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/1.jpg)
Socket Programming with PHPBack to the Basics
![Page 2: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/2.jpg)
In the beginning There was a process
and the process ran well
but it needed to talk to another process
“Use IPC and all shall be clear”
![Page 3: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/3.jpg)
IPC Inter Process Communication
File
Signal
Socket
Pipe
Named Pipe
Semaphore
…
![Page 4: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/4.jpg)
But which do I need?
Are the processes on the same computer?
Does it need to support multiple OS types?
Communication needs to be one way or two way?
![Page 5: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/5.jpg)
Network SocketsAll things internet
![Page 6: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/6.jpg)
Layers of Fun IP – forwards packets of data based on a
destination address
TCP – verifies the correct delivery of data from client to server with error and lost data correction
Network Sockets – subroutines that provide TCP/IP (and UDP and some other support) on most systems
![Page 7: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/7.jpg)
Acronym Madness The DOD section DARPA built ARPANET
which ran on TCP/IP and the protocols are maintained by IETF
TCP - Transmission Control Protocol
IP – Internet Protocol
UDP - User Datagram Protocol
![Page 8: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/8.jpg)
Packet of Data
![Page 9: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/9.jpg)
![Page 10: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/10.jpg)
What are Sockets?Programming 101
![Page 11: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/11.jpg)
Wormholes! Sockets are just like "worm holes" in
science fiction. When things go into one end, they (should) come out of the other.
![Page 12: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/12.jpg)
Types Connection-Oriented
“Ack?”
“Syn-Ack”
“Syn!”
Connectionless
0x49424d434f52504f524154494f3e32303036
![Page 13: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/13.jpg)
Data Flow Simplex – one way street
Half-Duplex – can send up and down, BUT only one at a time
Full-Duplex – can send up and down at the SAME time
![Page 14: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/14.jpg)
BSD SocketsBerkley / Posix and everything in between
![Page 15: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/15.jpg)
BSD -> Posix Everybody used BSD
“Hey, it might be smart to standardize this”
“Excellent, let’s just use BSD basically”
Posix sockets == BSD sockets with very minor alterations
![Page 16: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/16.jpg)
Network Socket Types
Stream
Connection oriented (tcp)
Datagram
Connectionless (udp)
Raw
Low level protocols
![Page 17: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/17.jpg)
Where are we going?
Addresses
Ports
![Page 18: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/18.jpg)
Doing it with PHP“Oh yeah, I guess PHP does have all those apis…”
![Page 19: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/19.jpg)
StreamsMaking things dead easy
![Page 20: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/20.jpg)
What is a Stream? Access input and output generically
Can write and read linearly
May or may not be seekable
Comes in chunks of data
![Page 21: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/21.jpg)
How PHP Streams Work
Stream Contexts
Stream Wrapper
Stream FilterALL IO
![Page 22: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/22.jpg)
Definitions Socket
Bidirectional network stream that speaks a protocol
Transport
Tells a network stream how to communicate
Wrapper
Tells a stream how to handle specific protocols and encodings
![Page 23: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/23.jpg)
Built in Socket Transports
tcp
udp
unix
udg
SSL extension
ssl
sslv2
sslv3
tls
![Page 24: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/24.jpg)
Things to watch for! Sockets block
stream_set_blocking
stream_set_timeout
stream_select
feof means “connection_closed”?
huge reads or writes (think 8K)
stream_get_meta_data is READ ONLY
![Page 25: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/25.jpg)
Using Sockets
![Page 26: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/26.jpg)
stream_socket_client
![Page 27: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/27.jpg)
stream_socket_server
![Page 28: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/28.jpg)
Sockets ExtensionAll the power of C apis, all the pain of C apis
![Page 29: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/29.jpg)
Almost C, but not quite
Wrapper around BSD sockets api
Some of the declarations are different
You really need to know how sockets work
Like writing in C without the hassles
A core extension but NOT a default extension
Possibly noisy, use an error handler
![Page 30: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/30.jpg)
Keep your man handy
PHP
socket_create
socket_listen
socket_bind
socket_read
C man page
socket
listen
bind
read
![Page 31: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/31.jpg)
Create First socket_create
domain (IPv4, IPv6, unix)
type – stream for tcp and dgram for udp is the usual
protocol – hint, you can pass 0 to get the “default” protocol for the type
![Page 32: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/32.jpg)
Listen socket_bind
$socket
address
port
socket_listen
$socket
backlog
socket_create_listen
port
backlog
create
bind
listen
![Page 33: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/33.jpg)
and Accept socket_accept
$socket
gives you a NEW socket resource to read off of, but you can’t accept off the newly created socket resource it returns
![Page 34: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/34.jpg)
Or Connect socket_connect
$socket
address (ipv4 or 6 depending on what you set)
port
![Page 35: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/35.jpg)
Read and Write socket_write
$socket
data
len (will truncate to this!)
socket_read
$socket
length
type (stop on newlines or grab as binary)
![Page 36: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/36.jpg)
Grab Data socket_recv - usually connected socket
socket_recvfrom - from a specific address and port
socket_recvmsg – grabs a whole message (datagram’s friend)
![Page 37: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/37.jpg)
Send Data socket_send
socket_sendmsg
socket_sendto
![Page 38: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/38.jpg)
All done! socket_shutdown – stop reading and writing
socket_close – all done, close it down
![Page 39: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/39.jpg)
Blocking, Selecting socket_select
socket_set_block
socket_set_unblock
what – no poll?
![Page 40: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/40.jpg)
Poll vs. Select select - has existed for a great while and
exists almost everywhere.
poll - Not existing on older unixes nor in Windows before Vista. Broken implementation in Mac OS X at least up to 10.4
![Page 41: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/41.jpg)
Interoperability
<?php
$stream = stream_socket_server("udp://0.0.0.0:58380”,$errno, $errstr, STREAM_SERVER_BIND);
$sock = socket_import_stream($stream);
?>
socket_import_stream
![Page 42: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/42.jpg)
There’s a lot more! setting options
errors
READ THE C MAN PAGES!
![Page 43: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/43.jpg)
WebsocketsTCP on bidirectional full-duplex acid
![Page 44: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/44.jpg)
RFC 6455 HTML5 protocol
New and shiny
Take the place of ajax, polling, and other hackery
Allow cross origin communications
![Page 45: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/45.jpg)
Websockets are… based on TCP
look like an “upgrade” request to HTTP clients/servers
reduce latency and overhead
full-duplex on a single socket
usable with proxies
bi-directional
allow polling and streaming without pain
persistent…. wait - persistent?
![Page 46: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/46.jpg)
Hmmm - persistent Websockets are persistent connections
Your $server_of_choice may not like this
newer nginx is smart about proxying them
apache has some websockets modules
IIS8 and higher has support as well
use a standalone php server
If using fastcgi/php-fpm, you’re going to eat processes
![Page 47: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/47.jpg)
How DO they DO it?
![Page 48: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/48.jpg)
NIH will kill you Rachet - http://socketo.me
Wrench - https://github.com/varspool/Wrench
PHPWS - https://github.com/Devristo/phpws
![Page 49: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/49.jpg)
Wamp Websocket Application Messaging Protocol
“OMG THEY STOLE OUR ACRONYM!”
Async RPC
Async sub/pub
![Page 50: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/50.jpg)
Implementation https://github.com/cballou/php-websockets-
demos
https://developer.mozilla.org/en-US/docs/WebSockets/Writing_WebSocket_client_applications
http://www.binarytides.com/websockets-php-tutorial/
http://srchea.com/blog/2011/12/build-a-real-time-application-using-html5-websockets/
![Page 51: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/51.jpg)
Websockets Resources
http://www.websocket.org
http://wamp.ws
http://tools.ietf.org/html/rfc6455
http://socket.io (use to make client sides less stupid)
http://www.w3.org/TR/websockets/
![Page 52: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/52.jpg)
My Little Project
Pick a good C websockets library
libwebsockets - http://libwebsockets.org
Wrap it up for PHP
Profit???
![Page 53: Socket programming with php](https://reader035.fdocuments.us/reader035/viewer/2022062404/554bcb79b4c9058f6c8b46cd/html5/thumbnails/53.jpg)
Contact [email protected]
@auroraeosrose
http://emsmith.net
http://github.com/auroraeosrose
Freenode
#phpwomen
#phpmentoring
#php-gtk