Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to create and Use Sockets in PHP?

Posted on 2010-11-28
11
Medium Priority
?
513 Views
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.

-Thanks
0
Comment
Question by:appinities
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
11 Comments
 
LVL 6

Accepted Solution

by:
loopfinity earned 750 total points
ID: 34225241
0
 

Author Comment

by:appinities
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"
Else
  msgbox "Login Failed"
Endif

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..
0
 

Author Comment

by:appinities
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

0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 43

Expert Comment

by:Rob
ID: 34264024
Have you gone through the user manual?

http://php.net/manual/en/book.sockets.php
0
 

Author Comment

by:appinities
ID: 34264391
Thanks Tagit, that link should help me alot, but I want you to convert my above codes into PHP. Thanks
0
 

Author Comment

by:appinities
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 "gateway.abcdxyz.com" 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..
Example:
If server responding "Yes I am here"

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

-Thanks :(
0
 

Author Comment

by:appinities
ID: 34359413
Lolz..Still no reply... Where are experts ???? :@
0
 
LVL 43

Assisted Solution

by:Rob
Rob earned 750 total points
ID: 34365926
Here is a basic client/server using the attached Socket.php class for the server

Running on your server:
<?php
require('Socket.php');

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

while(!$my_socket->waitClient()) {
	sleep(100);
}

// 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
<?php
ob_start();
$socket=socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
while (!socket_connect($socket, "localhost", 27)) {
	ob_flush();
	sleep(1);
}
$message="hello are you there?\n";
$sent = socket_write($socket, $message);
if ($sent === false) {
	// Error occurred, break the while loop
	echo "error";
	break;
}
else {
	// Data sent ok
	echo "sent: $message<br/>";
	echo "received: " . read($socket);
}
ob_end_flush();

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

Open in new window

Socket.php
0
 

Author Closing Comment

by:appinities
ID: 34367831
Thanks :)
0
 
LVL 43

Expert Comment

by:Rob
ID: 34373085
no problem but why the B grade? works doesn't it?
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

664 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