开发者

Child commands in popen telnet

I am trying to establish a local telnet session in C++ and send commands/receive data. Right now I have:

const char *cmd = "telnet 127.0.0.1 2006";
char buffer[256];

FILE *pipe = popen(cmd, "rw");   
//if( !pipe ) { perror("popen"); exit(-1); }

while( fgets(buffer, sizeof(buffer), pipe) != NULL &&   
      !feof(pipe) )
{
    if( ferror(pipe) ) { perror("fgets"); brea开发者_如何学Gok; }

    /* Here you do whatever you want with the data. */
    printf("%s", buffer);
}

pclose(pipe);

Which is opening the telnet connection. I need to send a command like: "/neighbors" and then receive the data it would return. Ideally, the session would remain open and I would re-query "/neighbors" every 20 seconds or so.

I think I need to create a child process with fork(), but I am very new to this process.


Using telnet seems like a rather roundabout way to do this. Have you considered using regular sockets to talk to the remote process? For example, try this guide to socket programming.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