Hi gurus,
I has faced one problem for a week and I has not found out the solution. Can you help me to solve it?
My Web Server connects to another Server that is made by myself through TCP/IP sockets.
(Web Server(Apache, PHP)) <==TCP/IP==> (another Server)
If client requests a page:
1. Web Server creates connection with "another Server"
2. Get information from "another server"
3. Return the page to the client
At after step 3, the web server closes connection immediately. I want to keep the connection alive for subsequent pages. This is my problem. Do you know the reasons? Thank you very much.
The socket ID is saved in a class, the class is saved in $_SESSION array. But the class can not be shared among subsequent accesses. Maybe PHP can keep atomic data in $_SESSION array, but PHP can not keep complex objects in $_SESSION array?. It is why when, after the step 3, the server destroy complex objects, and the connection is destroyed too. Am I right?
Keep TCP/IP connection alive for subsequent accesses
Moderator: General Moderators
The easiest way to do this with PHP based on my knowledge of the language would be to have a shell script invoked in the background that the current request can talk to on the command line (or by communicating via a .tmp file your script will write to disk), have the shell script (you can write shell scripts in PHP) started with nohup ./file.php & (will run in hte background and be immune to hangups). The reason being is even though you are putting your handle in a session as soon as the file ends the connection is closed, if you just want the connection to remain open you can tell the script to keep running but a new instance of your script will be invoked on the next request
Here is my code,
1. Client request login.php, here code in login.php
msn class is defined in another file, here snippet code in connection function
2. When login.php is returned to the client, the established connection is closed immediately. I want to keep this connection alive for subsequent access.
Is there any bugs in my code?
1. Client request login.php, here code in login.php
Code: Select all
<?php
start_session();
$_SESSION['msn'] = new msn;
$_SESSION['msn']->connect($Email, $Password);
?>Code: Select all
<?php
class msn{
....
function connect($passport, $password)
{
$this->trID = 1;
$this->MyEmail = $passport;
$this->Login = false;
if ($this->fp = @fsockopen(SERVER_IP, SERVER_PORT, $errno, $errstr, 2))
.....
}
...
}
?>Is there any bugs in my code?