void tcp_init() {
int ret=0;
uint64_t start;
if(tcp_mutex==NULL)
tcp_mutex = nwy_create_mutex();
nwy_sim_get_imei(&imei);
nwy_sdk_file_unlink(tcp_fail_txt);
start = nwy_get_ms();
do
{
if(nwy_get_ms()-start>=10000)
break;
ret = unistep_tcp();
nwy_ext_echo("tcp ret :%d",ret);
nwy_sleep(100);
}while(ret != 0);
//�1�7�1�7�1�7�1�7�1�7�1�7�1�7�0�4�1�7�?1�7�1�7�1�7�1�7�1�7�1�9�1�7
if(0 == ret&&send_location_thread==NULL)
send_location_thread = nwy_create_thread("locationthread", tcp_send_location, NULL, NWY_OSI_PRIORITY_NORMAL, 1024*5, 16);
else
nwy_resume_thread(send_location_thread);
}
这里是引用`、、、、、、
在这里插入代码片
int unistep_tcp()
{
int ret;
char *url_or_ip = "120.27.18.139";
char* sptr = nwy_ext_sio_recv_buff;
int port = 4999;
int on = 1;
int opt = 1;
uint64_t start;
struct sockaddr_in sa;
struct sockaddr_in *to4 = (struct sockaddr_in *)&sa;
to4->sin_len = sizeof(struct sockaddr_in);
to4->sin_family = AF_INET;
to4->sin_port = htons(port);
ip_addr_t addr = {0};
char ip_buf[64] = {0};
nwy_set_sim_detect_mode(0);
ret = nwy_hostname_check(url_or_ip, ip_buf);
if (ret != NWY_SUCESS) {
nwy_ext_echo("\r\ntcp ip error:\r\n");
}
addr.u_addr.ip4.addr = ipaddr_addr(ip_buf);
if(tcp_connect_flag)
{
nwy_ext_echo("\r\ntcp alreay connect");
return -4;
}
//�1�7�1�7�1�7�1�0�1�7�1�7�1�7
if (0 == nwy_ext_check_data_connect()) {
nwy_ext_echo("\r\ndata call not setup");
nwy_data_start_call_v02_t param_t;
memset(¶m_t,0,sizeof(nwy_data_start_call_v02_t));
param_t.profile_idx = 1;
param_t.is_auto_recon = 1;
param_t.auto_re_max = 0;
param_t.auto_interval_ms = 100;
ret = nwy_data_start_call(1,¶m_t);
OSI_LOGI(0, "=DATA= nwy_data_start_call ret= %d", ret);
if (ret != NWY_RES_OK)
nwy_ext_echo("\r\nStart data call fail, result<%d>\r\n",ret);
else
nwy_ext_echo("\r\nStart data call ...\r\n");
}
nwy_sleep(100);
//�1�7�1�7�1�7�1�7�1�7�0�7�1�7�1�7�1�7
if (sock_fd == 0) {
sock_fd= nwy_socket_open(AF_INET, SOCK_STREAM, IPPROTO_TCP);
}
inet_addr_from_ip4addr(&to4->sin_addr, ip_2_ip4(&addr));
nwy_socket_setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
nwy_socket_setsockopt(sock_fd, IPPROTO_TCP, TCP_NODELAY, (void *)&opt, sizeof(opt));
while(0 != nwy_socket_set_nonblock(sock_fd))
{
nwy_ext_echo("\r\nsocket set err\r\n");
nwy_sleep(100);
}
start = nwy_get_ms();
//�1�7�1�7�1�7�0�5�1�7�1�7�1�7�1�7�1�7
do
{
ret = nwy_socket_connect(sock_fd, (struct sockaddr *)&sa, sizeof(sa));
if(ret)
{
}
if((nwy_get_ms()-start)>= 10000 )
{
nwy_ext_echo("\r\nsocket connect timeout\r\n");
nwy_socket_shutdown(sock_fd, SHUT_RD);
nwy_socket_close(sock_fd);
sock_fd = 0;
return -1;
}
if(ret == -1)
{
//nwy_ext_echo("errno=%d\r\n", nwy_socket_errno());
if( EALREADY == nwy_socket_errno() || EISCONN == nwy_socket_errno() )
{
nwy_ext_echo( "\r\nnwy_net_connect_tcp connect ok..");
tcp_connect_flag = 1;
break;
}
if (EINPROGRESS != nwy_socket_errno() )
{
nwy_ext_echo("\r\nconnect errno = %d", nwy_socket_errno());
nwy_socket_close(sock_fd);
sock_fd = 0;
tcp_connect_flag = 0;
return -2;
}
}
nwy_sleep(100);
}while(1);
//�1�7�1�7�1�7�1�7�1�7�1�7�1�7�1�7�1�7�1�7�0�4�1�7�1�9�1�7
if(tcp_connect_flag)
{
if(NULL == tcp_recv_thread )
tcp_recv_thread = nwy_create_thread("tcp_recv_thread",nwy_tcp_recv_func, (void*)&sock_fd, NWY_OSI_PRIORITY_NORMAL, 2048, 4);
else
nwy_resume_thread(tcp_recv_thread);
nwy_ext_echo("\r\ntcp_recv_thread created!");
if(tcp_recv_thread ==NULL)
{
nwy_ext_echo("\r\ncreate tcp recv thread failed, close connect");
nwy_socket_close(sock_fd);
sock_fd = 0;
tcp_connect_flag = 0;
return -3;
}
return 0;
}
}
|