LWIP netconn TCP UDP測試 執行個體

來源:互聯網
上載者:User

本文由自己從網路上查資料整理而成 已經測試可用

只需修改下就能用到項目中

給需要的童鞋們參考

平台(LM3S9B92+UCOSII+LWIP)



線上IP地址轉換器(二進位 十進位 十六進位轉換)

http://www.ab126.com/goju/1840.html


LWIP netconn API函數下 實現的

TCP伺服器 

TCP用戶端

  UDP測試



UDP流程

        conn = netconn_new(NETCONN_UDP);       //建立UDP串連
        netconn_bind(conn,IP_ADDR_ANY, 80);   //綁定連接埠號碼
netconn_connect(conn,&destip,destport);
netconn_send(conn,UDPNetbuf);  //收或發
netconn_delete(conn);





主程式(三個線程只開啟了一個UDP的TCP測試的被注釋了每次開啟一個即可測試)

#include <includes.h>#include"lwip/sys.h"#include "user/my_tcp_client.h"#include "utils/uartstdio.h"                               /*  Application tasks           */#define          TASK_INPUT_PRIO       11#define          TASK_LWIP_PRIO        3#define taskinputstck    1024#define tasklwipstck     1024static  OS_STK       App_TaskStartStk[APP_CFG_TASK_START_STK_SIZE];OS_STK taskinput_stack[taskinputstck];OS_STK tasklwip_stack[tasklwipstck];extern struct netif lwip_netif;extern void stellarisif_input(void *arg);extern void httpd_thread(void *arg);extern void my_lwip_init(void);extern void tftp_thread(void *arg);static  void  App_TaskStart   (void *p_arg);int main (void){BSP_PreInit ();BSP_IntDisAll();OSInit();                                                   /* Initialize "uC/OS-II, The Real-Time Kernel"          */    OSTaskCreateExt((void (*)(void *)) App_TaskStart,           /* Create the start task                                */                (void           *) 0,                (OS_STK         *)&App_TaskStartStk[APP_CFG_TASK_START_STK_SIZE - 1],                (INT8U           ) APP_CFG_TASK_START_PRIO,                (INT16U          ) APP_CFG_TASK_START_PRIO,                (OS_STK         *)&App_TaskStartStk[0],                (INT32U          ) APP_CFG_TASK_START_STK_SIZE,                (void           *) 0,                (INT16U          )(OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR));OSStart();}static  void  App_TaskStart (void *p_arg){(void)p_arg;    BSP_PostInit();                                             /* Initialize BSP functions                             */Tmr_TickInit ();    /* Initialize the SysTick                               */my_lwip_init();        OSTaskCreate(stellarisif_input,                 (void *)&lwip_netif,                 &taskinput_stack[taskinputstck - 1],                 TASK_INPUT_PRIO);//OSTaskCreate(httpd_thread,/*OSTaskCreate(tftp_thread,                 (void *)0,                 &tasklwip_stack[tasklwipstck - 1],                 TASK_LWIP_PRIO);*//*OSTaskCreate(my_tcp_client_thread,                 (void *)0,                 &tasklwip_stack[tasklwipstck - 1],                 TASK_LWIP_PRIO);*///sys_thread_new("tcpserv",my_tcp_server_thread, (void *)0, 1024, 2);  //測試TCP伺服器//sys_thread_new("tcpclt",my_tcp_client_thread, (void *)0, 1024, 2);   //測試TCP用戶端sys_thread_new("udp",my_udp_thread, (void *)0, 1024, 2);              //測試UDP#if (OS_TASK_STAT_EN > 0)    OSStatInit();                                               /* Determine CPU capacity                               */#endif                           for(;;){OSTaskSuspend(OS_PRIO_SELF);                            /*  The start task can be pended here.                  */  }}


測試程式

標頭檔

#ifndef  __MY_TCP_CLIENT_H__#define  __MY_TCP_CLIENT_H__void my_tcp_server_thread(void *arg);void my_tcp_client_thread(void *arg);void my_udp_thread(void *arg);#endif


