重定向telnet方法

在现场调试嵌入式设备时,很多时候我们是无法连接串口查看打印信息的,只可以通过网络连接telnet登陆到设备终端,
而此时是无法查看当前运行的应用程序的打印信息的,为我们排查问题带来了一定的困难。当然,我们可以通过gdb工具
attach进程进行调试,但这种方式还是比较麻烦的。我根据gdb的原理,实现了一个快速将应用程序标准输出打印到telnet
终端的工具。代码如下:
============================================================
服务端程序
#include 
#include 
#include 
#include 
#include 
#include 
#include 
int stdio_redirect_daemon(char * tty)
{
    int ret = -1; 
    if(tty==NULL)
    {
        printf("print_redirection,parameter is null.\n");
        return -1;
    } 
    close(1);
    close(2);
    ret = (int)open(tty, 1);
    if(ret<0)
    {
    printf("redirect stdout to tty error:%s.\n",strerror(errno));
    return ret; 
    }
    ret = (int)open(tty, 2);
    if(ret<0)
    {
        printf("redirect stderr to tty error:%s.\n",strerror(errno));
         return ret; 
    }
    fflush(NULL);
    return 0;
}


int main()
{
    char tty[32]={0};
    //从客户端指定的文件中(/tmp/cttyname)读取
    int fd=open("/tmp/cttyname",O_RDWR);
    read(fd,tty,32);
    while(1)
    {
        if(access("/tmp/cttyflag",F_OK)<0)
        {
           ;
        }
        else
        {
           stdio_redirect_daemon(tty);
        }
        sleep(1);
    }
    return 0; 
}
===============================================================
ctty客户端
int main()
{
    char *tty=ttyname(1);
    char cmd[64]={0};
    strcpy(cmd,"echo ");
    strcat(cmd,tty);
    strcat(cmd, " /tmp/cttyname");
    system(cmd);
    system("touch /tmp/cttyflag");
    while(1)
    {
        sleep(1);
    }
    return 0;
}

服务端需要嵌入到应用程序中。客户端单独编译后部署到设备中,当登陆到设备后,运行客户端,即可实时

查看到当前应用程序在标准终端的输出。


TTY控制终端简单说明


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值