note 82163 added to ref.sockets

From: Date: Sat, 29 Mar 2008 20:31:46 +0000
Subject: note 82163 added to ref.sockets
Groups: php.notes 
Request: Send a blank email to php-notes+get-137729@lists.php.net to get a copy of this message
Here's a simple script for sending messages back and forth between a server and client. At this point, the code is fairly rough because once it enters the while loop, it doesn't stop but it can be modified and fixed. Enjoy. <?php //The Server error_reporting(E_ALL); $address = "127.0.0.1"; $port = "10000"; /* create a socket in the AF_INET family, using SOCK_STREAM for TCP connection */ $mysock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); socket_bind($mysock, $address, $port); socket_listen($mysock, 5); $client = socket_accept($mysock); echo "Server started, accepting connections...\n"; $i = 0; while (true == true) { $i++; echo "Sending $i to client.\n"; socket_write($client, $i, strlen($i)); $input = socket_read($client, 2048); echo "Response from client is: $input\n"; sleep(5); } echo "Closing sockets..."; socket_close($client); socket_close($mysock); ?> <?php //The Client error_reporting(E_ALL); $address = "127.0.0.1"; $port = 10000; /* Create a TCP/IP socket. */ $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); if ($socket === false) { echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n"; } else { echo "socket successfully created.\n"; } echo "Attempting to connect to '$address' on port '$port'..."; $result = socket_connect($socket, $address, $port); if ($result === false) { echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n"; } else { echo "successfully connected to $address.\n"; } $i = 0; while (true == true) { $i++; echo "Sending $i to server.\n"; socket_write($socket, $i, strlen($i)); $input = socket_read($socket, 2048); echo "Response from server is: $input\n"; sleep(5); } echo "Closing socket..."; socket_close($socket); ?> ---- Server IP: 64.71.164.2 Probable Submitter: 207.32.33.15 ---- Manual Page -- http://www.php.net/manual/en/ref.sockets.php Edit -- https://master.php.net/note/edit/82163 Del: integrated -- https://master.php.net/note/delete/82163/integrated Del: useless -- https://master.php.net/note/delete/82163/useless Del: bad code -- https://master.php.net/note/delete/82163/bad+code Del: spam -- https://master.php.net/note/delete/82163/spam Del: non-english -- https://master.php.net/note/delete/82163/non-english Del: in docs -- https://master.php.net/note/delete/82163/in+docs Del: other reasons-- https://master.php.net/note/delete/82163 Reject -- https://master.php.net/note/reject/82163 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#137729) next »