现在的位置: 首页 > 综合 > 正文

LWIP 无OS RAW-API 函数使用流程

2013年10月01日 ⁄ 综合 ⁄ 共 3804字 ⁄ 字号 评论关闭


简单的介绍下使用流程函数名只做演示用

代码来自于LWIP编程指南

一,作为客户端

网卡初始化

mac地址设置,自己的ip,子网掩码,网关设置

lwIPInit(MAC, local_ip, local_mask,local_gateway, IPADDR_USE_STATIC);

设置服务器ip  IP4_ADDR(&ipaddr,192,168,1,120);//server ip

连接服务器

tcp_new()

tcp_bind()

tcp_connect()  注意:这一步要注册连接成功的回调函数,在函数中可设置标志来标志连接成功

tcp_recv            注意:这一步要注册接收用的回调函数  一旦有数据接收就会调用在这里注册的回调函数

tcp_write

tcp_close()

const static uint8 TCP_TestData []="This is LwIP TCP Client  在Luminary Cortex-M3 上的测试!\r\n"; 
 void TCP_Client_Init(); 
  
 void  Delay(unsigned long  ulVal)                          /*  利用循环产生一定的延时         */ 
 { 
     while ( --ulVal  !=  0 ); 
 } 
  
 int main() 
 { 
   targetInit(); 
 
   InitNic(); 
   
   while(1) 
   { 
     TCP_Client_Init(); 
     Delay(1000000UL);   
     Delay(1000000UL);  
     Delay(1000000UL);  
    } 
 } 
  
 /*******  这是一个回调函数,当TCP 客户端请求的连接建立时被调用********/ 
 err_t TcpCli_Connected(void *arg ,struct tcp_pcb *pcb,err_t err) 
 { 
    tcp_write(pcb,TCP_TestData,sizeof(TCP_TestData),0);       /*   发送数据                        */ 
     
    tcp_close(pcb); 
     
    return ERR_OK; 
 } 
  
 void TCP_Client_Init() 
 { 
   struct tcp_pcb *Clipcb; 
   struct ip_addr ipaddr; 
    
   IP4_ADDR(&ipaddr,192,168,1,16); 
    
   Clipcb = tcp_new();                                     /*  建立通信的TCP 控制块(Clipcb)    */ 
   tcp_bind(Clipcb,IP_ADDR_ANY,1026);                    /*   绑定本地IP 地址和端口号         */ 
    
   tcp_connect(Clipcb,&ipaddr,1026,TcpCli_Connected); 
 }

二,作为服务器端

tcp_new

tcp_bind

tcp_listen

tcp_accpet(回调函数地址)    注意:在这里注册有连接后的回调函数,有连接时就会调用注册的回调函数


回调函数中:

tcp_setprio

tcp_recv

tcp_write

pbuf_free


/*************************TCP 测试的WEB服务器建立网页与服务器声明***********************/ 
 const static uint8 indexdata[]="<html>\ 
                          <head><title>广州致远电子</title></head>\ 
                          <body>\ 
                            这里是LwIP TCP在Luminary Cortex-M3 上的测试!\ 
                          </body>\ 
                          </html>"; 
 const static uint8 http_html_hdr []="HTTP/1.1  200 OK\r\nContent-type: text/html\r\n\r\n"; 
  
 /*******************  这是一个回调函数,当一个TCP 段到达这个连接时会被调用*****************/ 
 static err_t http_recv(void *arg, struct tcp_pcb *pcb,struct pbuf *p,err_t err) 
 { 
   if(p != NULL) 
  { 
     tcp_write(pcb,http_html_hdr,sizeof(htt p_html_hdr),0);             /*  发送http 协议头部信息      */ 
     tcp_write(pcb,indexdata,sizeof(indexdata),0);                    /*  发送http 网页信息          */ 
      
     pbuf_free(p);                                              /*  释放该TCP 段             */ 
  } 
   tcp_close(pcb);                                              /* 关闭这个连接              */ 
   err = ERR_OK; 
   return err; 
 } 
  
 /********************** 这是一个回调函数,当一个连接已经接受时会被调用**********************/ 
 static err_t http_accept(void *arg,struct tcp_pcb *pcb,err_t err) 
 { 
   tcp_setprio(pcb, TCP_PRIO_MIN);                              /*   设置回调函数优先级,当   */ 
                                                             /*   存在几个连接时特别重要,  */ 
 /*   此函数必须调用*/ 
   tcp_recv(pcb,http_recv);                                      /*   设置TCP 段到时的回调函数 */ 
     
   err = ERR_OK; 
 return err; 
 } 
 void http_init(void) 
 { 
   struct tcp_pcb *pcb; 
    
   pcb = tcp_new();                                          /*  建立通信的TCP 控制块(pcb)    */ 
   tcp_bind(pcb,IP_ADDR_ANY,80);                           /*  绑定本地IP 地址和端口号      */ 
   pcb = tcp_listen(pcb);                                      /*  进入监听状态                 */ 
   tcp_accept(pcb,http_accept);                                /*  设置有连接请求时的回调函数   */ 
 } 
  
 int main() 
 { 
   targetInit(); 
   InitNic(); 
   
   http_init(); 
   while(1) 
   { 
     ; 
   } 
 }

httpd实例

//*****************************************************************************
 //!
 //! 这个函数是当有数据传送到控制器时会被调用。
 //! 
 //*****************************************************************************
 static err_t http_recv(void *arg, struct tcp_pcb *pcb,struct pbuf *p,err_t err)
 {
 	char RecivePcBuf[1600];
 //	 	unsigned char FROK;
 	tcp_recved(pcb,p->len);	
 	strcpy(RecivePcBuf,p->payload);
 	if(p != NULL)
 	{
 		
 	tcp_write(pcb,p->payload,p->len,1);
 		        
 	}
 	pbuf_free(p); /* 释放该TCP段 */
 	 											  
 //	tcp_close(pcb); /* 关闭这个连接 */
 	err = ERR_OK;
 	return err;
 }
 
 //*****************************************************************************
 //!
 //! This is a call back servic function ,when a connection was accepet this 
 //! function will be called .
 //! 
 //*****************************************************************************
 static err_t http_accept(void *arg,struct tcp_pcb *pcb,err_t err)
 {
 	//
 	// 设置回调函数优先级,当存在几个连接时特别重要,此函数必须调用
 	//
 	tcp_setprio(pcb, TCP_PRIO_NORMAL);
 	//
 	// 设置TCP段到时的回调函数
 	//
 	tcp_recv(pcb,http_recv);
 	err = ERR_OK;
 	return err;
 }
 //*****************************************************************************
 //!
 //! This is the data dealwith function
 //! 
 //*****************************************************************************
 
 
 //*****************************************************************************
 //!
 //! Initial the TCP server
 //! 
 //*****************************************************************************
 
 void http_init(void)
 { 
 
 	//
 	// 建立通信的TCP控制块(pcb)
 	//
 	pcbs = tcp_new();
 	//
 	// 绑定本地IP地址和端口号
 	//
 	tcp_bind(pcbs,IP_ADDR_ANY,1024); 
 	//
 	//  进入监听状态
 	//
 	pcbs = tcp_listen(pcbs); 
 	//
 	// 设置有连接请求时的回调函数
 	//
 	tcp_accept(pcbs,http_accept);
 }

抱歉!评论已关闭.