Problem reading/writing sockets

Problem reading/writing sockets

am 24.10.2007 18:56:53 von can2002

I've been trying to put together an application to change channel on a
media streaming server. The server is able to issue IR commands to
its attached equipment using LIRC, with commands being issued by a
socket connection to TCP/8765.

I put together a basic Python app that issued a command, which worked
fine. I wanted to create a fairly simple web interface and so set
about producing a similar simple PHP application. I've had limited
success so far, with the media server rejecting the connections.
Unfortunately I have limited access to the OS of the media server - I
just know that it recognises the commands from the Python app and
rejects the PHP one. I need to submit the following string:

SEND_ONCE pace_stb_5 rm_power

My PHP app (with all error checking removed) is as follows:

error_reporting(E_ALL);
echo "

TCP/IP Connection

\n";
$service_port = '8765';
$address = '192.168.1.100';
$socket = socket_create(AF_INET, SOCK_STREAM, 0);
$result = socket_connect($socket, $address, $service_port);
$out = '';

socket_send($socket, 'SEND_ONCE pace_stb_5 rm_power\n',
strlen('SEND_ONCE pace_stb_5 rm_power\n'));
echo $result . "OK
";

$out = socket_read($socket, 1024);
echo $out;

socket_close($socket);
?>

In case it's of use, my Python application is:

#!/usr/bin/python
import socket,string

def send_ir(host, port, command):

exterity=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
exterity.connect((host, port))
exterity.send(command+"\n")

while 1:
response = exterity.recv(1024)
if response.split('\n')[-2] == "END":
return response.split('\n')[1]
break
exterity-socket.close()

HOST = '192.168.1.100'
PORT = 8765

status = send_ir(HOST, PORT, 'SEND_ONCE pace_stb_5 rm_power')
print status

On the face of it it seems as though both apps are sending the same
string; however I'm wondering if they're submitting the strings in a
different way. Apologies if any of the above is vague; I'll happily
provide any additional information if needed!

Thanks in advance,
Chris

Re: Problem reading/writing sockets

am 24.10.2007 19:59:30 von Jerry Stuckle

Can2002 wrote:
> I've been trying to put together an application to change channel on a
> media streaming server. The server is able to issue IR commands to
> its attached equipment using LIRC, with commands being issued by a
> socket connection to TCP/8765.
>
> I put together a basic Python app that issued a command, which worked
> fine. I wanted to create a fairly simple web interface and so set
> about producing a similar simple PHP application. I've had limited
> success so far, with the media server rejecting the connections.
> Unfortunately I have limited access to the OS of the media server - I
> just know that it recognises the commands from the Python app and
> rejects the PHP one. I need to submit the following string:
>
> SEND_ONCE pace_stb_5 rm_power
>
> My PHP app (with all error checking removed) is as follows:
>
> > error_reporting(E_ALL);
> echo "

TCP/IP Connection

\n";
> $service_port = '8765';
> $address = '192.168.1.100';
> $socket = socket_create(AF_INET, SOCK_STREAM, 0);
> $result = socket_connect($socket, $address, $service_port);
> $out = '';
>
> socket_send($socket, 'SEND_ONCE pace_stb_5 rm_power\n',
> strlen('SEND_ONCE pace_stb_5 rm_power\n'));
> echo $result . "OK
";
>
> $out = socket_read($socket, 1024);
> echo $out;
>
> socket_close($socket);
> ?>
>
> In case it's of use, my Python application is:
>
> #!/usr/bin/python
> import socket,string
>
> def send_ir(host, port, command):
>
> exterity=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
> exterity.connect((host, port))
> exterity.send(command+"\n")
>
> while 1:
> response = exterity.recv(1024)
> if response.split('\n')[-2] == "END":
> return response.split('\n')[1]
> break
> exterity-socket.close()
>
> HOST = '192.168.1.100'
> PORT = 8765
>
> status = send_ir(HOST, PORT, 'SEND_ONCE pace_stb_5 rm_power')
> print status
>
> On the face of it it seems as though both apps are sending the same
> string; however I'm wondering if they're submitting the strings in a
> different way. Apologies if any of the above is vague; I'll happily
> provide any additional information if needed!
>
> Thanks in advance,
> Chris
>
>

