Page 1 of 1

Socket Issue with using a duplicated active socket.

Posted: Thu Jun 18, 2009 12:40 pm
by jdredd
I have a telnet based application that runs, and need to add support for PHP to run. So I already have the connection / Socket created and going, and socket duplicated before i createprocess run php.

I just run c:\php\php.exe Test.PHP 12345
12345 being the socket handle

Having issues finding out to just read/write from the socket. I don't need to create/bind/ect.. my main app does that already.
This works in my other child apps that are not PHP based.

Been looking around but only find examples for making a PHP that will create/bind/listen .. not just "inherit" the socket handle i want to pass it.

Re: Socket Issue..

Posted: Thu Jun 18, 2009 12:49 pm
by requinix
php.exe will inherit the socket but your PHP script will not.

PHP deals with sockets using resources. You need a resource - like the one returned by socket_create - to do anything. You still have to open the socket in your code.

Re: Socket Issue..

Posted: Thu Jun 18, 2009 1:50 pm
by jdredd
Thats what I figured, but I can't get how to create the resource and have it use the socket handle ID that passed.

Re: Socket Issue with using a duplicated active socket.

Posted: Thu Jun 18, 2009 4:47 pm
by Weirdan
You could dup2() the socket handle to STDIN/STDOUT before executing php binary. For those fds PHP automatically creates resources, accessible as STDIN/STDOUT constants from php userland code. I guess inetd does something like this.

Re: Socket Issue with using a duplicated active socket.

Posted: Fri Jun 19, 2009 1:01 am
by jdredd
Dup2 ?

I am using Delphi to create a console application that is telnet based. Being able to use PHP to be ran and display the output back to the end user would be great. Spawning other non php apps that have i have from it that use the passed socket handle work fine, but they are written in freepascal and have direct winsock access easily.

Still trying out some things but so far no luck, just missing 1 thing basically to bring it all together.

Steven

Re: Socket Issue with using a duplicated active socket.

Posted: Fri Jun 19, 2009 2:37 am
by Weirdan
jdredd wrote:Dup2 ?
Oh, dup2() is a unix api call. I didn't realize you were using windows. From what I heard, under windows you can use dio functions to access open file descriptors by their numbers, but then again, Winsock handles are not files. There's a example in MSDN that shows how to achieve on windows what dup2 does on unix platforms: http://support.microsoft.com/default.as ... -us;190351

Re: Socket Issue with using a duplicated active socket.

Posted: Sat Jun 20, 2009 11:19 pm
by jdredd
Ok... i was able to create in/out pipes in my Delphi application. So when someone telnets in.. they can use the telnet service
as usual then i have a command they can run, and it will execute PHP.EXE to run the .php file. It will pipe out to the telnet connection and they can input/output like it was nothing. I have the script doing out ANSI graphics to make it pretty.
Now my next thing is, is how to get ANSI graphics to work on the "Server" side. On the server side, you see the ansi chars and all.

Can PHP do any color changing modes in command line? If so, ill just port over my ansi parser to PHP and all my color output commands, ect. thats if PHP can do that.

Anyone know a way to get XP cmd.exe to do ansi? i can modify the config.NT file and set it to dosmode and ansi.sys , but it wont work with the php.exe as Dosmode wont run win32 apps.

Re: Socket Issue with using a duplicated active socket.

Posted: Sun Jun 21, 2009 10:05 am
by Weirdan
jdredd wrote:Can PHP do any color changing modes in command line?
You just output certain sequences of bytes. Actual coloring must be supported by a terminal application. The cmd.exe does not parse ansi escapes, so you will need another wrapper program, like ANSICON

Re: Socket Issue with using a duplicated active socket.

Posted: Sun Jun 21, 2009 7:20 pm
by jdredd
I got the localside ansi stuff taken care of.

Now that Im this far, is there a way to write new input routines?

fread / fget ... i can sepcify the length of the return.. but it doesn't limit how much they can type in initially.

I would love to have a routine that could like

getinput($input,20);

and would only allow them to type 20 chars max. but that would require to read their input char by char and decide when to
stop.