Free Web Hosting Forum
(#1 (permalink))
Old
Junior Member
Zalastax is on a distinguished road
 
Posts: 4
Join Date: May 2012
Default PHP sockets, how do I start it? - 05-10-2012, 07:23 AM

I want to use sockets in PHP but I don't know how to start it. Just creating a .php file with the socket script in doesn't seem enough.
Please tell me what are the necessary code to get it running. I would really appreciate a test script that just accept and then disconnect a socket.

I have searched the forums and I found a test script here: fsockopen() - they say it's enabled - doesn't work but it was in an attachment that was invalid.
Reply With Quote
Sponsored Links
(#2 (permalink))
Old
Senior Member
grace1004 is on a distinguished road
 
Posts: 761
Join Date: Dec 2010
Default 05-10-2012, 10:25 AM

I got the following example code from php.net. When testing, it works fine.

PHP Code:
<?php
$fp 
fsockopen("yourdomain.com"80$errno$errstr30);
if (!
$fp) {
    echo 
"$errstr ($errno)<br />\n";
} else {
    
$out "GET / HTTP/1.1\r\n";
    
$out .= "Host: yourdomain.com\r\n";
    
$out .= "Connection: Close\r\n\r\n";
    
fwrite($fp$out);
    while (!
feof($fp)) {
        echo 
fgets($fp128);
    }
    
fclose($fp);
}
?>
Reply With Quote
(#3 (permalink))
Old
Junior Member
Zalastax is on a distinguished road
 
Posts: 4
Join Date: May 2012
Default 05-10-2012, 12:20 PM

Thank you for the reply. It did work. But in that example the script is acting client. Is it possible for it to act as server too? What IP and port should be used when acting server? If I would use the first example on this page: http://php.net/manual/en/sockets.examples.php I would need to change the IP to the server IP and the port to an open port, since it is free I know all ports aren't open.
As I have understood usually you have to start the socket server from a command line?
Reply With Quote
(#4 (permalink))
Old
Senior Member
grace1004 is on a distinguished road
 
Posts: 761
Join Date: Dec 2010
Default 05-10-2012, 03:32 PM

Do you think it's possible to do server-side socket test without having own server?
Reply With Quote
(#5 (permalink))
Old
Junior Member
Zalastax is on a distinguished road
 
Posts: 4
Join Date: May 2012
Default 05-10-2012, 05:01 PM

Well since you can specify a host name to connect to and since they handle DNS I didn't think it was impossible. The request could be handled like when you connect on port 80. Since they say sockets work I thought it was possible.

So is it impossible?
Reply With Quote
(#6 (permalink))
Old
Senior Member
grace1004 is on a distinguished road
 
Posts: 761
Join Date: Dec 2010
Default 05-10-2012, 06:18 PM

PHP Code:
<?php
error_reporting
(E_ALL);

echo 
"<h2>TCP/IP Connection</h2>\n";

/* Get the port for the WWW service. */
$service_port getservbyname('www''tcp');

/* Get the IP address for the target host. */
$address gethostbyname('mydomain.com');

/* Create a TCP/IP socket. */
$socket socket_create(AF_INETSOCK_STREAMSOL_TCP);
if (
$socket === false) {
    echo 
"socket_create() failed: reason: " socket_strerror(socket_last_error()) . "\n";
} else {
    echo 
"OK.\n";
}

echo 
"Attempting to connect to '$address' on port '$service_port'...";
$result socket_connect($socket$address$service_port);
if (
$result === false) {
    echo 
"socket_connect() failed.\nReason: ($result) " socket_strerror(socket_last_error($socket)) . "\n";
} else {
    echo 
"OK.\n";
}

$in "HEAD / HTTP/1.1\r\n";
$in .= "Host: mydomain.com\r\n";
$in .= "Connection: Close\r\n\r\n";
$out '';

echo 
"Sending HTTP HEAD request...";
socket_write($socket$instrlen($in));
echo 
"OK.\n";

echo 
"Reading response:\n\n";
while (
$out socket_read($socket2048)) {
    echo 
$out;
}

echo 
"Closing socket...";
socket_close($socket);
echo 
"OK.\n\n";
?>
After running example #2 code (http://php.net/manual/en/sockets.examples.php), I got the following response:

Quote:
TCP/IP Connection
OK. Attempting to connect to '31.170.160.169' on port '80'...OK. Sending HTTP HEAD request...OK.
Reading response: HTTP/1.1 200 OK Date: Thu, 10 May 2012 17:56:23 GMT Server: Apache X-Powered-By:
PHP/5.2.17 Connection: close Content-Type: text/html Closing socket...OK.
Isn't this what you want?

To test example #1 code, you need to have your own server for executing php script on shell mode.

Last edited by grace1004; 05-10-2012 at 06:24 PM.
Reply With Quote
(#7 (permalink))
Old
Junior Member
Zalastax is on a distinguished road
 
Posts: 4
Join Date: May 2012
Default 05-10-2012, 07:21 PM

No i wanted it to act as a server and wait for incoming connections, but since that is impossible I will see if i can set up my own server instead.
Reply With Quote
Reply

Tags
fsockopen, php, socket

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.5.2
vBulletin Skin developed by: vBStyles.com