Page 1 of 1

Need help understanding beginning of socket programming code

Posted: Thu Jun 15, 2006 10:41 pm
by kabuki1985
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


A code taken from http://www.devshed.com/c/a/PHP/Socket-P ... ith-PHP/1/

Code: Select all

<?php
// set some variables
  $host = "127.0.0.1";
  $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");
// 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) 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);
?>


Why is it when i type a letter the server closes and i get connection to host lost ?
telnet 127.0.0.1 1234
s <--- character i type and below is the reply


Connection to host lost.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu Jun 15, 2006 10:44 pm
by kabuki1985
sorry i was just reading it and wanted to modify it.

Posted: Fri Jun 16, 2006 1:08 am
by kabuki1985
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I modified the code to loop on input until it gets a "kill" string. Problem is, why i can only input one character each time ?

Code: Select all

do{
// read client input
  $input = socket_read($spawn, 1024) or die("Could not read input\n");
  echo $input;
// clean up input string
  $input = trim($input);
  echo $input;
// reverse client input and send back
  $output = strrev($input) . "\n";
  echo $output;
  socket_write($spawn, $output, strlen ($output)) or die("Could not write
  output\n");
   }while ($input != "kill");

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Jun 16, 2006 2:48 am
by kabuki1985
I think that wasn't a good code to start with. I've found another one which i understand better.
Thanks