socket_send($socket, 'SEND_ONCE pace_stb_5 rm_power\n',

You have the string in single quotes, so PHP sends the characters
backslash and en, instead of the newline character you want.

Try pulling the string in double quotes (in both places).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: Problem reading/writing sockets

am 25.10.2007 00:59:39 von can2002

On 24 Oct, 18:59, Jerry Stuckle wrote:

> You have the string in single quotes, so PHP sends the characters
> backslash and en, instead of the newline character you want.
>
> Try pulling the string in double quotes (in both places).
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================

Hi Jerry,

Thanks for coming back to me. I had originally used double quotes,
but the PHP script seemd to hand (firefox just reported waiting for
server for many minutes). I was obviously have a bad 5 mins, as when
I re-read the section on single vs. double quotes I could work out
why...

I'll do some more experimentation.

Chris

Re: Problem reading/writing sockets

am 25.10.2007 09:23:30 von can2002

On 24 Oct, 23:59, Can2002 wrote:
> but the PHP script seemd to hand

Oops, should have been hang, not hand!

Chris

Re: Problem reading/writing sockets

am 25.10.2007 10:04:38 von Andrew DeFaria

This is a multi-part message in MIME format.
--------------070105050708040502040109
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Can2002 wrote:
> On 24 Oct, 23:59, Can2002 wrote:
>> but the PHP script seemd to hand
> Oops, should have been hang, not hand!
>
> Chris
But that "seemd" is just dandy!

My lord what are we teaching the kids now a days!
--
Andrew DeFaria
I know you may think you know what I said, but I'm not sure that you
realize that what you think I said is not really what I meant.


--------------070105050708040502040109
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit








Can2002 wrote:

On 24 Oct, 23:59, Can2002

wrote:

but the PHP script seemd to hand


Oops, should have been hang, not hand!



Chris


But that "seemd" is just dandy!



My lord what are we teaching the kids now a days!

--



I know you may think you know what I said,
but I'm not sure that you realize that what you think I said is not
really what I meant.







--------------070105050708040502040109--

Re: Problem reading/writing sockets

am 25.10.2007 14:56:49 von Jerry Stuckle

Can2002 wrote:
> On 24 Oct, 18:59, Jerry Stuckle wrote:
>
>> You have the string in single quotes, so PHP sends the characters
>> backslash and en, instead of the newline character you want.
>>
>> Try pulling the string in double quotes (in both places).
>>
>> --
>> ==================
>> Remove the "x" from my email address
>> Jerry Stuckle
>> JDS Computer Training Corp.
>> jstuck...@attglobal.net
>> ==================
>
> Hi Jerry,
>
> Thanks for coming back to me. I had originally used double quotes,
> but the PHP script seemd to hand (firefox just reported waiting for
> server for many minutes). I was obviously have a bad 5 mins, as when
> I re-read the section on single vs. double quotes I could work out
> why...
>
> I'll do some more experimentation.
>
> Chris
>
>

Well, it sounds like you need to look at what's actually being
communicated. It could seem to hang of it's waiting for a response from
the remote device which doesn't come, for instance.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: Problem reading/writing sockets

am 25.10.2007 21:24:41 von can2002

On 25 Oct, 13:56, Jerry Stuckle wrote:
>
> Well, it sounds like you need to look at what's actually being
> communicated. It could seem to hang of it's waiting for a response from
> the remote device which doesn't come, for instance.

Thanks again Jerry,

I did wonder that, which was what led me to try the single quotes. I
know the box responds after a newline, both from seeing the effect of
removing it from my python app or connecting via Telnet. Am I right
in thinking that echoing "\n" will result in a newline character being
sent?

Sorry if it sounds like a stupid question...

Chris

Re: Problem reading/writing sockets

am 26.10.2007 05:43:09 von Jerry Stuckle

Can2002 wrote:
> On 25 Oct, 13:56, Jerry Stuckle wrote:
>> Well, it sounds like you need to look at what's actually being
>> communicated. It could seem to hang of it's waiting for a response from
>> the remote device which doesn't come, for instance.
>
> Thanks again Jerry,
>
> I did wonder that, which was what led me to try the single quotes. I
> know the box responds after a newline, both from seeing the effect of
> removing it from my python app or connecting via Telnet. Am I right
> in thinking that echoing "\n" will result in a newline character being
> sent?
>
> Sorry if it sounds like a stupid question...
>
> Chris
>
>

Chris,

Echoing a "\n" won't send it over the socket, but I don't think that's
what you mean.

Sending a "\n" over the socket should send a newline character. But is
your device expecting a new line (Unix style) or a cr/lf (Windows
style)? If the latter, try "\r\n".

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: Problem reading/writing sockets

am 07.11.2007 16:28:03 von can2002

On Oct 26, 3:43 am, Jerry Stuckle wrote:
> Echoing a "\n" won't send it over the socket, but I don't think that's
> what you mean.
>
> Sending a "\n" over the socket should send a newline character. But is
> your device expecting a new line (Unix style) or a cr/lf (Windows
> style)? If the latter, try "\r\n".

Hi Jerry,

Thanks for that and sorry for the delay in response. I'm not
completely sure, but the device is running an embedded version of
Linux with Busybox and LIRC, so I'd be surprised if it needed Windows-
style returns. I simply append "/n" in Python...

I've been distracted with other work in the last week, but I'll keep
looking.

Cheers,
Chris