reading async return message with kdb c interface on windows

Hi, 

I’m having trouble reading async return message with kdb c interface. http://code.kx.com/wiki/Cookbook/InterfacingWithC. shows the following code 

#include"k.h"#include<stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<sys/types.h>#include<sys/socket.h>#include<netinet/in.h>#include<arpa/inet.h>int main(){ int retval; K x,r; int fd=khp("localhost",9999); // In a production system, check the return value fd_set fds; struct timeval tv; while(1){ tv.tv_sec=5; tv.tv_usec=0; FD_ZERO(&fds); FD_SET(fd,&fds); retval=select(fd+1,&fds,NULL,NULL,&tv); if(retval==-1) perror("select()"),exit(1); else if(retval){ printf("Data is available now.\n"); if(FD_ISSET(fd,&fds)){ x=k(fd,(S)0); printf("%d\n",x->i); } } else printf("No data within five seconds.\n"); } kclose(fd); return 0;}

I changed to the following for windows and VC2013. but I’m not reciving anything. seems like socket problem? I’ve no idea here.

Thanks

JQ

 

#include <winsock.h>

#include"k.h"

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<sys/types.h>

int main(){

int retval;

K x, r;

int fd = khp(“localhost”, 5001); // In a production system, check the return value

printf(“%d\n”, fd);

k(-fd, “a:2+1”, (K)0);

        k(fd, “a”, (K)0)

fd_set fds;

struct timeval tv;

while (1){

tv.tv_sec = 5;

tv.tv_usec = 0;

FD_ZERO(&fds);

FD_SET(fd, &fds);

retval = select(fd + 1, &fds, NULL, NULL, &tv);

if (retval == -1)

perror(“select()”), exit(1);

else if (retval){

printf(“Data is available now.\n”);

if (FD_ISSET(fd, &fds)){

x = k(fd, (S)0);

printf(“%d\n”, x->i);

}

}

else

printf(“No data within five seconds.\n”);

k(fd, “a”, (K)0);

}

kclose(fd);

return 0;

}

Is the server you are connecting to actually sending out async messages?  

e.g. something like a tickerplant publishing data? 

If you run this in a vanilla kdb+ process

q)system"p 9999";.z.ts:{neg[h]rand[100i];};.z.po:{h::x;system"t 1000";};.z.pc:{system"t 0"}

and then run your c program, you should see a random number being published each second.

Hi Charles,

I tried your method and it works fine. I’m getting random number from c++ client. Thanks a lot.

But I’m wondering what is the problem? Maybe my kdb server didn’t send out async reply? How do I receive async message and subscribe to tickerplant? Do I need to setup something on the server side as well?

Thanks

JQ

To be more specific

  1. how do I send an async message say “2+2” to get an async result of “4”?

  2. how do I do proper subscription to a tickerplant table? 

So I think Jonny’s questions are still pertinent.

#1

k(-fd, “neg[.z.w] 42i”, (K)0); // send async msg to server, and server will then send an async mg of 42i to this socket

        x=k(fd,(S)0); // read incoming message of 42i

#2
Have you set up a ticker plant - see http://code.kx.com/wiki/Startingkdbplus/tick

The c example code does nothing except connect and wait for incoming messages. To have this subscribe to the tickerplant, you’ll have to explicitly do something like

x=k(fd,“.u.sub”,ks(“tablename”),ks(“”),(K)0);

before you enter that while loop.

Thanks so much, both Charles and Jonny.

It works fine now. I think my problem was I didn’t know I had to ask the sever to send a async msg back to my socket. :(