Im trying to understand how a socket works in PHP.
Lets say I have a file called socket.php, and this creates a socket bound to my localhost on port 99.
Then I run the socket in a while loop so it's constantly connected.
is there a function in PHP to make calls to that socket while its listening?
Another question is: If I have another service such as Java running on a socket -- is it a bad idea to use PHP to connect to the socket to make a call. I ask because I could potentially be recreating new socket connections many, many times.
So is having to reconnect to a socket hundreds of times in PHP bad? Or should I re-use the same socket connection somehow? (I am thinking in terms of AJAX calls to PHP which connects to a Java Socket).
Edit: You can see the example code: https://github.com/JREAM/sandbox/tree/master/php
Im trying to communicate with in socket.php and socket_send.php -- I am leaving socket.php running and opening another console and running socket_send.php and trying to get a result into the console.
Answer to your first Question: I suggest going over here everything you need about sockets is there. Basically the function you want to use is socket_read or socket_recvfrom if using UDP.
Answer to your second Question: Sockets are just a way to send messages to services. It doesn't matter if a client is in php and the server is in Java. Think of it this way. Does it matter that you are viewing a web-page on a linux Web Server with a windows Box?
Related
This is my first question on this site so I'll try to be as clear as possible.
We're building an EPP client to talk to Nominet EPP service. Nominet limits the sessions to be established by us to 5 sessions at a time. Source: http://registrars.nominet.uk/gtlds/gtld-registrar-systems/epp-gtlds
The problem that we're tackling here is we can't create a new socket every time the new Apache request comes in because of we might have 10 users that use this function at the same time.
What I'm thinking right now is we implement one script that creates 5 sockets and keeps them alive by sending hello command to Nominet occasionally (before the timeout, obviously). Then when the new process created by Apache, that process will use the exists sockets to send the command to Nominet.
The problem is I did a lot of research but can't find any way to do this, so I ended up looking for someone who knows more about this stuff.
Here is what I found but it's not what we want (not include some URL that I forgot to save while I was doing the research): PHP Threads Sharing a Central Socket Object
Anyone know how to implement the sharing socket schema? I would be more than appreciated if someone can help on this.
Also, if you know the solution in another language, please do tell. We're open for the new languages and architecture.
Thanks :)
Just finally got it works, I use a socket to solve this problem. This is how:
Process A connect to Nominet and keep the connection alive. It also opens the listener at port X.
Process B (apache process) will send the XML to process A via socket port X, then port A will forward that to the opening socket to Nominet and send XML back to process B.
For a new project I need to implement remote desktop protocols. The addresses of the remote need to be secured and may never get sent to the client. After a lot of research and some tests I found Guacamole, which also has a Java client. The project is designed as an API though, so I started porting some bits of the Java client example to PHP.
The use case will be the following:
User logs into my service (Laravel application)
WebSocket connection establishes to a constantly running PHP script (using HOA\WebSocket)
Upon authorization a TCP socket needs to be established to the Guacamole Daemon
Commands coming via WebSocket need to be directed to the Guacamole Daemon and vice versa
What makes this complicated is the fact that the application needs to be able to serve multiple clients simultaneously. Multiple TCP sockets need to be established and multiple WebSocket connections need to be managed all at once.
For my simple test I opened the socket via fsockopen and then looped to wait for data. With this I obviously can't listen to multiple sockets at once (at least realistically), but I stumbled upon the React Socket Client library:
Think of this library as an async version of fsockopen() or stream_socket_client().
This sounds like it is what I need, but then again, I'm using HOA and its WeSocket server, which apparently also runs in a loop (when invoking WebsocketConnectionHandler->run()).
Should I even be using React's Socket Client or should I try to use HOA's Socket library instead (seeing as I'm already using WebSocket from that)? Are React and HOA even compatible in their event loop, so could I listen to WebSocket clients and a TCP connection at the same time?
If so, could anyone give me some hints or examples on how to get started with coupling these two? Thanks!
I have a tcp socket service MyServ running on the background (using Java, doesn't really matter though), and a web server with php that accesses MyServ using persistent socket (pfsockopen).
The problem is, if one php request stopped for whatever reason, it leaves some un-read data in the persistent socket, and the following php request will get an error when reading this socket.
I wonder how's other services with similar scenario (like php-mysql, php-memcached) dealing with this problem? more specificly, how can php tell that a used persistent socket is clean?
I have an application written in VB.net that runs on a clients pc.
I also have a website written in mostly javascript, http and php.
The thing I want to do is to connect the website to the application, so that when i.e. a certain button is pressed, it connects to the client application and raises an event.
I have tried approaches like TCP socket communication by having a TCP Socket Server running in the background of the client application. I can connect to the server by having a client connection from another vb.net application, but whenever I try to connect through PHP it fails. (I have only tried PHP since server-side scripting seems to make more sense in this case)
Another approach I have tried is to have an HTTP server running in the background of my desktop application and then have a PHP script connect to it, that fails as well.
One thing that I've been thinking about as a last resort is to simply have a textfile on the webserver and a PHP script writing to it after given parameters and then have the client application to read the file every few seconds. But this wouldn't be very efficient with larger amounts of data, would it?
What is the proper way of doing this?
If you have any questions about the code I've been using, feel free to ask.
If you don't get my blurry explanation, try this image: http://i.imgur.com/8njxVFj.png
Thanks in advance.
To have your data more organized i would suggest you to store your data on a database server for example mysql (which is free).
I'm trying to write a php code that acts as a server. All it would do is connects two socket, it opens a file socket and pipes everything from it into another socket that it connects to. It would run 24/7. I'm not familiar with PHP but this project requires it. Any help?
Here's a great resource that does exactly what you need:
http://devzone.zend.com/article/1086
An actual answer would be pretty long winded for this Q/A format...