Page 1 of 1

PHP socket help needed urgent

Posted: Mon Jun 22, 2009 3:50 am
by aiplvt
Dear all,
I have been using php script for developing my web server. Basically this server listens for client socket and accept incoming messages and return back to the client. The below is the code . The problem is , when the script executes socket_accept() it blocks the code and hence I am unable to print the echo values before that. The page remains blank. How do i overcome it.. I want the values to be printed in the page and later on the socket_accept can go for blocking how do i do that. what is the problem in this code pls reply

Code: Select all

 
<?
// set some variables
$host = "xx.xx.xx.xx";
echo "<br />"; echo "Host :";
echo $host; echo "   @   ";
 
// don't timeout!
set_time_limit(0);
 
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Could not create
socket\n");
 
$i = 1;
while($i == 1)
{
// bind socket to port
$result = socket_bind($socket, $host, 0);
$errorcode = socket_last_error();
   if ($errorcode != 0)
   {
     $i = 1;
   }
   else
   {
     $i = 0;
   }
}
socket_getsockname($socket, $host, $socket_port);
echo $socket_port; 
 
// start listening for connections
$result = socket_listen($socket, SOMAXCONN) or die("Could not set up socket
listener\n");
echo "<br />";echo "Listening Socket Status:".$result;
 
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming
connection\n");
 
// read client input
$input = socket_read($spawn,1024,PHP_NORMAL_READ) or die("Could not read input\n");
 
// clean up input string
$input = trim($input);
echo "<br />";echo "Recieved Input : ".$input;
 
// reverse client input and send back
$output = strrev($input) . "\n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write
output\n");
 
echo "<br />";echo "Output From the Server : ".$output;
// close sockets
 
socket_close($spawn);
socket_close($socket);
?>
 

Re: PHP socket help needed urgent

Posted: Mon Jun 22, 2009 5:30 am
by MeLight
I don't understand the problem, why not just put the text above the socket_listen() function call?

Re: PHP socket help needed urgent

Posted: Mon Jun 22, 2009 5:36 am
by aiplvt
to make it simple i want the socket_accept to act as a non blocking call... and allow the echo values above that to be printed.

Re: PHP socket help needed urgent

Posted: Mon Jun 22, 2009 6:21 am
by MeLight
not sure that it will lead where you want to go, but it will definitely do what you are asking:
bool socket_set_nonblock ( resource $socket )

Re: PHP socket help needed urgent

Posted: Mon Jun 22, 2009 8:26 am
by aiplvt
going into basics creating server..

1.socket_create()
2.socket_bind()
3.socket_listen()
4.socket_accept()......

Now i dont find any issues executing the first 3 scripts but when the execution reaches script 4 it hangs... what are the reasons due to which the socket_server can hang? BTW i was able to run the same script few days back but the same is not running now. Is there anything that i need to enable in my server or how do i debug it? There are no compile errors also when i try to run the client to connect the server using telnet i get
Connecting To xx.xx.xx.xx...Could not open connection to the host, on port xxxx: Connect failed.

How do i go about and please find my initial post for the code.

Re: PHP socket help needed urgent

Posted: Mon Jun 22, 2009 8:53 am
by MeLight
the socket_accept() isn't hanging, it's blocking ie waiting for an incoming connection. In order to override this behavior you should use the function from my previous post.
About you connecting with telnet, what command are you using??

Re: PHP socket help needed urgent

Posted: Mon Jun 22, 2009 11:29 pm
by aiplvt
telnet> open xx.xx.xx.xx.xx xxxx

PHP client - server socket help!!!

Posted: Tue Jun 23, 2009 12:51 am
by aiplvt
Dear all,
This is yet another parallel post from me regd php sockets . This is my requirement

1. I need to write a web server application using PHP and this could be on one of the free web hostings servers that provide php functionalities.

2. I have written the PHP server code on the free web server but the problem is the port, as I dont get a dedicated port for server to listen. So i used a polling method to get the open port and display it on the webpage so that client can use it to make connection.

3. After knowing in which port the server is listening i can use telent to connect to the web server with the port. u:\> telnet host port

The problem

1. In order to display the available port on the webserver i used "print $port;" and after that socket_accept() is called. Since the socket_accept() is a blocking call the "print $port" is not displayed on the screen. Due to which i could not know on which port the server is listening and the client could not make a connection to the server.

What are the ways available for the client to know on which port the server is listening keeping in mind the server takes a random free port and there is no dedicated port available?

And since this is free web hosting server there might be several other programs running in the same host listening to different ports.

I believe I am doing something wrong fundamentally which i am not getting to know because the code which i am using has no errors and well tested.

I hope someone in this forum will help me out...