9 Replies - 9519 Views - Last Post: 07 February 2011 - 09:39 PM Rate Topic: -----

#1 KuroTsuto  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 42
  • View blog
  • Posts: 182
  • Joined: 13-February 09

WAMP server & Sockets

Posted 17 February 2009 - 12:45 PM

Hey,

one of the projects that I'm throwing around in my spare time involves a server-side application interacting with a MySQL database through PHP scripts. I'm not one to set up my entire webserver from scratch, so I simply utilized the WAMP tool to install them all. I would really like to remove the whole aspect of having my application interact through the HTTP protocol, and so I would like to use PHP for CLI scripts and just have them running in the background and listening on specified ports. However, WAMP seems to really dislike its included sockets module, for whenever I run a script from the command line through any of the three binaries (php.exe, php-win.exe, or php-cgi.exe), I am only returned with an error stating that the first socket-related function encountered is undefined. I have tried manually setting both of the php.ini files that appear in a WAMP install (php.ini and phpForApache.ini), yet neither seem to yield different results.

On one final note, I understand that this issue belongs more appropriately in the WAMP support forums, but it is no stretch to say the WAMP support is commonly less than ideal....

Thanks in advance for whatever aid you might offer ;)

Is This A Good Question/Topic? 0
  • +

Replies To: WAMP server & Sockets

#2 musya  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 11
  • View blog
  • Posts: 1,012
  • Joined: 25-April 07

Re: WAMP server & Sockets

Posted 18 February 2009 - 01:50 PM

Quick question why do you run it through one of the three binaries?
Was This Post Helpful? 0
  • +
  • -

#3 KuroTsuto  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 42
  • View blog
  • Posts: 182
  • Joined: 13-February 09

Re: WAMP server & Sockets

Posted 20 February 2009 - 12:28 PM

Sorry for my late reply... I run it through one of the three binaries because I am unaware of any other method to run a command-line script... is there something I might be missing?
Was This Post Helpful? 0
  • +
  • -

#4 pr4y  Icon User is offline

  • Location: 127.0.0.1
  • member icon

Reputation: 35
  • View blog
  • Posts: 621
  • Joined: 19-September 08

Re: WAMP server & Sockets

Posted 20 February 2009 - 12:31 PM

What you are missing is that you aren't supposed to "run" php.exe. What you need to do here is load the Shell, and execute your .PHP files by using the following command:

php /path/to/file.php



Hope this helps!
Was This Post Helpful? 0
  • +
  • -

#5 KuroTsuto  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 42
  • View blog
  • Posts: 182
  • Joined: 13-February 09

Re: WAMP server & Sockets

Posted 22 February 2009 - 10:48 AM

Thanks for the reply, and sorry for the late reply, once more. That is actually how I'm parsing/executing my script; opening the shell and executing the binary through the command line with the absolute path to my script as an argument. But instead of using
php /path/to/file.php
as you suggested, I am using
php -f /path/to/file.php
as -f is the command line option to parse and execute a script file. In really nothing more than desperation, I have replaced "php" with "php-cgi" and "php-win" as the original "php" is a command to execute the "php.exe" binary, and thus I thought attempting to parse/execute the script through one of the other two binaries might have a different result. I can assure you that the method by which I am executing the script is not at fault, for I may parse and execute any script that does not involve socket-related functions without error. It is more the proper integration of the socket module into WAMP that appears to be faulty, as per my original post.

This post has been edited by KuroTsuto: 22 February 2009 - 10:57 AM

Was This Post Helpful? 0
  • +
  • -

#6 cam888  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 64
  • Joined: 20-February 09

Re: WAMP server & Sockets

Posted 22 February 2009 - 01:49 PM

On Linux, command line php has a different php.ini, this may be the case for windows.
Was This Post Helpful? 0
  • +
  • -

#7 musya  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 11
  • View blog
  • Posts: 1,012
  • Joined: 25-April 07

Re: WAMP server & Sockets

Posted 22 February 2009 - 02:00 PM

Honestly it might just be a windows problem not the install of the server.
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,516
  • Joined: 23-August 08

Re: WAMP server & Sockets

Posted 22 February 2009 - 08:00 PM

Search your drive for php.ini (although it's likely just underneath the wamp install directory) and open it.

Find the following line:
;extension=php_sockets.dll


Delete the leading semi-colon (which comments that line out).

Save the file.

Enjoy socket goodness. For example this program:
<?php
    $socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname("tcp"));
    if (!$socket)
    {
        die("Socket creation failed: " . socket_last_error());
    }
    
    if (!socket_connect($socket, "208.67.217.230", 80))
    {
        echo("Socket connect failed: " . socket_last_error());
        socket_close($socket);
        exit();
    }
    
    $request = "GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n";
    if (!socket_send($socket, $request, strlen($request), 0))
    {
        echo("Socket send failed: " . socket_last_error());
        socket_close($socket);
        exit();
    }
    
    $response = "";
    if (!socket_recv($socket, $response, 8192, 0))
    {
        echo("Socket recv failed: " . socket_last_error());
        socket_close($socket);
        exit();
    }
    
    echo ("Received $response \n");
    socket_close($socket);
?>


Returns:
C:\Documents and Settings\Jack>\wamp\bin\php\php5.2.6\php.exe -f sock.php
Received HTTP/1.0 200 OK
Cache-Control: private, max-age=0
Date: Mon, 23 Feb 2009 02:57:18 GMT
Expires: -1
Content-Type: text/html; charset=ISO-8859-1
Set-Cookie: PREF=ID=c9a669240fbcea34:TM=1235357838:LM=1235357838:S=byc9eIiH5Mc4K
GQl; expires=Wed, 23-Feb-2011 02:57:18 GMT; path=/; domain=.google.com
Server: gws
X-Cache: MISS from .
Via: 1.0 .:80 (squid)
Connection: close



C:\Documents and Settings\Jack>

Was This Post Helpful? 0
  • +
  • -

#9 KuroTsuto  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 42
  • View blog
  • Posts: 182
  • Joined: 13-February 09

Re: WAMP server & Sockets

Posted 25 February 2009 - 01:44 PM

Thanks for the long and detailed reply ;). Unfortunately, to quote my original post,

Quote

I have tried manually setting both of the php.ini files that appear in a WAMP install (php.ini and phpForApache.ini), yet neither seem to yield different results.


But what you have told does confirm that my attempts were carried out correctly... Perhaps this is some sort of incidental flaw in my environment, after all... I suppose I'll start digging, lol.

Thanks for the help!
~KuroTsuto
Was This Post Helpful? 0
  • +
  • -

#10 setprem  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 07-February 11

Re: WAMP server & Sockets

Posted 07 February 2011 - 09:39 PM

I had the same problem with wamp. so i editted php.ini and phpForApache.ini and enabled php_sockets.dll.
I tried the code posted by JackOfAllTrades. It was working when i tried it from cmd.exe. But when i opened through web browser it is showing the error socket_create() function not defined...:-(
My project needs to execute a code containing this function when users access the page through web browser.
So please help me to fix the problem.....

This post has been edited by setprem: 07 February 2011 - 09:41 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1