Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Socket programming with php

  • Login to see the comments

Socket programming with php

  1. 1. Socket Programming with PHP Back to the Basics
  2. 2. 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”
  3. 3. IPC  Inter Process Communication  File  Signal  Socket  Pipe  Named Pipe  Semaphore  …
  4. 4. 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?
  5. 5. Network Sockets All things internet
  6. 6. 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
  7. 7. 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
  8. 8. Packet of Data
  9. 9. What are Sockets? Programming 101
  10. 10. Wormholes!  Sockets are just like "worm holes" in science fiction. When things go into one end, they (should) come out of the other.
  11. 11. Types  Connection-Oriented  “Ack?”  “Syn-Ack”  “Syn!”  Connectionless  0x49424d434f52504f524154494f3e32303036
  12. 12. 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
  13. 13. BSD Sockets Berkley / Posix and everything in between
  14. 14. 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
  15. 15. Network Socket Types  Stream  Connection oriented (tcp)  Datagram  Connectionless (udp)  Raw  Low level protocols
  16. 16. Where are we going?  Addresses  Ports
  17. 17. Doing it with PHP “Oh yeah, I guess PHP does have all those apis…”
  18. 18. Streams Making things dead easy
  19. 19. 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
  20. 20. How PHP Streams Work Stream Contexts Stream Wrapper Stream Filter ALL IO
  21. 21. 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
  22. 22. Built in Socket Transports  tcp  udp  unix  udg  SSL extension  ssl  sslv2  sslv3  tls
  23. 23. 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
  24. 24. Using Sockets
  25. 25. stream_socket_client
  26. 26. stream_socket_server
  27. 27. Sockets Extension All the power of C apis, all the pain of C apis
  28. 28. 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
  29. 29. Keep your man handy PHP  socket_create  socket_listen  socket_bind  socket_read C man page  socket  listen  bind  read
  30. 30. 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
  31. 31. Listen socket_bind  $socket  address  port socket_listen  $socket  backlog socket_create_listen  port  backlog  create  bind  listen
  32. 32. 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
  33. 33. Or Connect  socket_connect  $socket  address (ipv4 or 6 depending on what you set)  port
  34. 34. Read and Write  socket_write  $socket  data  len (will truncate to this!)  socket_read  $socket  length  type (stop on newlines or grab as binary)
  35. 35. Grab Data  socket_recv - usually connected socket  socket_recvfrom - from a specific address and port  socket_recvmsg – grabs a whole message (datagram’s friend)
  36. 36. Send Data  socket_send  socket_sendmsg  socket_sendto
  37. 37. All done!  socket_shutdown – stop reading and writing  socket_close – all done, close it down
  38. 38. Blocking, Selecting  socket_select  socket_set_block  socket_set_unblock  what – no poll?
  39. 39. 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
  40. 40. 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
  41. 41. There’s a lot more!  setting options  errors  READ THE C MAN PAGES!
  42. 42. Websockets TCP on bidirectional full-duplex acid
  43. 43. RFC 6455  HTML5 protocol  New and shiny  Take the place of ajax, polling, and other hackery  Allow cross origin communications
  44. 44. 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?
  45. 45. 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
  46. 46. How DO they DO it?
  47. 47. NIH will kill you  Rachet - http://socketo.me  Wrench - https://github.com/varspool/Wrench  PHPWS - https://github.com/Devristo/phpws
  48. 48. Wamp  Websocket Application Messaging Protocol  “OMG THEY STOLE OUR ACRONYM!”  Async RPC  Async sub/pub
  49. 49. Implementation  https://github.com/cballou/php-websockets-demos  https://developer.mozilla.org/en- US/docs/WebSockets/Writing_WebSocket_client_applic ations  http://www.binarytides.com/websockets-php-tutorial/  http://srchea.com/blog/2011/12/build-a-real-time- application-using-html5-websockets/
  50. 50. 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/
  51. 51. My Little Project  Pick a good C websockets library  libwebsockets - http://libwebsockets.org  Wrap it up for PHP  Profit???
  52. 52. Contact auroraeosrose@gmail.com @auroraeosrose http://emsmith.net http://github.com/auroraeosrose Freenode #phpwomen #phpmentoring #php-gtk

×