basic socket communications?

Sorry if this is overly noob of badly formed, but I’m a trader tryingto evaluate kdb+ for a HFT infrastructure, and my developer skills areweak.I can’t find an example anywhere of opening a client socket connectionand writing a string to it (some trivial login message maybe), orsomething similar to a while(true) do {} loop with a listener.Can someone paste some trivial code or give me a pointer to aresource? All I can find is mentions of IPC for Q processes.Thanks in advance.

To: personal-kdbplus@googlegroups.com
X-Mailer: Apple Mail (2.1077)

you would have to do it yourself by extending with C/C++ or through =
Java/C# etc
note that reading from sockets can be put on kdb+'s select loop (look =
for sd0 and s1 for example in =
https://code.kx.com/trac/wiki/Cookbook/InterfacingWithC)

Regards,
Attila
On 5 Jan 2010, at 01:18, bjh wrote:

> Sorry if this is overly noob of badly formed, but I’m a trader trying
> to evaluate kdb+ for a HFT infrastructure, and my developer skills are
> weak.
>
> I can’t find an example anywhere of opening a client socket connection
> and writing a string to it (some trivial login message maybe), or
> something similar to a while(true) do {} loop with a listener.
>
> Can someone paste some trivial code or give me a pointer to a
> resource? All I can find is mentions of IPC for Q processes.
>
> Thanks in advance.
>
> –
>
> You received this message because you are subscribed to the Google =
Groups “Kdb+ Personal Developers” group.
> To post to this group, send email to =
personal-kdbplus@googlegroups.com.
> To unsubscribe from this group, send email to =
personal-kdbplus+unsubscribe@googlegroups.com.
> For more options, visit this group at =
http://groups.google.com/group/personal-kdbplus?hl=en.
>
>

OK, perhaps this leads to a better question. Let’s say my use case isthat I want to do real-time metrics on the NYSE ARCABook feed. VWAPs,correlations, etc.Is the “proper” approach to actually write the market data gateway inC/C++/Java, and then interface kdb+ to that? I was hoping to actuallywrite the market data listener directly in Q.At this level of cost/capability, crossing process boundaries, evenusing Unix Domain Sockets, is really worth avoiding, if possible.Thanks!

Attila has given you the link for info on writing a client app that
sends data to a kdb+, but you would likely benefit from loading a
shared library into kdb+ that did the comms with the NYSE ARCABook
feed and called back into Q with the data. Attila did mention that you
can hook into the kdb+ main loop with sd1/sd0 which would then
callback to your code from the main thread when data has arrived on
that socket, allowing you to read the socket, create some k objects
and push them up for processing in Q. The info for interfacing and
extending with c can be found here

https://code.kx.com/trac/wiki/Cookbook/InterfacingWithC
https://code.kx.com/trac/wiki/Cookbook/ExtendingWithC

And Arthur’s notes

http://kx.com/q/c/c/a.c
http://kx.com/q/c/c/a.q
http://kx.com/q/c/c/readme.txt

And for reference how it is done with the Reuters SSL api

http://kx.com/q/c/feed/ssl.c
http://kx.com/q/tick/ssl.q

charset=US-ASCII;
format=flowed;
delsp=yes

attached is generic socket interface.
it compiles on unices (linux and osx) and it should not be hard to
port it to windows.
get k.h from http://kx.com/q/c/c/k.h. place the skt.so in appropriate
directory for q shared libs
(usualy ~/q/l32/ for 32bit linux).

felix

–Apple-Mail-3-932175528
Content-Disposition: attachment;
filename=skt.c
Content-Type: application/octet-stream;
x-unix-mode=0644;
name=“skt.c”

// linux 32bit: gcc -shared -m32 -fPIC -o skt.so skt.c
// linux 64bit: gcc -shared -m64 -fPIC -o skt.so skt.c
// mac: gcc -dynamiclib -o skt.so -undefined dynamic_lookup skt.c
#include
#include
#include
#include
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include “k.h”

extern I conn(I,I),addr(S);extern S host(I);

// local callback for incomming data. it forwards the call to .skt.recv
// q: .skt.recvcb[fd;msg]
// ZK sktrcv2(I d){r0(k(0,“.skt.recvcb”,ki(d),(K)0));R (K)0;}
ZK sktrcv2(I d){I n=0;K m;ioctl(d,FIONREAD,&n);if(!n){sd0(d);r0(k(0,“.skt.pc”,ki(d),(K)0));R (K)0;};
m=ktn(KC,n);recv(d,kG(m),n,0);r0(k(0,“.skt.recvcb”,ki(d),m,(K)0));R (K)0;}

