Re: socket gives notice on server side(simple newbie code)
| From: | Christoph Becker | Date: | Wed, 20 Aug 2014 14:55:30 +0000 |
| Subject: | Re: socket gives notice on server side(simple newbie code) | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-323750@lists.php.net to get a copy of this message | ||
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