About socket programming in php
Dear All,
Sorry for my frequent questions in this forum. I have came here with a lot of questions. I need help in socket programming in php. For example, Lets consider I have a ip number and port number. Using this ip and port values i want to send a message to server though sockets. The server handles the message and reply to my machine through sockets. After that I wants to display the message in the front end.
Its possible to obtain this using php sockets....
Please guide me....
Thanks in advance...
mahe4us
Junior Poster in Training
55 posts since Sep 2006
Reputation Points: 10
Solved Threads: 0
Yes, of course.
Take a look at the PHP stream (socket) functions: http://www.php.net/stream
If you look at the page about at the example: Making a POST request to an https server, you'll notice it is a socket connection to the port 443 over SSL.
Stream functions allow any protocol however.
Usually on a socket connection, you will do something like:
Open a socket using fsockopen().
write to the socket with fwrite()
read from the stream with fread() or fgets() till end of the stream, feof()
wait a few seconds with sleep()
read.. write.. etc.
What protocol are you using this for? PHP may have abstracted functions for each protocol so you don't have to deal with the raw socket...
digital-ether
Nearly a Posting Virtuoso
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
dont forget to close the socket connection with fclose(). On some systems the memory allocated for this will be allocated indefinitely unless you close the socket specifically...
digital-ether
Nearly a Posting Virtuoso
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
I have a same question. I mean, if I want to send a little bit of message (ex: "pingIPok") from IP 192.168.0.1:80 to IP 192.168.0.7:80, and the IP target will replay with message (ex: "message receive for pingIPok") using socket function on php (port = 80). Then make a record of sql table for each of messages sent and received. How to do it with PHP, please give me an example with complete code.
Thank for your replay :) I hope you will replay my question
In this case you can just use:
file_get_contents();
What you want to do is make a http socket connection over the regular port 80, so this is nothing more then downloading a webpage over HTTP.
So if you had a PHP page, called: receive_ping.php
Then you could just have the code to write to the database in that.
On your remote server do:
file_get_contents('http://remote_site.com/receive_ping.php');
The php page can gather the IP of the site doing the ping and other info and insert that into the database.
digital-ether
Nearly a Posting Virtuoso
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101