Linux-ARM 连接网络摄像头,tcp封装http协议的问题 5C

1.问题背景
有一块裁剪过的ARM板要用网线和网络摄像头直连,厂商只提供了CGI-API,没有其他接口,由于板子资源限制,也没法装http服务和curl。目前考虑用tcp 封装http 进行控制。

尝试过用Wireshark抓过浏览器和摄像头通信的包结构如下

GET /cgi-bin/images_cgi?channel=0&user=admin&pwd=admin HTTP/1.1
Accept: text/html, application/xhtml+xml, image/jxr, */*
Accept-Language: zh-CN
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko
Accept-Encoding: gzip, deflate
Host: 192.168.14.18
DNT: 1
Connection: Keep-Alive

问题:
我把cookie删掉了,其余部分直接同过socket调试工具发给摄像头是有应的
图片说明

然后我tcp客户端也按照相同的格式send,但是始终没有收到摄像头的回应,是哪边的问题?

tcp 客户端代码:

static protocol_err_t protocol_CameraCommand(PROTOCOL *this)
{
    unsigned short port = 80;           // 服务器的端口号
    char *server_ip = "192.168.14.18";  // 服务器ip地址

    int sockfd = socket(AF_INET, SOCK_STREAM, 0);// 创建TCP套接字
    if(sockfd < 0)
    {
        protocol_SendString(this,"socket failed\n");
        return PROTOCOL_ERR_FAIL;
    }

    struct sockaddr_in server_addr; //定义服务器信息结构体
    bzero(&server_addr,sizeof(server_addr)); 
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(port);
    inet_pton(AF_INET, server_ip, &server_addr.sin_addr);

    int err_log = connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));   // 主动连接服务器
    if(err_log != 0)
    {
        sprintf(this->sendBuffer, "connect error: %s(errno: %d)\n",strerror(errno),errno);
        protocol_SendString(this, this->sendBuffer);
        close(sockfd);
        return PROTOCOL_ERR_FAIL;
    }

    char str1[1024] = "";
    sprintf(str1, "%s\r\n","GET /cgi-bin/date_cgi?action=get&user=admin&pwd=admin HTTP/1.0"); //服务端接收数据处理文件地址,并带参数
    sprintf(str1, "%s%s\r\n",str1,"Accept: text/html, application/xhtml+xml, image/jxr, */*");
    sprintf(str1, "%s%s\r\n",str1,"Accept-Language: zh-CN");
    sprintf(str1, "%s%s\r\n",str1,"User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko");
    sprintf(str1, "%s%s\r\n",str1,"Accept-Encoding: gzip, deflate");
    sprintf(str1, "%s%s\r\n",str1,"Host: 192.168.14.18"); //服务器地址
    sprintf(str1, "%s%s\r\n",str1,"Connection: Keep-Alive");
    //sprintf(str1,"%s%s\r\n",str1,"Cookie: JSESSIONID=5386A9443729D7EB0B61E38A9C7CF52F");
    sprintf(str1, "%s\r\n",str1);
    protocol_SendString(this,"----------------------------- HTTP Data ----------------------------------\n\n");
    sprintf(this->sendBuffer,"%s",str1);
    protocol_SendString(this, this->sendBuffer);
    sprintf(this->sendBuffer,"--------------------------- Data Len=%d ----------------------------------\n\n",strlen(str1));
    protocol_SendString(this, this->sendBuffer);

    int ret=send(sockfd, str1, strlen(str1), 0);   // 向服务器发送信息
    if(ret<0)
    {
        protocol_SendString(this,"send");
        close(sockfd);
        return PROTOCOL_ERR_FAIL;
    }
    protocol_SendString(this, "------------------------ server retrun data -------------------------------\n");
    char recv_buf[1024*10240]="";
    recv(sockfd, recv_buf, sizeof(recv_buf), 0);

    sprintf(this->sendBuffer,"%s\n\n",recv_buf);
    protocol_SendString(this, this->sendBuffer);
    close(sockfd);

    return PROTOCOL_ERR_NONE;
}

1个回答

假设代码有问题,排查步骤:
先在电脑上跑通,请求互联网上的网站HTTP服务
再在电脑上跑通,请求摄像头的HTTP服务
再在ARM板上跑通

DNS解析,代码可以调getaddrinfo得到(调试测试直接用ping看一下ip,代码写死ip就行)

自己发送http请求最少只要三行就够了,别的没有用的内容去掉,
例如 连接 www.baidu.com后发送
GET / HTTP/1.1\r\n
Host: www.baidu.com\r\n
\r\n

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问