Sagewire Logo

Problem reading/writing sockets

8 Message(s) by 3 Author(s) originally posted in php programming


From: Can2002 Date:   Wednesday, October 24, 2007
I have been trying to put together an app lication 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 have 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:

<?php
error_reporting(E_ALL);
echo "<h2>
TCP/IP Connection</h2>\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<br />";

$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 will happily
provide any additional information if needed!

Thanks in advance,
Chris


From: Jerry Stuckle Date:   Wednesday, October 24, 2007
wrote in message:
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 have 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:
<?php
error_reporting(E_ALL);
echo "<h2>
TCP/IP Connection</h2>\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<br />";
$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 will happily
provide any additional information if needed!
Thanks in advance,
Chris



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

You've the string in single quotes, so PHP sends the character s
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@xxxxxxxxxxx
==================


From: Can2002 Date:   Wednesday, October 24, 2007
wrote in message:

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...@xxxxxxxxxxx
==================



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 will do some more experimentation.

Chris


From: Can2002 Date:   Thursday, October 25, 2007
wrote in message:
but the PHP script seemd to hand



Oops, should've been hang , not hand!

Chris


From: Andrew DeFaria Date:   Thursday, October 25, 2007
--------------070105050708040502040109


wrote in message:
wrote in message:
but the PHP script seemd to hand
Oops, should've been hang, not hand!
Chris


But that "seemd" is just dandy!

My lord what are we teaching the kids now a days!
--
Andrew DeFaria <http://defaria.com>
I know you may think you know what I said, but I'm not sure that you
realize that what you think I said isn't really what I meant.--------------070105050708040502040109

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


From: Jerry Stuckle Date:   Thursday, October 25, 2007
wrote in message:
wrote in message:
You've 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...@xxxxxxxxxxx
==================
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 will 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 does not come, for instance.

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


From: Can2002 Date:   Thursday, October 25, 2007
wrote in message:
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 does not 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


From: Jerry Stuckle Date:   Thursday, October 25, 2007
wrote in message:
wrote in message:
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 does not 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 do not 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@xxxxxxxxxxx
==================



Next Message: Optimization of mySQL to XML output...



Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional