Questions about sockets in php? - php

1- Let's say my computer ip address is 111.11.111.11, and the server that my php script is on is 222.22.222.22, so if i access and run the php script that is on the server and start a socket server, which ip do my clients need to connect to?
2- Is it possible to have a socket running on php which keeps reading, and responding to the clients until I close the browser, So basically what i'm trying to do is to start a socket which keeps reading, and accepting clients, and keeps communicating with them multiple times with each.
thanks for the answer, but i think i didn't explain well enough on my question 2, so let me make it easier:
Is it possible to create a chat server using php? because the point i was getting into was if it's possible to accept multiple clients and keep them alove.

222.22.222.22. But it sounds like you are starting up a socket server in response to a HTTP request. Probably, that won't work as intended, since the PHP interpreter terminates after the response is sent. If you had permissions, you could fork a separate socket server process, but I don't know what that would accomplish.
No. Even if you kept the interpreter running, there is no way to tell when the browser closes. The closest you can get is determining the browser (as determined by cookies or IP) stops communicating with you.

1- 222.22.222.22, your server's IP.
2- When a visitor arrives you can spawn a 'socket process' and implement a client side 'heartbeat' application using JavaScript/AJAX, but that implies you running the socket backend script (possibly) for a long time, which may cause problems (Like having a lot of PHP processes open, depending on the way your web server is set up this may cause problems)

Related

PHP - Apache process use the created socket by another process

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.

Make socket resource accessible through several pages in php

In PHP, is there any way to create, connect and maintain an open (socket)connection so that the connection object can be accessed on several pages?
Imagine a small windows based client program that connects to its server software when you start it. You can send a couple of commands to the server using buttons(lets say start and stop calculation), and receive confirmations on my input.
In this example, when "started", the server will do some counting/calculations and send some values every second to the client which shows this every second until the server is done with its job or you send the "stop" command. The client will also stay connected until you close the program or click some disconnect-button.
How could this client work as a web client running in PHP on the same box as the server software?
I managed to send commands and receive feedback using socket or pfsockopen, but I'm having trouble maintaining the connection since I don't want to reload the page.
I don't want to reload the page every second, so I figured I can use Ajax to execute PHP scripts to read/write to the server without reloading the page, but the script files I execute on the web server can't find my connection resource. I tried saving the resource in session with no luck.
Alternatively, is there any better way of achieving this?
Also: the server software is an old piece of VB6 software, and the web server is on the same box as the VB6 software. The PHP site will work as an interface.

Website to connect to a client desktop application

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).

PHP Socket Help Understanding

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?

Website to computer communications

I am working on my senior project at university and I have a question. My advisor and other workers don't know much more on the matter so I thought I would toss it out to SO and see if you could help.
We want to make a website that will be hosted on a server that we are configuring. That website will have buttons on it, and when visitors of that website click a certain button we want to register an event on the server. We plan on doing this with PHP.
Once that event is registered (this is where we get lost), we want to communicate with a serial device on a remote computer. We are confident we can set up the PHP event/listener for the button press, but once we have that registered, how do we signal to the remote computer(connected via T1 line/routers) to communicate with the serial device? What is this sequence of events referred to as? The hardest thing for us (when researching it) is that we are not certain what to search for!
We have a feeling that a python script could be running on the server, get signals from the PHP listener, and then communicate with the remote PC. The remote PC could also be running a python script that then will communicate with our serial device. Again, most of this makes sense, but we are not clear on how we communicate between Python and PHP on the web server (or if this is possible).
If any one could give me some advice on what to search for, or similar projects I would really appreciate it. Thanks,
Both php and python can communicate via sockets, so I guess that is a good bet.
In PHP, when you receive the signal from the click button, open a socket to your python app with socket_create.
In python, you would need to implement a server socket (SocketServer) that listens for a connection.
Try reading up on sockets communication in general and socket programming in php and python specifically.
You can set up a web server also on the remote computer, perhaps using the same software as on the public server, so you do not need to learn another technology. The public server can make HTTP requests and the remote server responds by communicating with the serial device.

Categories