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

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

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

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

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.

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:

<?
//put file function
function putItNow($source)
{
	$saveto = "D:\dreserv\www\dredsync\dds-includes\images\down\img3.jpg";
	$putdata = fopen($source, "r");
	/* Open a file for writing */
	$fp = fopen($saveto, "w");

	/* Read the data _MAX_UPLOADED_FILE KB at a time and write to the file */
	while ($data = fread($putdata, 1024))
	fwrite($fp, $data);
	fclose($fp);
	fclose($putdata);
}
putItNow("http://192.168.0.7:1441/dds-includes/images/syncronized/img3.jpg");
?>

OR BASED ON YOUR FUNCTION ADVICED

<?
$getContent = file_get_contents("http://192.168.0.7:1441/dds-includes/lib/downloadlist.txt");
echo '<BR>'.$getContent;
?>

thnxs for you

I agree with above examples...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.