LWIP netconn TCP UDP测试 实例

本文由自己从网络上查资料整理而成 已经测试可用

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

给需要的童鞋们参考

平台(LM3S9B92+UCOSII+LWIP)



在线IP地址转换器(二进制 十进制 十六进制转换)

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


LWIP netconn API函数下 实现的

TCP服务器 

TCP客户端

  UDP测试

LWIP netconn TCP UDP测试 实例_第1张图片

LWIP netconn TCP UDP测试 实例_第2张图片

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     1024

static  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.106
	serverport=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

你可能感兴趣的:(LWIP netconn TCP UDP测试 实例)