Jump to content

How to run server script continuously and send the update data


Antrikssh

Recommended Posts

Hello Everyone,I am working on site creation and I am newbie with this work. I have used php for creating my site.I want to create server script that run on my machine continuously and send the updated data to the client who want that data. I have created two sample code one is Server.php and another is Client.php.Server.php<?phpsession_start();$_SESSION['Argument'] = 10;while(1) {print "Updated data in Server Script ".$_SESSION['Argument'];$_SESSION['Argument'] += 20;sleep(5);}?>Client.php<?phpsession_start();print "Updated data in Client Script ".$_SESSION['Argument'] ;?> I have ran this Server.php Script from command prompt and I want that updated data I will get on Client.php script whenever I run client script. But the problem is when I ran Server.php script through command line it ran as per instruction but when I run Client Script through command line/ Browser it is in hold(Connecting...) process. I thought that Server.php never lose any resources for client script even I gave sleep() for 5 sec. My question is that I will get my updated data from server when server run continously on my machine. I dont want to use Socket method.Thanks and Regard,Antrikssh... smile.gif

Link to comment
Share on other sites

Yes I use Linux, but corn is used to execute the script on sepecific interval of time minimum(1 min) maximum that you allocate. If my script is execute in every 1 min. and I will get the the updated data after every 1 min then its not a good solution for that because there is lot of user who use my site on a single time or in difference of sec then how the get from server. I hope you understand my problem and requirement. Thanks...

Link to comment
Share on other sites

The browser never stops trying to connect because the "server" script never stops. What's the problem with using a socket connection? What problem are you trying to solve by having the script never end? You'll never be able to use that script for a client, the client will time out waiting for a response which never finishes. Also, the session has no meaning on a command line. A session is part of HTTP, which doesn't happen when you run a script on a command line. http://www.php.net/manual/en/features.commandline.php

Link to comment
Share on other sites

As I was said in my first post that I am waorking with the site (for learning purpose) and I am new with this work. I have created my client pages. Now I want to build server script for that. Server script is running on command line continuously as per my knowledge(if I am wrong then please correct me). I want to define all the database operation in server script. Now If I run my server script then my database connection will make once at that time and that will use by each client script. I have used Socket connection but I dont want to do my task by that way. I want to do my task as I am thinking. Create one database connection in server and take the request from different user perform the operation in server and send the result to client.And server must running continuously.

Link to comment
Share on other sites

If you want a continuously running server listening for clients then PHP is not the tool for that. Java or C++ would be a better choice to build a server, but if you're not using socket connections then I don't know how you're planning on getting your clients to connect to it. The server needs to listen on a socket in order for a client to connect to it. A PHP script that never ends is not a server. When you send a request to that script you're not connecting to some already-running script, you're telling Apache or whatever your web server is to make a new request for that script and start a new instance of it. If you want persistent database connections then you can use persistent connections across multiple scripts, PHP and MySQL support that. When people build "servers" with PHP they do not run continuously like Apache does. A SOAP server in PHP, for example, is a PHP script that gets requested like any other PHP script and reads the request data and sends a response similar to how a web server does. The request and response are just formatted a certain way, like XML or JSON or whatever it supports. So, your requirements are contradictory. If you want a server running continuously then you use sockets to connect to it, unless you can think of another way to tell a client how to connect to a running server without using a socket.

Link to comment
Share on other sites

Hello, I am sorry for my late reply, Mr. Moderator thanks for giving me such a pricious knowlege to us. Can you please help me to build my server script because I want to learn that , In php how I will build my server. You Kow that what I am requiring with my server. Please help me and teach me about the server site programming.I used socket programming but its very lengthy way. Is there is any other alternative for my requirment. In corn problem is minimium time of script execution(1 min). Once again I share my requirment in very simple manner.1) I want to create server script in which I will create database connection and then I will pass this object to other script so that it will use this object as per there requirment.2) I dont want to create seperate connection of database for each request. If my reuirment is logically possible then help me. Otherwise tell me where I am wrong so that I will never do this mistake again. Please help me to out of this... Thanks Antrikssh....

Edited by Antrikssh
Link to comment
Share on other sites

If you need to use PHP for this then I would recommend reading about persistent database connections in PHP: http://php.net/manual/en/features.persistent-connections.php That article describes what they are, what they are not, and why to use them. You can create a persistent connection with mysqli by prepending a "p:" before the host name. http://www.php.net/manual/en/mysqli.construct.php

Link to comment
Share on other sites

  • 1 month later...

Hello Everyone, Thanks to all for your precious time that you gave me. I solved my problem by using socket programming. Mr. Moderator thanks to gave me right direction otherwise I nevered got the required solution.

Edited by Antrikssh
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...