Keep TCP/IP connection alive for subsequent accesses

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
quangnt
Forum Newbie
Posts: 2
Joined: Wed Apr 05, 2006 10:03 pm

Keep TCP/IP connection alive for subsequent accesses

Post by quangnt »

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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

PHP can store objects, regardless of complexity, in sessions provided the class is defined before the session is started again in subsequent requests. If your secondary connection does not keep the session alive, the object will be lost.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

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
quangnt
Forum Newbie
Posts: 2
Joined: Wed Apr 05, 2006 10:03 pm

Post by quangnt »

Here is 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);
?>
msn class is defined in another file, here snippet code in connection function

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))	
                    .....
}
...
}
?>
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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The server can choose to terminate the connection. You do not have any control over this unless it is your server, which it appears is not. However, it could also be your code, since you did not post all of it, it's quite hard to tell, especially in the very little you have posted.
Post Reply