client socket is automatically sending input to socket serve

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

client socket is automatically sending input to socket serve

Post by s.dot »

I am playing around with php sockets again, and it seems my client server is automatically sending input to the socket server!

My Script:

Code: Select all

#!C:\php\php.exe -q
<?php

//we will create a socket, set options, bind, and listen
//accept client connection, accept input, parse it, write it to client
//close client, close master

$master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($master, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($master, 'localhost', 1234);
socket_listen($master, 5);

$client	= socket_accept($master);
$input	= socket_read($client, 1024);
$output	= strrev($input) . chr(0);

socket_write($client, $output);
unset($output);

socket_shutdown($client, 2);
socket_close($client);

socket_shutdown($master, 2);
socket_close($master);
The socket server is ouputting "ýûýûûûû" to the client, which means it must have received it from the client. I do recall the first time I ran this I typed "string" into the client, and the server output "gnirtsýûýûûûû" to the client. So it seems this "ýûýûûûû" is hanging around even after the sockets have been closed and recreated!

Where is "ýûýûûûû" coming from (must be cached or in buffer somewhere?) or why is my client sending it?

I am running the server as a shell script and using PuTTY as the telnet client.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
phu
Forum Commoner
Posts: 61
Joined: Tue Mar 30, 2010 6:18 pm

Re: client socket is automatically sending input to socket s

Post by phu »

Your code runs as expected (i.e. no odd strings being sent one way or the other) on my Gentoo machines (PHP 5.2.13), both listening on localhost/connecting from the same machine and listening on a network address/connecting from a remote machine.

Have you tried a different telnet client? It might be some kind of nonstandard connection string.

Also, to see if it's just an initial thing, you might try this (slightly modified version of your script to let the server respond until you send 'exit' as well as displaying what it's sent, so hopefully you can tell at least where that input is showing up):

Code: Select all

<?php

//we will create a socket, set options, bind, and listen
//accept client connection, accept input, parse it, write it to client
//close client, close master

$master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($master, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($master, 'localhost', 1234);
socket_listen($master, 5);

$client = socket_accept($master);

$input = '';

while ($input != 'exit')
{
    $input  = trim(socket_read($client, 1024));
    echo "Got input: '{$input}'\n";
    
    $output = strrev($input) . "\n";

    socket_write($client, $output);
    unset($output);
}

socket_shutdown($client, 2);
socket_close($client);

socket_shutdown($master, 2);
socket_close($master);
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: client socket is automatically sending input to socket s

Post by s.dot »

Clever!

Thanks for that. Now here's what I'm receiving.

Socket server:

Code: Select all

C:\Users\Scott>c:\php\php.exe -qf c:\apache2\htdocs\sockets\socket_server.php

Got input: ' √▼ √  √↑ √' ²☺ √♥ ²♥'
Got input: ''
Got input: 'hi'
Got input: ''
Got input: 'what's up'
Got input: ''
Got input: 'it's indenting'
Got input: ''
Got input: 'ok i should probably kill this now'
Got input: ''
Got input: 'exit'

C:\Users\Scott>
Socket client:

Code: Select all

ýûýûûûû

hi
ih

  what's up
pu s'tahw

         it's indenting
gnitnedni s'ti

              ok i should probably kill this now
won siht llik ylbaborp dluohs i ko

                                  exit
I am going to try with a different telnet client to see if that solves the problem.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: client socket is automatically sending input to socket s

Post by s.dot »

OK, trying with a different telnet client solves the problem. I will continue to use PuTTy since I'm familiar with it, but I'll just ignore the first input/output. :-D

Eventually, I will have flash as the client anyways.
Thank you!
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: client socket is automatically sending input to socket s

Post by s.dot »

Here's a question..

If I have the socket server up and running in an infinite while() loop (which is good), how can I kill it when I make a script change - short of restarting my pc (and without any clients connected)?

EDIT| On windows 7
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
phu
Forum Commoner
Posts: 61
Joined: Tue Mar 30, 2010 6:18 pm

Re: client socket is automatically sending input to socket s

Post by phu »

Welcome. :)

As far as killing the server without connecting... perhaps using the task manager? I think it lists processes (last time I spent any real time with Windows was XP).

The only problem with killing the process directly is that it can leave the socket you're using occupied -- at least, it can on Linux. It does depend on the socket library implementation, though; I've had that problem in some languages but not others, and the platform could certainly make a difference as well.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: client socket is automatically sending input to socket s

Post by s.dot »

Figured that out as well.

1) Open a command prompt and type TASKLIST
It will then list the tasks. The socket server will be running under php.exe as type CONSOLE. Note the PID of this task.

Then type TASKKILL /F /PID 1111 (where 1111 is the pid of the socket server)

I'm sure I'll have more questions regarding sockets but I'll try to figure them out on my own before posting. :) You seem to know quit a bit... very handy!
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply