How to get data from YARP's name server without using any YARP code in your own program.
How to get data from YARP's name server without using any YARP code in your own program.
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#ifdef WIN32
#include <winsock2.h>
#else
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#endif
#define PORT 10000
#define HOST "127.0.0.1"
#define BUFSIZE 1000
#ifdef WIN32
void windowsNetStart(void) {
int wsaRc;
WSADATA wsaData;
if(wsaRc=WSAStartup(0x0101, &wsaData)) {
perror("WinSock init");
}
if(wsaData.wVersion != 0x0101) {
WSACleanup();
perror("wsaData.wVersion");
}
}
#endif
int main(
int argc,
char *argv[]) {
char buf[BUFSIZE];
int i;
#ifdef WIN32
SOCKET sd;
#else
int sd;
#endif
struct sockaddr_in pin;
struct hostent *hp;
if (argc<=1) {
printf("Please supply a message to send to the nameserver. ");
printf(" Examples:\n");
printf(" help\n");
printf(" list\n");
printf(" query /portname\n");
exit(1);
}
#ifdef WIN32
windowsNetStart();
#endif
if ((hp = gethostbyname(HOST)) == 0) {
perror("gethostbyname");
exit(1);
}
memset(&pin, 0, sizeof(pin));
pin.sin_family = AF_INET;
pin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
pin.sin_port = htons(
PORT);
if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
if (connect(sd,(struct sockaddr *) &pin, sizeof(pin)) == -1) {
perror("connect");
exit(1);
}
buf[0] = '\0';
strncat(buf,"NAME_SERVER",sizeof(buf));
for (i=1; i<argc; i++) {
strncat(buf," ",sizeof(buf));
strncat(buf,argv[i],sizeof(buf));
}
strncat(buf,"\n",sizeof(buf));
printf("Message to send to name server:\n");
printf("%s", buf);
if (send(sd, buf, strlen(buf), 0) == -1) {
perror("send");
exit(1);
}
for (i=0; i<BUFSIZE; i++) {
buf[i] = '\0';
}
if (recv(sd, buf, BUFSIZE, 0) == -1) {
perror("recv");
exit(1);
}
printf("Response from yarp server:\n");
printf("%s", buf);
#ifdef WIN32
closesocket(sd);
#else
close(sd);
#endif
}
int main(int argc, char *argv[])