Discussion:
socket gives notice on server side(simple newbie code)
Negin Nickparsa
2014-08-20 02:48:39 UTC
Permalink
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
Negin Nickparsa
2014-08-20 02:50:10 UTC
Permalink
Sincerely
Negin Nickparsa
Post by Negin Nickparsa
I am struggling with my code and I cannot figure out what I have done
which it tells me Notice
<?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);
?>
<?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;
?>
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
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
​sorry I changed the array content which was persian it was like this
before:

$stock=array("salam"=>"salam","esmet chie"=>"negin","chika mikoni"=>"socket
minevisam!");​
Jim Giner
2014-08-20 14:07:47 UTC
Permalink
Could we see the "actual" code now with that change?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Christoph Becker
2014-08-20 14:55:30 UTC
Permalink
Post by Negin Nickparsa
I am struggling with my code and I cannot figure out what I have done which
it tells me Notice
<?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);
?>
<?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;
?>
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
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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Negin Nickparsa
2014-08-20 15:00:46 UTC
Permalink
Sincerely
Negin Nickparsa
Post by Christoph Becker
Post by Negin Nickparsa
I am struggling with my code and I cannot figure out what I have done
which
Post by Negin Nickparsa
it tells me Notice
<?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);
?>
<?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;
?>
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
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
Post by Negin Nickparsa
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.​
Jim Giner
2014-08-20 15:09:52 UTC
Permalink
Post by Negin Nickparsa
echo "you entered ".$input;
if($stock[$input]!=NULL)
{
$answer=$stock[$input];
socket_write($sock,$answer, $port);
}
else{
echo socket_strerror(0);
}
How about adding the following before the above lines:
if (!isset($stock[$input]))
print_r($stock);
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Robert Williams
2014-08-20 16:05:37 UTC
Permalink
I suspect there's a line-feed character in the input value. Try changing this:

if($stock[$input]!=NULL)

to this:

if($stock[trim($input)]!=NULL)

--
Robert E. Williams, Jr.
Senior Vice President of Software Development
Newtek Businesss Services, Inc. -- The Small Business Authority
https://www.newtekreferrals.com/rewjr
http://www.thesba.com/
Post by Jim Giner
Post by Negin Nickparsa
echo "you entered ".$input;
if($stock[$input]!=NULL)
{
$answer=$stock[$input];
socket_write($sock,$answer, $port);
}
else{
echo socket_strerror(0);
}
if (!isset($stock[$input]))
print_r($stock);
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Notice: This communication, including attachments, may contain information that is confidential. It constitutes non-public information intended to be conveyed only to the designated recipient(s). If the reader or recipient of this communication is not the intended recipient, an employee or agent of the intended recipient who is responsible for delivering it to the intended recipient, or if you believe that you have received this communication in error, please notify the sender immediately by return e-mail and promptly delete this e-mail, including attachments without reading or saving them in any manner. The unauthorized use, dissemination, distribution, or reproduction of this e-mail, including attachments, is prohibited and may be unlawful. If you have received this email in error, please notify us immediately by e-mail or telephone and delete the e-mail and the attachments (if any).
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Negin Nickparsa
2014-08-20 19:12:33 UTC
Permalink
Sincerely
Negin Nickparsa
Post by Negin Nickparsa
if($stock[$input]!=NULL)
if($stock[trim($input)]!=NULL)
--
Robert E. Williams, Jr.
Senior Vice President of Software Development
Newtek Businesss Services, Inc. -- The Small Business Authority
https://www.newtekreferrals.com/rewjr
http://www.thesba.com/
​Robert actually you were right! var_dump had the string(7) "salam" so it
is not string(4) and that is the problem the problem was somewhere else
which I didn't get it at first
thank you guys! great help!

I learned that I should trim my string even when it is string with STDIN it
would include the quotes in the string too​
Post by Negin Nickparsa
Post by Jim Giner
Post by Negin Nickparsa
echo "you entered ".$input;
if($stock[$input]!=NULL)
{
$answer=$stock[$input];
socket_write($sock,$answer, $port);
}
else{
echo socket_strerror(0);
}
if (!isset($stock[$input]))
print_r($stock);
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Notice: This communication, including attachments, may contain information
that is confidential. It constitutes non-public information intended to be
conveyed only to the designated recipient(s). If the reader or recipient of
this communication is not the intended recipient, an employee or agent of
the intended recipient who is responsible for delivering it to the intended
recipient, or if you believe that you have received this communication in
error, please notify the sender immediately by return e-mail and promptly
delete this e-mail, including attachments without reading or saving them in
any manner. The unauthorized use, dissemination, distribution, or
reproduction of this e-mail, including attachments, is prohibited and may
be unlawful. If you have received this email in error, please notify us
immediately by e-mail or telephone and delete the e-mail and the
attachments (if any).
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Negin Nickparsa
2014-08-20 19:02:11 UTC
Permalink
Sincerely
Negin Nickparsa
Post by Jim Giner
Post by Negin Nickparsa
echo "you entered ".$input;
if($stock[$input]!=NULL)
{
$answer=$stock[$input];
socket_write($sock,$answer, $port);
}
else{
echo socket_strerror(0);
}
if (!isset($stock[$input]))
print_r($stock);
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
​

c:\wamp\www\phpzend>php -f server.php
you entered salam
Array
(
[salam] => salam
[esmet chie] => negin
[chika mikoni] => socket minevisam!
)

Notice: Undefined index: salam
in C:\wamp\www\phpzend\server.php on line 14

Call Stack:
0.0010 232832 1. {main}() C:\wamp\www\phpzend\server.php:0

The operation completed successfully.

Also Robert I did trim it as you said and the result didn't change

so what's the problem why it is not set! there?!
Loading...