How to create and Use Sockets in PHP?

Posted on 2010-11-28
Last Modified: 2013-11-27
Please help me to create single Socket and Socket Arrays in PHP . How to operate DataArrival Socket Connect, Socket Error, Socket Close events in PHP in detail.

Question by:appinities
  • 6
  • 3

Accepted Solution

loopfinity earned 250 total points
ID: 34225241

Author Comment

ID: 34229438
Thanks "loopfinity" . Your reply is appreciable, But still ..lemme show you with an example what I want..

In visual basic 6.0 I am using a socking, its coding is like:

Im sending packets like:
Socket1.SendData (0), HextoAscii("02 03 8E 00 1A 00 00 00 07 00 01 00 00 00 01 01")

Open in new window

Here (0) is the Array of the socket..

Same thing for Data Arrival method in vb6.0 is:

Private Sub Sockm_DataArrival(ByVal Index As Variant, ByVal bytesTotal As Long)
On Error Resume Next
Dim TempString as String

Socket1.GetData Index, TempString, vbString
If  TempString = "2 1 9C 0 Then
   msgbox "Login Successfully"
  msgbox "Login Failed"

Open in new window

Same thing I wann do with the PHP sockets.. so please show me "How can I use above codings in PHP sockets"?

-Thanks alot in Advance..

Author Comment

ID: 34229447
Sorry for double posting, Ijn the above code
Private Sub Sockm_DataArrival

Open in new window

is Actually

Private Sub Socket1_DataArrival

Open in new window

LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 34264024
Have you gone through the user manual?

Author Comment

ID: 34264391
Thanks Tagit, that link should help me alot, but I want you to convert my above codes into PHP. Thanks
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.


Author Comment

ID: 34316473
Ok letme reduce the complexity of my question...

What should be the codings for : -

Suppose I wanna create a socket (TCP sockets not HTTP) with 5 arrays and wanna Connect these arrays with "" with Port "9009"

Now, After successfully connectivity , I wanna send a string to connected server.. string is "hello are you there?"

Now, If the connected server reply to my request, then I wanna display those responded message which are coming from the Server..
If server responding "Yes I am here"

then, how can I get responded message and can display it ?

-Thanks :(

Author Comment

ID: 34359413
Lolz..Still no reply... Where are experts ???? :@
LVL 42

Assisted Solution

by:Rob Jurd, EE MVE
Rob Jurd, EE MVE earned 250 total points
ID: 34365926
Here is a basic client/server using the attached Socket.php class for the server

Running on your server:

$my_socket = new Socket(100, 27, true);

while(!$my_socket->waitClient()) {

// now connected

$sent_text = $my_socket->read();

if (strlen($sent_text) > 0) {
	$my_socket->write("Yes I am here\n");


Open in new window

connect on the client
$socket=socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
while (!socket_connect($socket, "localhost", 27)) {
$message="hello are you there?\n";
$sent = socket_write($socket, $message);
if ($sent === false) {
	// Error occurred, break the while loop
	echo "error";
else {
	// Data sent ok
	echo "sent: $message<br/>";
	echo "received: " . read($socket);

function read($_socket) {
	$message = '';
	do {
		$buff     = socket_read($_socket,1,PHP_BINARY_READ);
		$message .= $buff;
	} while( $buff != "\n" );
	return $message;

Open in new window


Author Closing Comment

ID: 34367831
Thanks :)
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 34373085
no problem but why the B grade? works doesn't it?

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Shoutout to Emily Plummer ( for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now