Jump to content

Connecting to php socket


Clintonb

Recommended Posts

Ive done quite a bit of php programming for websites but i stumbled on a post where someone built a windows tcp client in delphi the communicated with a php script using sockets a bit like a chat server.

 

How does php work that you are able to do this. I created a tcpclient app that connects to 127.0.0.1 with my wamp running and i have a connection. How do i send the request to that specific php script that will receive my request and respond?

Link to comment
Share on other sites

When you are using PHP as part of a web server through cgi or an apache module, then the http server is handling HTTP (tcp wrapped) connections.  

 

This is obviously not what you want.  The webserver is in the way, and it is also wired to communicate via HTTP, which is a request/response protocol that is not inherently persistent.

 

So when you create a listening application you have to utilize command line php.

 

Another popular alternative to writing our own socket server, would be to utilize a websocket server.  There are some servers out there already, written in PHP.  

 

See:  https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=php+websockets+server&*

Link to comment
Share on other sites

Look into ReactPHP, and try the following.

<?php
require 'vendor/autoload.php';
$loop = \React\EventLoop\Factory::create(); 
$socket = new \React\Socket\Server($loop);
$socket->on('connection', function (\React\Socket\ConnectionInterface $stream) {
    $stream->on('data', function($data) use ($stream){
        $stream->write("send $data back to client");
    });
});
$socket->listen(1337,'0.0.0.0');
$loop->run();
{
    "require": {
        "react/socket": "^0.4.4",
        "react/stream": "^0.4.5",
        "react/event-loop": "^0.4.2",
    }
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.