473,405 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Creating Persistent Php Sockets

Hi,
I wrote a socket program to pass message form client and server.

This is a test case and to be inplemented in the later stage.

In my test case the connection between the server and the client is found only once. Now i need the server to be open always and wait for the requests.

Bcoz even if one client closes there would be other clients to communicate.

Server Side Script sample.php

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     echo "This is the Server";
  3.     set_time_limit(0);
  4.     $socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP); // Create the Socket
  5.     socket_bind($socket,'192.168.1.41',1234);
  6.     socket_listen($socket,5); // listen for any incoming connections
  7.     $connection = socket_accept($socket);
  8.     socket_write($connection,'Youve successfully connected to my computer!rn');
  9.     $serverOut = socket_read($connection,2048);
  10.     echo $serverOut;
  11.     $serverSaid = "There is a to and fro Communication startd here";
  12.     socket_write($connection,$serverSaid,strlen($serverSaid));
  13.     socket_close($connection);
  14.     socket_close($socket);
  15.  
  16. ?>
Client side Script sampleClient.php

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     $clientSocket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP); // Create the Socket
  3.     socket_connect($clientSocket,'192.168.1.41',1234);
  4.     $out = socket_read($clientSocket,2048);
  5.     echo $out;
  6.     socket_write($clientSocket,'This Works In Both Ways');
  7.     $serverOut = socket_read($clientSocket,2048);
  8.     echo "\n\n".$serverOut;
  9.     socket_close($clientSocket);
  10. ?>
[Please use CODE tags when posting source code. Thanks! --pbmods]

---Help needed Very Urgent.
i tried to work with loops but with fail... The result was not as expected.

Looking for ward for the reply
Jul 5 '07 #1
4 6953
dafodil
392 256MB
Hi,
I wrote a socket program to pass message form client and server.

This is a test case and to be inplemented in the later stage.

In my test case the connection between the server and the client is found only once. Now i need the server to be open always and wait for the requests.

Bcoz even if one client closes there would be other clients to communicate.

Server Side Script sample.php

<?php
echo "This is the Server";
set_time_limit(0);
$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP); // Create the Socket
socket_bind($socket,'192.168.1.41',1234);
socket_listen($socket,5); // listen for any incoming connections
$connection = socket_accept($socket);
socket_write($connection,'Youve successfully connected to my computer!rn');
$serverOut = socket_read($connection,2048);
echo $serverOut;
$serverSaid = "There is a to and fro Communication startd here";
socket_write($connection,$serverSaid,strlen($serve rSaid));
socket_close($connection);
socket_close($socket);

?>

Client side Script sampleClient.php

<?php
$clientSocket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP); // Create the Socket
socket_connect($clientSocket,'192.168.1.41',1234);
$out = socket_read($clientSocket,2048);
echo $out;
socket_write($clientSocket,'This Works In Both Ways');
$serverOut = socket_read($clientSocket,2048);
echo "\n\n".$serverOut;
socket_close($clientSocket);
?>


---Help needed Very Urgent.
i tried to work with loops but with fail... The result was not as expected.

Looking for ward for the reply
I tried to run your code and there are certain errors....

Please take a peek at this link there is a good sample with exceptions on it:
http://www.php.net/sockets
Jul 5 '07 #2
pbmods
5,821 Expert 4TB
Changed thread title to better describe the problem (did you know that threads whose titles contain fewer than three words actually get FEWER responses?).
Jul 5 '07 #3
Changed thread title to better describe the problem (did you know that threads whose titles contain fewer than three words actually get FEWER responses?).
Thanks for the change hope i would get more responses.

Can i know the errors pls.

One thing you need to do is change the host name or the Ip address to that of ur server or PC. Then ihope u wont get errors. AND IT WOULD RUN ONLY ONCE....

There are two ways to enable this
1. multi threading. ---- Does Php support this???
2. My server socket should not close the socket even when the client closes the connection.
Jul 5 '07 #4
kovik
1,044 Expert 1GB
PHP does not quite support multi-threading.

You seem to be new to this, and you may be interested in tackling smaller projects before taking on sockets. Just a suggestion.
Jul 5 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Randell D. | last post by:
Folks, I currently connect to my db with PHP code that uses non-persistent connections. I've read that persistent connections can help performance since a connection to the db will use an...
0
by: obhayes | last post by:
Hi All, Im using classic ASP (3.0) and I have a web farm with 2 webservers (webserver A and webserver B, both windows server 2003). I do not want to store any client specific information on the...
1
by: brad | last post by:
Hi, Im using classic ASP (3.0) and I have a web farm with 2 webservers (webserver A and webserver B, both windows server 2003). I do not want to store any client specific information on the...
4
by: M?rio Amado Alves | last post by:
Will you help an outsider trying to trace the current state of persistent object technology? "I expect that there will be persistent object stores with STL-conforming interfaces fitting into the...
0
by: Sagaert Johan | last post by:
this code using System; using System.Net.Sockets; namespace ConsoleApplication1 { /// <summary> /// Summary description for Class1. /// </summary>
6
by: David Rasmussen | last post by:
I use urllib2 to do some simple HTTP communication with a web server. In one "session", I do maybe 10-15 requests. It seems that urllib2 opens op a connection every time I do a request. Can I...
1
by: Eric Lindsay | last post by:
I am trying to understand the differences between and uses of persistent, default and alternate styles. I have read http://www.w3.org/TR/REC-html40/present/styles.html section 14.3.2 on...
3
by: sanchita | last post by:
Hello everyone, I didn't get any response in "Security" forum hence posting here again. I am having problem with persistent cookies. Even after setting "CreatePersistentCookie" to true in...
3
by: cjhegarty | last post by:
Hello, I have an application which runs on a single linux box, with a C++ backend server and a web-based front end, with apache also running on the local server. I'm using PHP to communicate...
2
by: =?Utf-8?B?U2Vhbk1hYw==?= | last post by:
I am familiar with how to use winsock in vb6 to create a network app. I'm trying to find a way to take the current vb6 app and create a windows service using system.net.sockets. How do you create...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.