Reading binary data via a socket...

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Reading binary data via a socket...

emacstheviking
Hi,

In a nutshell: how do I read binary data from a socket with socket, get_byte et al ?

I have written a Redis client (against 2.8) which is fully functional and ready to release but I have been writing test cases for as many of the commands as I can without going mad on it and when I use the DUMP command it barfs. Here is the session:

| ?- [gpredis].
compiling /home/sean/tmp/gnu-prolog-redis-client/gpredis.pl for byte code...
/home/sean/tmp/gnu-prolog-redis-client/gpredis.pl compiled, 192 lines read - 16073 bytes written, 17 ms

(4 ms) yes
| ?- redis(set(gnu_prolog, 'Is awesome!')).
STATUS: OK

yes
| ?- redis(get(gnu_prolog)).
STRING: Is awesome!

true ? 

yes
| ?- redis(dump(gnu_prolog)).
uncaught exception: error(representation_error(character),get_code/2)
| ?- 

The problem is that I am opening the socket like this:

redis_connect(Conn) :-
redis_connect(Conn, localhost, 6379).

redis_connect(redis(SI, SO, S), Host, Port) :-
socket('AF_INET', S),
socket_connect(S, 'AF_INET'(Host, Port), SI, SO),
set_stream_type(S, binary).  %% added to see if I could fix it but no dice so far!

and then I use get_code/2 everywhere. That seemed like a good idea initially because redis deals with strings but they are binary safe and this obviously isn't, I can cope with that if I can figure out how to do it with set_stream_type/. open/3,open/4 allow the mode to be set but how do I coerce a socket into binary mode with GNU Prolog?

Somewhere in there I need to force it into binary mode but it being Friday and all I can't seem to figure it out...I am really keen to release this project to GitHub but I refuse until I am happy with it!

My client handles all redis commands by de-constructing the term passed into the redis() predicate, output to the console but the redis_do() predicate passes back the results. Once I have this issue solved it will be a matter of days to complete and release it so any assistance would be appreciated. I will probably crack it tonight myself but I am at work and thought I would look into it at lunchtime! This is my first "useful" bit of Prolog (well, useful to me so far!) and I am keen to complete it *and* understand it.

:)

Thanks,
Sean.


_______________________________________________
Users-prolog mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/users-prolog
Reply | Threaded
Open this post in threaded view
|

Re: Reading binary data via a socket...

Daniel Diaz-3
Le 22/11/2013 15:15, emacstheviking a écrit :
Hi,

In a nutshell: how do I read binary data from a socket with socket, get_byte et al ?

I have written a Redis client (against 2.8) which is fully functional and ready to release but I have been writing test cases for as many of the commands as I can without going mad on it and when I use the DUMP command it barfs. Here is the session:

| ?- [gpredis].
compiling /home/sean/tmp/gnu-prolog-redis-client/gpredis.pl for byte code...
/home/sean/tmp/gnu-prolog-redis-client/gpredis.pl compiled, 192 lines read - 16073 bytes written, 17 ms

(4 ms) yes
| ?- redis(set(gnu_prolog, 'Is awesome!')).
STATUS: OK

yes
| ?- redis(get(gnu_prolog)).
STRING: Is awesome!

true ? 

yes
| ?- redis(dump(gnu_prolog)).
uncaught exception: error(representation_error(character),get_code/2)
| ?- 

The problem is that I am opening the socket like this:

redis_connect(Conn) :-
redis_connect(Conn, localhost, 6379).

redis_connect(redis(SI, SO, S), Host, Port) :-
socket('AF_INET', S),
socket_connect(S, 'AF_INET'(Host, Port), SI, SO),
set_stream_type(S, binary).  %% added to see if I could fix it but no dice so far!

and then I use get_code/2 everywhere. That seemed like a good idea initially because redis deals with strings but they are binary safe and this obviously isn't, I can cope with that if I can figure out how to do it with set_stream_type/. open/3,open/4 allow the mode to be set but how do I coerce a socket into binary mode with GNU Prolog?
You have to set the stream(s) in binary mode (not the socket).

set_stream_type(SI, binary),
set_stream_type(SO, binary),

Then use get_byte(SI, X) or put_byte(SI, X).

Daniel


Somewhere in there I need to force it into binary mode but it being Friday and all I can't seem to figure it out...I am really keen to release this project to GitHub but I refuse until I am happy with it!

My client handles all redis commands by de-constructing the term passed into the redis() predicate, output to the console but the redis_do() predicate passes back the results. Once I have this issue solved it will be a matter of days to complete and release it so any assistance would be appreciated. I will probably crack it tonight myself but I am at work and thought I would look into it at lunchtime! This is my first "useful" bit of Prolog (well, useful to me so far!) and I am keen to complete it *and* understand it.

:)

Thanks,
Sean.


--
Ce message a été vérifié par MailScanner pour des virus ou des polluriels et rien de suspect n'a été trouvé.

_______________________________________________
Users-prolog mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/users-prolog


--
Ce message a été vérifié par MailScanner pour des virus ou des polluriels et rien de suspect n'a été trouvé.
_______________________________________________
Users-prolog mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/users-prolog
Reply | Threaded
Open this post in threaded view
|

Re: Reading binary data via a socket...

emacstheviking
>>
> You have to set the stream(s) in binary mode (not the socket).
>
> set_stream_type(SI, binary),
> set_stream_type(SO, binary),
>

Obvious really isn’t it… I have done sockets PHP and C, used setsockopt, but why oh why didn’t I have the brains to figure this out in Prolog for heavens sake?

:)

I should really send you a christmas card Danial to say thanks once again!

Sean


> Then use get_byte(SI, X) or put_byte(SI, X).
>
> Daniel


_______________________________________________
Users-prolog mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/users-prolog