Socket connection lost

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
minds_gifts
Forum Commoner
Posts: 63
Joined: Mon Feb 10, 2003 4:23 am

Socket connection lost

Post by minds_gifts »

Hello everybody,
I read the Socket Programming tutorial and tried to execute the simple server example.
When i telnet to the ip-address and port, it connects, and when i input a string, it losts the connection.could somebody please tell me why is this happening. I'm executing this script from the command prompt.
Thanks in advance.
Heres the script

Code: Select all

<?php

// set some variables
$host = "ip-address of the machine";
$port = 1234;

// don't timeout!
set_time_limit(0);

// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");

// bind socket to port
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");

// start listening for connections
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
echo "waiting for connections....\n";
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
echo "Received connection request\n";

// read client input
$input = socket_read($spawn, 1024) or die("Could not read input\n");

// clean up input string
$input = trim($input);

// reverse client input and send back
$output = strrev($input) . "\n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");

// close sockets
socket_close($spawn);
socket_close($socket);
?>
User avatar
devork
Forum Contributor
Posts: 213
Joined: Fri Aug 08, 2003 6:44 am
Location: p(h) developer's network

Post by devork »

there is an example about sockets in php manual (socket functions)
check that out
http://www.php.net/manual/en/ref.sockets.php
minds_gifts
Forum Commoner
Posts: 63
Joined: Mon Feb 10, 2003 4:23 am

Post by minds_gifts »

HI there,

I tested that example too.It has also the same problem.As per the explanation, it is supposed to lost the connection when i type quit and enter.It does'nt work in that way.

Thanks anyways.
Post Reply