944,122 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 11164
  • PHP RSS
Nov 23rd, 2006
0

About socket programming in php

Expand Post »
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...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
mahe4us is offline Offline
54 posts
since Sep 2006
Nov 24th, 2006
0

Re: About socket programming in php

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...
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Nov 24th, 2006
0

Re: About socket programming in php

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...
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Oct 15th, 2008
0

Re: About socket programming in php

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hairulazami is offline Offline
2 posts
since Oct 2008
Oct 17th, 2008
0

Re: About socket programming in php

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:

PHP Syntax (Toggle Plain Text)
  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:
PHP Syntax (Toggle Plain Text)
  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.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Oct 21st, 2008
0

Re: About socket programming in php

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:

PHP Syntax (Toggle Plain Text)
  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

PHP Syntax (Toggle Plain Text)
  1. <?
  2. $getContent = file_get_contents("http://192.168.0.7:1441/dds-includes/lib/downloadlist.txt");
  3. echo '<BR>'.$getContent;
  4. ?>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hairulazami is offline Offline
2 posts
since Oct 2008
Apr 27th, 2010
0
Re: About socket programming in php
thnxs for you
Reputation Points: 10
Solved Threads: 0
Newbie Poster
m-d88 is offline Offline
1 posts
since Apr 2010
Apr 27th, 2010
0
Re: About socket programming in php
I agree with above examples...
Reputation Points: 16
Solved Threads: 48
Posting Whiz
BzzBee is offline Offline
327 posts
since Apr 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Drop down list and JGraph
Next Thread in PHP Forum Timeline: Once one option is picked instatly display text





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC