{"id":62,"date":"2011-04-08T18:00:34","date_gmt":"2011-04-08T18:00:34","guid":{"rendered":"http:\/\/beta.robertprice.co.uk\/robblog\/2011\/04\/using_php_to_send_a_udp_message-shtml\/"},"modified":"2011-04-08T18:00:34","modified_gmt":"2011-04-08T18:00:34","slug":"using_php_to_send_a_udp_message-shtml","status":"publish","type":"post","link":"https:\/\/www.robertprice.co.uk\/robblog\/using_php_to_send_a_udp_message-shtml\/","title":{"rendered":"Using PHP To Send A UDP Message"},"content":{"rendered":"<p>\nPreviously I&#8217;ve written about sending <a href=\"http:\/\/en.wikipedia.org\/wiki\/User_Datagram_Protocol\">UDP<\/a> datagrams using Perl and JavaScript with node.js.\n<\/p>\n<p>\nAs I&#8217;ve been using a lot of <a href=\"http:\/\/www.php.net\/\">PHP<\/a> recently, I thought I&#8217;d show how to send data over UDP using PHP.\n<\/p>\n<p>\nIf you&#8217;ve not come across UDP before, it&#8217;s a simple protocol for sending messages over the internet that aren&#8217;t guaranteed to arrive. You just fire them off and hope they arrive.\n<\/p>\n<p>\nThe previous examples showed sending a simple heartbeat message, a string simply saying &#8220;PyHB&#8221;, to a server listening on port 43278. I&#8217;ll do the same in this example.\n<\/p>\n<p>\nFirstly we&#8217;ll setup a few variables saying where we&#8217;ll be sending the the message to, along with how often we want to send the message and what it should say.\n<\/p>\n<pre class=\"lang:php decode:true \" >$server_ip = '127.0.0.1';\n$server_port = 43278;\n$beat_period = 5;\n$message = 'PyHB';<\/pre>\n<p>\nNow we have to create the socket, using the <a href=\"http:\/\/php.net\/manual\/en\/function.socket-create.php\">socket_create<\/a> function. We need the domain to be of type <var>AF_INET<\/var> as we are using an IP4 address for our server, the type needs to be <var>SOCK_DGRAM<\/var> and the protocol needs to be <var>SOL_UDP<\/var>as we&#8217;re sending a UDP datagram.\n<\/p>\n<pre class=\"lang:php decode:true \" >$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP))\n<\/pre>\n<p>\nIt&#8217;s time to send the message now, and to do this we can use PHP&#8217;s <a href=\"http:\/\/php.net\/manual\/en\/function.socket-sendto.php\">socket_sendto<\/a> function. This function takes the parameters of the socket to send the message over, the message itself, the length of the message, some flags, the destination address of the message and the port to send it to. We&#8217;re not setting any flags so we can just set this field to 0. The following code will do the trick.\n<\/p>\n<pre class=\"lang:php decode:true \" >socket_sendto($socket, $message, strlen($message), 0, $server_ip, $server_port);\n<\/pre>\n<p>\nLet&#8217;s wrap this up in a loop that sends the message every <var>$beat_period<\/var> seconds.\n<\/p>\n<pre class=\"lang:php decode:true \" >&lt;php\n$server_ip   = '127.0.0.1';\n$server_port = 43278;\n$beat_period = 5;\n$message     = 'PyHB';\nprint \"Sending heartbeat to IP $server_ip, port $server_portn\";\nprint \"press Ctrl-C to stopn\";\nif ($socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) {\n  while (1) {\n    socket_sendto($socket, $message, strlen($message), 0, $server_ip, $server_port);\n    print \"Time: \" . date(\"%r\") . \"n\";\n    sleep($beat_period);\n  }\n} else {\n  print(\"can't create socketn\");\n}\n?&gt;\n<\/pre>\n<p>\nThis code is run on the command line, and not via a web server.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Previously I&#8217;ve written about sending UDP datagrams using Perl and JavaScript with node.js. As I&#8217;ve been using a lot of PHP recently, I thought I&#8217;d show how to send data over UDP using PHP. If you&#8217;ve not come across UDP before, it&#8217;s a simple protocol for sending messages over the internet that aren&#8217;t guaranteed to &hellip; <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/using_php_to_send_a_udp_message-shtml\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Using PHP To Send A UDP Message&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[2],"tags":[50,69,75],"class_list":["post-62","post","type-post","status-publish","format-standard","hentry","category-dev","tag-php","tag-udp","tag-web-development"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Using PHP To Send A UDP Message - Robert Price<\/title>\n<meta name=\"description\" content=\"An example of using PHP to send a message using UDP.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.robertprice.co.uk\/robblog\/using_php_to_send_a_udp_message-shtml\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using PHP To Send A UDP Message - Robert Price\" \/>\n<meta property=\"og:description\" content=\"An example of using PHP to send a message using UDP.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/using_php_to_send_a_udp_message-shtml\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2011-04-08T18:00:34+00:00\" \/>\n<meta name=\"author\" content=\"rob\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rob\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_php_to_send_a_udp_message-shtml\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_php_to_send_a_udp_message-shtml\\\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"Using PHP To Send A UDP Message\",\"datePublished\":\"2011-04-08T18:00:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_php_to_send_a_udp_message-shtml\\\/\"},\"wordCount\":299,\"keywords\":[\"PHP\",\"UDP\",\"Web Development\"],\"articleSection\":[\"Dev\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_php_to_send_a_udp_message-shtml\\\/\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_php_to_send_a_udp_message-shtml\\\/\",\"name\":\"Using PHP To Send A UDP Message - Robert Price\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\"},\"datePublished\":\"2011-04-08T18:00:34+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"description\":\"An example of using PHP to send a message using UDP.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_php_to_send_a_udp_message-shtml\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_php_to_send_a_udp_message-shtml\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_php_to_send_a_udp_message-shtml\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using PHP To Send A UDP Message\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\",\"name\":\"Robert Price\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\",\"name\":\"rob\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"caption\":\"rob\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Using PHP To Send A UDP Message - Robert Price","description":"An example of using PHP to send a message using UDP.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.robertprice.co.uk\/robblog\/using_php_to_send_a_udp_message-shtml\/","og_locale":"en_GB","og_type":"article","og_title":"Using PHP To Send A UDP Message - Robert Price","og_description":"An example of using PHP to send a message using UDP.","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/using_php_to_send_a_udp_message-shtml\/","og_site_name":"Robert Price","article_published_time":"2011-04-08T18:00:34+00:00","author":"rob","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rob","Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.robertprice.co.uk\/robblog\/using_php_to_send_a_udp_message-shtml\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/using_php_to_send_a_udp_message-shtml\/"},"author":{"name":"rob","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"Using PHP To Send A UDP Message","datePublished":"2011-04-08T18:00:34+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/using_php_to_send_a_udp_message-shtml\/"},"wordCount":299,"keywords":["PHP","UDP","Web Development"],"articleSection":["Dev"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/using_php_to_send_a_udp_message-shtml\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/using_php_to_send_a_udp_message-shtml\/","name":"Using PHP To Send A UDP Message - Robert Price","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website"},"datePublished":"2011-04-08T18:00:34+00:00","author":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"description":"An example of using PHP to send a message using UDP.","breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/using_php_to_send_a_udp_message-shtml\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/using_php_to_send_a_udp_message-shtml\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/using_php_to_send_a_udp_message-shtml\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"Using PHP To Send A UDP Message"}]},{"@type":"WebSite","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website","url":"https:\/\/www.robertprice.co.uk\/robblog\/","name":"Robert Price","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5","name":"rob","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","caption":"rob"}}]}},"_links":{"self":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/62","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/comments?post=62"}],"version-history":[{"count":0,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/62\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=62"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=62"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=62"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}