About socket programming in php

Reply

Join Date: Sep 2006
Posts: 54
Reputation: mahe4us is an unknown quantity at this point 
Solved Threads: 0
mahe4us's Avatar
mahe4us mahe4us is offline Offline
Junior Poster in Training

About socket programming in php

 
0
  #1
Nov 23rd, 2006
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...
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: About socket programming in php

 
0
  #2
Nov 24th, 2006
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...
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: About socket programming in php

 
0
  #3
Nov 24th, 2006
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...
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2
Reputation: hairulazami is an unknown quantity at this point 
Solved Threads: 0
hairulazami hairulazami is offline Offline
Newbie Poster

Re: About socket programming in php

 
0
  #4
Oct 15th, 2008
Originally Posted by digital-ether View Post
Yes, of course.
Take a look at the PHP stream (socket) functions: http://www.php.net/stream
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...

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
Last edited by hairulazami; Oct 15th, 2008 at 10:08 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: About socket programming in php

 
0
  #5
Oct 17th, 2008
Originally Posted by hairulazami View Post
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:

  1. 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:
  1. 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.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2
Reputation: hairulazami is an unknown quantity at this point 
Solved Threads: 0
hairulazami hairulazami is offline Offline
Newbie Poster

Re: About socket programming in php

 
0
  #6
Oct 21st, 2008
o'right, I just read details of the current function on my php manual cfm

so, If the condition like that, I can use this code to implementation it:

  1. <?
  2. //put file function
  3. function putItNow($source)
  4. {
  5. $saveto = "D:\dreserv\www\dredsync\dds-includes\images\down\img3.jpg";
  6. $putdata = fopen($source, "r");
  7. /* Open a file for writing */
  8. $fp = fopen($saveto, "w");
  9.  
  10. /* Read the data _MAX_UPLOADED_FILE KB at a time and write to the file */
  11. while ($data = fread($putdata, 1024))
  12. fwrite($fp, $data);
  13. fclose($fp);
  14. fclose($putdata);
  15. }
  16. putItNow("http://192.168.0.7:1441/dds-includes/images/syncronized/img3.jpg");
  17. ?>

OR BASED ON YOUR FUNCTION ADVICED

  1. <?
  2. $getContent = file_get_contents("http://192.168.0.7:1441/dds-includes/lib/downloadlist.txt");
  3. echo '<BR>'.$getContent;
  4. ?>
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC