Re: socket gives notice on server side(simple newbie code)
| From: | Negin Nickparsa | Date: | Wed, 20 Aug 2014 15:00:46 +0000 |
| Subject: | Re: socket gives notice on server side(simple newbie code) | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-323751@lists.php.net to get a copy of this message | ||
Sincerely
Negin Nickparsa
On Wed, Aug 20, 2014 at 7:55 AM, Christoph Becker <cmbecker69@gmx.de> wrote:
> Negin Nickparsa wrote:
>
> > I am struggling with my code and I cannot figure out what I have done
> which
> > it tells me Notice
> >
> > here is my server.php:
> >
> > <?php
> > set_time_limit(0);
> >
> > $stock=array("hi"=>"hi","name"=>"negin","question"=>"answer");
> > $address="127.0.0.1";
> > $port="127";
> > $sock= socket_create(AF_INET,SOCK_STREAM, getprotobyname("tcp"));
> > socket_bind($sock, $address,$port);
> > socket_listen($sock);
> > $client= socket_accept($sock);
> > $input=socket_read($client, 1024);
> > echo "you entered ".$input;
> > if($stock[$input]!=NULL)
> > {
> > $answer=$stock[$input];
> > socket_write($sock,$answer, $port);
> >
> > }
> > else{
> > echo socket_strerror(0);
> > }
> > socket_close($sock);
> > ?>
> >
> > and here is my client.php:
> >
> > <?php
> > $address="127.0.0.1";
> > $port="127";
> > $sock= socket_create(AF_INET,SOCK_STREAM, getprotobyname("tcp"));
> > echo "enter your data:";
> > $input= fgets(STDIN);
> > socket_connect($sock, $address,$port);
> > socket_write($sock,$input ,strlen($input));
> > $dt=socket_read($sock, 1024);
> > echo $dt;
> > ?>
> >
> > I really cannot undrestand why the output is this on server side:
> >
> > c:\wamp\www\phpzend>php -f server.php
> > you entered salam
> >
> > Notice: Undefined index: salam
> > in C:\wamp\www\phpzend\server.php on line 12
> >
> > Call Stack:
> > 0.0010 232328 1. {main}() C:\wamp\www\phpzend\server.php:0
> >
> > The operation completed successfully.
> >
> > but it doesn't write on my client side so this notice completely is a big
> > issue I var_dumped the $input and it is string I have no idea what's
> wrong
> > here
>
> Have you checked that var_dump($input) prints string(5) "salam" (note
> the string length 5).
>
> --
> Christoph M. Becker
>
>
> yeah I checked that one and it is string I even commented out my code and
tried to show the $stock[$input] and it had no problem
Jim here is the code:
here is my server.php:
<?php
set_time_limit(0);
$stock=array("salam"=>"salam","esmet
chie"=>"negin","chika mikoni"=>"socket
minevisam!");
$address="127.0.0.1";
$port="127";
$sock= socket_create(AF_INET,SOCK_STREAM, getprotobyname("tcp"));
socket_bind($sock, $address,$port);
socket_listen($sock);
$client= socket_accept($sock);
$input=socket_read($client, 1024);
echo "you entered ".$input;
if($stock[$input]!=NULL)
{
$answer=$stock[$input];
socket_write($sock,$answer, $port);
}
else{
echo socket_strerror(0);
}
socket_close($sock);
?>
and here is my client.php:
<?php
$address="127.0.0.1";
$port="127";
$sock= socket_create(AF_INET,SOCK_STREAM, getprotobyname("tcp"));
echo "enter your data:";
$input= fgets(STDIN);
socket_connect($sock, $address,$port);
socket_write($sock,$input ,strlen($input));
$dt=socket_read($sock, 1024);
echo $dt;
?>
the output on server the side:
c:\wamp\www\phpzend>php -f server.php
you entered salam
Notice: Undefined index: salam
in C:\wamp\www\phpzend\server.php on line 12
Call Stack:
0.0010 232328 1. {main}() C:\wamp\www\phpzend\server.php:0
The operation completed successfully.