Nelly
Nelly

Reputation: 309

reverse string in php server-client

I'm new in php and I'm trying to reverse a string given by a client in a socket connection but it prints 0 instead of the reverse string and I don't know where should I modify to work fine.I put here only the server code before here I do all the things:

 >?php
 $sfd = socket_create(AF_INET,SOCK_STREAM,0);
 socket_bind($sfd,"localhost",8888);
 socket_listen($sfd,0);
 socket_recv($cfd,$buffer,15,0);
 (string) $sum="";
 for ($i=strlen($buffer)-1; $i>0; $i--){
        (string)$sum=(string)($sum + $buffer[$i]);
 }
 echo "received ".$buffer;
 echo "\n".(string)$sum;
 socket_send($cfd,$sum,9,0)
?>

Any ideas?Thank you.

Upvotes: 0

Views: 139

Answers (1)

Jitendra Kumar. Balla
Jitendra Kumar. Balla

Reputation: 1213

<?php
 $sfd = socket_create(AF_INET,SOCK_STREAM,0);
 socket_bind($sfd,"localhost",8888);
 socket_listen($sfd,0);
 socket_recv($socket, $buffer, 2048, MSG_WAITALL)
 echo "received ".$buffer;
 $revStr=strrev($buffer);
?>

Use socket_recv($socket, $buf, 2048, MSG_WAITALL) method to read all the content from the socket, and then use strrev($buffer) to reverse the given string.

Upvotes: 1

Related Questions