Saturday, January 8, 2011

Creating a socket client in PHP

Hey!! Good thing that PHP supported SOCKET extension on PHP 5.0.0. It really help us developers to develop a socket server and client using only PHP libraries.

First, your server should be able to connect to socket server ip and port. a socket port was provision for client to connect in socket.

Once you have established your connection, you can now follow the simple steps below for you to be able to develop a socket client in PHP.

1. Define the socket server ip and port.

$socket_port = '<socket server port>';
$socket_ip = '<socket server ip>';


2. Create a TCP/IP socket. to get the error, you can use socket_sterror() function passing the error code parameter using socket_last_error().

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
echo socket_sterror(socket_last_error());


3. After the socket was successfully created, you can now send input data request using socket_write() function, parameters are the created socket object, the input data request, and the input data length.

$in = "<input data request>";
$len = strlen($in);
socket_write($socket, $in, $len);


4. To get the return data of the socket server request from no.3, you can use socket_read() function, parameters are the socket and the maximum length of the binary data.

$out = socket_read($socket, 2048);


5. And lastly, closing the socket by socket_close() function.

socket_close($socket);


Hope you were able to follow the steps above, you can try the complete code below, just define the socket server ip and port with a valid input data request.

<?php
error_reporting(E_ALL);

/* define socket server ip and port here.. */
$socket_port = '<socket server port>';
$socket_ip = '<socket server ip>';

/* create a tcp/ip socket.. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
   $error = socket_strerror(socket_last_error());
   echo "socket_create() failed: [$result] $error\n";
} else {
   echo "socket_create() ok.\n";
}

/* connect to socket server ip and port */
$result = socket_connect($socket, $socket_ip, $socket_port);
if ($result === false) {
   $error = socket_strerror(socket_last_error($socket));
   echo "socket_connect() failed: [$result] $error\n";
} else {
   echo "socket_connect() ok.\n";
}

$in = "<input data request>";
$len = strlen($in);

echo "sending input data request.\n";
socket_write($socket, $in, $len);
echo "socket_write() ok.\n";

echo "reading return data.\n";
while ($out = socket_read($socket, 2048)) {
   echo "socket_read() : $out";
}

echo "closing the socket.";
socket_close($socket);
echo "socket_close() ok.\n\n";
?>

No comments:

Post a Comment

Leadership 101


  • Leadership demands sacrifices for the near-term to receive lasting benefits. the longer we wait to make sacrifices, the harder they become. Successful people make important decisions early in their life, then manage those decisions the rest of their lives.
  • Growth does not happen by chance. If you want to be sure to grow, you need a plan something strategic, specific, and scheduled. it's a discipline that would need incredible determination from us.
  • Success comes by going the extra mile, working the extra hours, and investing the extra time. The same is true for us. If we want to get to excel in any segment of life, a little extra effort can help. Our efforts can go a long way if we only work a little smarter, listen a little better, push a little harder, and persevere a little longer.
  • Making a difference in your work is not about productivity; it's about people. When you focus on others and connect with them, you can work together to accomplish great things.
  • Envision a goal you'd like to reach. Make it big enough to scare you a little. Now write down a plan for moving toward it. Create mini-goals within the big goal, to set yourself up for continual progress. And include some risks, too. Set yourself up for success.
  • Leaders build margins, not image. A leader may be forced to take unpopular stands for the good of the company. Popularity isn't bad, but decisions made solely on the basis of popular opinion can be devastating. So take courage and make the right though sometimes painful choices.