1.问题背景
需要将arm用网线连接网络摄像头对其进行控制。厂商只提供了CGI接口,且由于系统是裁剪过的curl这些也用不了,所以尝试使用tcp协议直接封装成http。
2.问题现象
用tcp工具,pc作为客户端连接摄像头,发送可以直接接收到有效返回。但是arm板与tcp测试工具或者摄像头都连不上,报111
客户端代码如下
static protocol_err_t protocol_CameraCommand(PROTOCOL *this)
{
unsigned short port = 8000; // 服务器的端口号
char *server_ip = "192.168.13.11"; // 服务器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.1"); //服务端接收数据处理文件地址,并带参数
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.13.11"); //服务器地址
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");
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;
}
char recv_buf[521]="";
recv(sockfd, recv_buf, sizeof(recv_buf), 0);
protocol_SendString(this, "------------------------ server retrun data -------------------------------\n");
sprintf(this->sendBuffer,"%s\n\n",recv_buf);
protocol_SendString(this, this->sendBuffer);
close(sockfd);
return PROTOCOL_ERR_NONE;
}