Clintonb Posted March 19, 2017 Share Posted March 19, 2017 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? Quote Link to comment Share on other sites More sharing options...
requinix Posted March 19, 2017 Share Posted March 19, 2017 You need to learn about HTTP. This thing seems a reasonable explanation of it. Once you know HTTP you can send requests and receive responses over that socket. Quote Link to comment Share on other sites More sharing options...
gizmola Posted March 19, 2017 Share Posted March 19, 2017 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&* Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted March 19, 2017 Share Posted March 19, 2017 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", } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.