# reading async return message with kdb c interface on windows

**URL:** <https://forum.kx.com/t/reading-async-return-message-with-kdb-c-interface-on-windows/10598>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [November 19, 2015, 8:30am UTC](https://forum.kx.com/t/reading-async-return-message-with-kdb-c-interface-on-windows/10598 "2015-11-19T08:30:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![JQ1](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@JQ1](https://forum.kx.com/u/JQ1)\
**Post date:** [November 19, 2015, 8:30am UTC](https://forum.kx.com/t/reading-async-return-message-with-kdb-c-interface-on-windows/10598/1 "2015-11-19T08:30:00Z")

</div>

Hi,&nbsp;

I’m having trouble reading async return message with kdb c interface.&nbsp;[http://code.kx.com/wiki/Cookbook/InterfacingWithC](http://code.kx.com/wiki/Cookbook/InterfacingWithC). shows the following code&nbsp;

```
#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

&nbsp;

#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);

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

fd\_set fds;

struct timeval tv;

while (1){

[tv.tv](http://tv.tv)\_sec = 5;

[tv.tv](http://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;

}

---

<div class="post-metadata">

**Author:** ![pressjonny0](https://avatars.discourse-cdn.com/v4/letter/p/ccd318/32.png) [@pressjonny0](https://forum.kx.com/u/pressjonny0)\
**Post date:** [November 19, 2015, 9:15am UTC](https://forum.kx.com/t/reading-async-return-message-with-kdb-c-interface-on-windows/10598/2 "2015-11-19T09:15:00Z")

</div>

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

e.g. something like a tickerplant publishing data?&nbsp;

>

---

<div class="post-metadata">

**Author:** ![charlie1](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@charlie1](https://forum.kx.com/u/charlie1)\
**Post date:** [November 19, 2015, 10:18am UTC](https://forum.kx.com/t/reading-async-return-message-with-kdb-c-interface-on-windows/10598/3 "2015-11-19T10:18:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JQ1](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@JQ1](https://forum.kx.com/u/JQ1)\
**Post date:** [November 20, 2015, 1:48am UTC](https://forum.kx.com/t/reading-async-return-message-with-kdb-c-interface-on-windows/10598/4 "2015-11-20T01:48:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JQ1](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@JQ1](https://forum.kx.com/u/JQ1)\
**Post date:** [November 20, 2015, 2:41am UTC](https://forum.kx.com/t/reading-async-return-message-with-kdb-c-interface-on-windows/10598/5 "2015-11-20T02:41:00Z")

</div>

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?&nbsp;

---

<div class="post-metadata">

**Author:** ![charlie1](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@charlie1](https://forum.kx.com/u/charlie1)\
**Post date:** [November 20, 2015, 6:30am UTC](https://forum.kx.com/t/reading-async-return-message-with-kdb-c-interface-on-windows/10598/6 "2015-11-20T06:30:00Z")

</div>

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

&nbsp; &nbsp; &nbsp; &nbsp; 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.

---

<div class="post-metadata">

**Author:** ![JQ1](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@JQ1](https://forum.kx.com/u/JQ1)\
**Post date:** [November 21, 2015, 2:58pm UTC](https://forum.kx.com/t/reading-async-return-message-with-kdb-c-interface-on-windows/10598/7 "2015-11-21T14:58:00Z")

</div>

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. :(
