Hello everybody,
I've read the manual regarding sockets, followed the user contribution and read couple of tutorials inorder to get the very simple example working.I would be greatful to you if somebody can make this code working in the way I really want it to.
First attempt:
With this code, I'm able to create the connection and then i telnet to the ip-address and port no.Now, when i input a character,the connection is lost immediately:
Code:
<?php
// set some variables
$host = "127.0.0.1";
$port = 10000;
// 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);
?>
Second attempt:
NOw with this code, its much better.when i try to input, it accepts and it says what i said.Actually,both the codes were supposed to accept a string and when i press enter, it is supposed to close the socket connection.
It does'nt happen in that way.
Code:
<?php
/*
* We don't want any time-limit for how the long can hang
* around, waiting for connections:
*/
set_time_limit(0);
/* Create a new socket: */
if(($sock = socket_create(AF_INET, SOCK_STREAM, 0)) < 0 )
{
print strerror( $sock ) . "\n";
exit(1);
}
/* Bind the socket to an address and a port: */
if(($ret = socket_bind( $sock, "127.0.0.1", 10000 )) < 0 )
{
print strerror( $ret ) . "\n";
exit(1);
}
/*
* Listen for incoming connections on $sock.
* The '5' means that we allow 5 queued connections.
*/
if(($ret = socket_listen( $sock, 5)) < 0 )
{
print strerror( $ret ) . "\n";
}
/* Accept incoming connections: */
if(($msgsock = socket_accept( $sock )) < 0)
{
print strerror( $msgsock ) . "\n";
exit(1);
}
/* Send the welcome-message: */
$message = "Welcome to my TCP-server!\n";
if(($ret = socket_write( $msgsock, $message, strlen($message)) ) < 0
)
{
print strerror( $msgsock ) . "\n";
exit(1);
}
/* Read/Receive some data from the client: */
$buf = '';
if(($buf = socket_read( $msgsock, 128 )) < 0 )
{
print strerror( $ret ) . "\n";
exit(1);
}
/* Echo the received data back to the client: */
if(($ret = socket_write( $msgsock, "You said: $buf\0\n", strlen("You
said: $buf\0\n")) ) < 0 )
{
print strerror( $ret ) . "\n";
exit(1);
}
/* Close the communication-socket: */
socket_close($msgsock );
/* Close the global socket: */
socket_close($sock );
?>
I tested the above code(s) under windows-NT,XP and from the command prompt.
Thanks in advance for all your suggestions.