.C檔案

 #include "includes.h"               /* uC/OS interface */#include "lwip/api.h"#include <stdio.h>#include <string.h>#include "user/my_tcp_client.h"#include "utils/uartstdio.h"//*********************************************************////  tcp 伺服器端測試程式////*********************************************************void my_tcp_server_thread(void *arg){    struct netconn *conn, *newconn = NULL;    struct netbuf        *TCPNetbuf;char getText[]="hello i am server!";        conn = netconn_new(NETCONN_TCP);      /* 建立TCP串連  */    netconn_bind(conn,NULL,80);           /* 綁定本地地址和監聽的連接埠號碼 */      netconn_listen(conn);                 /* 進入監聽狀態 */UARTprintf("\033[2JTCP listening\n");    while(1)    {      newconn = netconn_accept(conn);    /*阻塞當前進程到有資料接收 */      if(newconn != NULL)      {                     /* 發送資料  */                netconn_write(newconn,(void *)&getText,sizeof(getText),NETCONN_NOCOPY);                netbuf_delete(TCPNetbuf);                     netconn_close(newconn);       /* 關閉串連   */                      while(netconn_delete(newconn) != ERR_OK)            OSTimeDlyHMSM(0, 0, 1, 0);      }    }}//*********************************************************////  tcp 用戶端測試程式////*********************************************************void my_tcp_client_thread(void *arg){    struct netconn *conn;    struct netbuf        *TCPNetbuf;//char *dat;err_t myerr;char Text[]="hello i am client!";static struct ip_addr serverip;   //目標機IP    static unsigned short serverport;       //目標機連接埠號碼serverip.addr = htonl(0xC0A8016A); //192.168.1.106serverport=7000;        conn = netconn_new(NETCONN_TCP);      /* 建立TCP串連  */    netconn_bind(conn,NULL,7000);           /* 綁定本地地址和監聽的連接埠號碼 */ OSTimeDlyHMSM(0, 0, 3, 0);UARTprintf("\033[2JTCP connect....\n"); myerr=netconn_connect(conn,&serverip,serverport); //串連主機 cnn,目標機IP,連接埠號碼if(myerr==-1){UARTprintf("\033[2JTCP connect err\n");}else{ UARTprintf("\033[2JTCP connect ok\n");}   /* 建立一個新的netbuf */TCPNetbuf =netbuf_new();//netbuf_alloc(TCPNetbuf, 40);/* 引用這個文本給netbuf */netbuf_ref(TCPNetbuf,Text,sizeof(Text));     while(1)    {   OSTimeDlyHMSM(0, 0, 3, 0);/* 發送文本 */netconn_send(conn,TCPNetbuf);netconn_write(conn,(void *)&Text,sizeof(Text),NETCONN_NOCOPY);            OSTimeDlyHMSM(0, 0, 3, 0);/* 刪除conn和buf */netconn_delete(conn);netbuf_delete(TCPNetbuf);    }}//*********************************************************////  tcp 用戶端測試程式////*********************************************************void my_udp_thread(void *arg){        static struct netconn *conn;        static struct netbuf  *UDPNetbuf;        static struct ip_addr destip;   //目標機IP        static unsigned short destport;       //目標機連接埠號碼        unsigned char Array[]="hello i am udp!";destip.addr = htonl(0xC0A8016A); //192.168.1.106    destport=7000;     /* 建立一個新的netbuf *///UDPNetbuf =netbuf_new();        conn = netconn_new(NETCONN_UDP);       //建立UDP串連        netconn_bind(conn,IP_ADDR_ANY, 80);   //綁定連接埠號碼        while(1)        { OSTimeDlyHMSM(0, 0, 3, 0); UARTprintf("\033[2JUDP connec...\n"); /* 串連遠程主機 */netconn_connect(conn,&destip,destport);/* 建立一個新的netbuf */UDPNetbuf= netbuf_new();/* 引用這個文本給netbuf */netbuf_ref(UDPNetbuf,Array, sizeof(Array));/* 發送文本 */netconn_send(conn,UDPNetbuf);OSTimeDlyHMSM(0, 0, 3, 0);/* 刪除conn和buf */netconn_delete(conn);netbuf_delete(UDPNetbuf);   //釋放緩衝區        }  }


最後附上一個TCP伺服器接收並把收到的資料返回的參考


        /* Grab new connection. */
        newconn = netconn_accept(conn);
    
        /* Process the new connection. */
        if (newconn) 
        {
          struct netbuf *buf;
          void *data;
          u16_t len;
      
          while ((buf = netconn_recv(newconn)) != NULL) 

          {
            do 
            {
              netbuf_data(buf, &data, &len);
              netconn_write(newconn, data, len, NETCONN_COPY);
            } 
            while (netbuf_next(buf) >= 0);
          
            netbuf_delete(buf);
          }
        
          /* Close connection and discard connection identifier. */
          netconn_close(newconn);
          netconn_delete(newconn);


參考文章 http://www.amobbs.com/thread-5059971-1-1.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.