// local callback for incomming connexions. it forwards the call to .skt.accept to validate the connexion
// q: .skt.po[svr_fd,a:addr,p:port,fd]. connexions can be closed from .skt.po with sktcls()
ZK sktaccpt(I d){struct sockaddr_in s;I fd,n=sizeof(s);bzero(&s,n);fd=accept(d,(struct sockaddr )&s,&n);r0(sd1(-fd,sktrcv2));
r0(k(0,“.skt.po”,ki(d),ki(ntohl(s.sin_addr.s_addr)),ki(ntohs(s.sin_port)),ki(fd),(K)0));R (K)0;}

// get socket flags
K sktgetflgs(K v){K r;r=ktn(KI,2);kI(r)[0]=MSG_PEEK;kI(r)[1]=MSG_WAITALL;R r;}

// create a socket server. p: port(int)
K sktsvr(K p){I d,on=1;struct sockaddr_in s;if(-KI!=p->t)R krr(“type”);d=socket(AF_INET,SOCK_STREAM,0);if(-1==d)R orr(“socket”);
if(-1==setsockopt(d,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)))R orr(“setsockopt”);bzero(&s,sizeof(s));
s.sin_family=AF_INET;s.sin_addr.s_addr=htonl(INADDR_ANY);s.sin_port=htons(p->i);
if(-1==bind(d,(struct sockaddr
)&s,sizeof(s)))R orr(“bind”);if(-1==listen(d,24))R orr(“listen”);R sd1(-d,sktaccpt);}

// receive data from a socket (called from q). d: fd(int), f: flags(int), n: bytes to read (int)
// returns a list with the message and the number of bytes read or raise an error
K sktrcv(K d,K n,K f){I r;K m;if(d->t!=-KI||f->t!=-KI||n->t!=-KI)R krr(“type”);m=ktn(KG,n->i);bzero(kG(m),n->i);
r=recv(d->i,kG(m),n->i,f->i);if(r>0)R knk(2,m,ki(r));r0(m);R -1==r?orr(“recv”):krr(“close”);}

// send a message/messages to a socket/sockets: d: fd/fds (int/vec int)
K sktsnd(K d,K m){I r;if(-KI!=d->t||!(KC==m->t||KG==m->t))R krr(“type”);
r=send(d->i,kG(m),m->n,0);R -1==r?orr(“send”):ki(r);}

// open a socket h: host (sym), p: port (int)
K sktopen(K h,K p){I a,c;if(-KS!=h->t||-KI!=p->t)R krr(“type”);
a=addr(*h->s==‘\0’?(S)“localhost”:h->s);if(-1==a)R orr(“address”);
c=conn(a,p->i);if(-1==c)R orr(“connect”);R sd1(-c,sktrcv2);}

// close a socket. d: fd(int)
K sktclose(K d){if(d->t!=-KI)R krr(“type”);sd0(d->i);R ki(d->i);}

// shutdown a socket
K sktshutdown(K d,K h){if(d->t!=-KI||h->t!=-KI)R krr(“type”);shutdown(d->i,h->i);R ki(d->i);}



–Apple-Mail-3-932175528
Content-Disposition: attachment;
filename=skt.q
Content-Type: application/octet-stream;
x-unix-mode=0644;
name=“skt.q”


\d .skt

/ get MSG_PEEK and MSG_WAITALL values
.skt.MSG_PEEK.skt.MSG_WAITALL set’(:skt 2:sktgetflgs,1);


/ interface
/ socket server: p: port (int)
svr::skt 2:(sktsvr;1)
/ open a socket connexion h: host (sym), p: port(int)
open::skt 2:(sktopen;2)
/ close a socket d: fd (int)
close::skt 2:(sktclose;1)
/ shutdown a socket d: fd(int), h: how(int 0:RD,1:WR,2:RDWR)
shutdown::skt 2:(sktshutdown;2)
/ receive
recv::skt 2:(sktrcv;3)
/ send d: fd (int), m: message (char$/byte$)
send::skt 2:(sktsnd;2)

/ callabcks
recvcb:{[d;m]0N!(.skt.recvcb;d;m)}<br>/ port open cb: sd:server fd (int), a:addr(int), p:port (int), fd: skt descriptor(int)<br>/ connexions can be closed from .skt.po with .skt.cls[d]<br>po:{[sd;a;p;d]0N!(.skt.po;sd;a;p;d);}
/ port close cb: d: fd(int)
pc:{[d]0N!(`.skt.pc;d);}


–Apple-Mail-3-932175528
Content-Type: text/plain;
charset=US-ASCII;
format=flowed;
delsp=yes

</errno.h></string.h></unistd.h>

Thanks very much guys. Sorry to ask questions over my head, but I’malready learning a lot.