zoukankan      html  css  js  c++  java
  • lwip TCP client 客户端 & FreeRTOS

    static void tcpecho_thread(void *arg)
    {
    
      ip_addr_t serverIpAddr;
      struct netbuf *buf;
      void *data;
      u16_t len;
      err_t err;
      struct netconn *conn;
    
      IP4_ADDR(&serverIpAddr,192,168,1,13);// 服务器IP地址
          
      LWIP_UNUSED_ARG(arg);
      
          for( ;; )
          {
                    
                    printf("正在建立连接服务器到:192.168.1.13 :65000...
    ");
                    
                    
                    /* Create a new connection identifier. */
                    conn = netconn_new(NETCONN_TCP);
                    
                    if (conn!=NULL)
                    {
            
                        /* Netconn connection to Server IP , port number 65000. */
                
                        err = netconn_connect(conn, &serverIpAddr, 65000);
                        
                        if(err == ERR_OK)
                        {
    
                            printf("TCP Server 192.168.1.13 :65000 连接成功.
    ");
                            
                            for( ;; )
                            {
                                
                                /* receive data until the other host closes the connection */
                                if((err = netconn_recv(conn, &buf)) == ERR_OK) //这个是死等 TCP 数据
                                {
                                     //获取一个指向netbuf 结构中的数据的指针
    
                                     if((err = netbuf_data(buf, &data, &len)) == ERR_OK)
                                     {
                                         
                                            //接收到的数据 转发给串口1,来达到透传的目的
                                            comSendBuf(COM1,data,len);
                                         
                                            netbuf_delete(buf);
                                         
                                     }
                                     else
                                     {
                                         printf("err:netbuf_data(buf, &data, &len):%d.
    ",err);
                                     }
                                    
                                }
                                else//if((err = netconn_recv(conn, &buf)) == ERR_OK)
                                {
                                    
                                    printf("err:netconn_recv(conn, &buf):%d.
    ",err);
                                    
                                    netbuf_delete(buf);
                                    
                                    break;
                                }
                        
                            }
    
                        }
                    
                        printf("TCP Server 192.168.1.13 :65000 连接失败.
    ");
                        
                         netconn_close(conn);
                         netconn_delete(conn);
                        
                        /* vTaskDelayUntil是绝对延迟,vTaskDelay是相对延迟。*/
                        vTaskDelay(1000);
                    }
                    else//(conn!=NULL)
                    {
                        printf("Can not create TCP netconn.
    ");
                        
                        /* vTaskDelayUntil是绝对延迟,vTaskDelay是相对延迟。*/
                        vTaskDelay(1000);
                    }
    
                }
    }

    1、根据某位大神 总结的经验!

  • 相关阅读:
    测试模式 windows2008 内部版本7601
    移动端UC /QQ 浏览器的部分私有Meta 属性
    正则表达式 正向预查 负向预查
    获取指定元素的某一个样式属性值
    把普通对象转换成json格式的对象
    求平均数-----类数组转换成数组
    轮播图
    倒计时
    JS 预解释相关理解
    ul ol di三者区别
  • 原文地址:https://www.cnblogs.com/suozhang/p/6742967.html
Copyright © 2011-2022 走看看