簡(jiǎn)單個(gè)人博客模板網(wǎng)站網(wǎng)站內(nèi)容管理系統(tǒng)
文章目錄
- 1. TCP通信 客戶端(關(guān)鍵配置)
- 2. TCP 服務(wù)端配置
- 3. UDP 點(diǎn)播通信
- 4. UDP 廣播通信
- 5. UIP_UDP_APPCALL 里邊的處理example
- 6. TCP數(shù)據(jù)處理 ,UIP_APPCALL調(diào)用的函數(shù)
UIP_APPCALL TCP的數(shù)據(jù)都在這個(gè)宏定義的函數(shù)里進(jìn)行數(shù)據(jù)處理的
UDP 數(shù)據(jù)在#define UIP_UDP_APPCALL udp_appcall 中處理
1. TCP通信 客戶端(關(guān)鍵配置)
// 定義 MAC 地址(6 字節(jié))static const uint8_t mac_address[6] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55};// 設(shè)置 MAC 地址memcpy(ethaddr.addr, mac_address, sizeof(mac_address));uip_setethaddr(ethaddr);uip_ipaddr_t ipaddr;uip_init(); // uIP初始化uip_ipaddr(ipaddr, 192, 168, 1, 137); // 設(shè)置本地設(shè)置IP地址uip_sethostaddr(ipaddr);uip_ipaddr(ipaddr, 192, 168, 1, 1); // 設(shè)置網(wǎng)關(guān)IP地址(其實(shí)就是你路由器的IP地址)uip_setdraddr(ipaddr);uip_ipaddr(ipaddr, 255, 255, 255, 0); // 設(shè)置網(wǎng)絡(luò)掩碼uip_setnetmask(ipaddr);uip_ipaddr_t ipaddr;uip_ipaddr(&ipaddr, 192, 168, 1, 100); // 設(shè)置TCP Server IP為192.168.1.100uip_connect(&ipaddr, htons(8080)); // 端口為1400
2. TCP 服務(wù)端配置
uip_ipaddr_t ipaddr, remote_ip;// 定義 MAC 地址(6 字節(jié))static const uint8_t mac_address[6] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55};// 設(shè)置 MAC 地址memcpy(ethaddr.addr, mac_address, sizeof(mac_address));uip_setethaddr(ethaddr);// uIP初始化uip_init();/* ARP table initialize. */uip_arp_init();/*-----------UIP-TCP-Server---------------------*/// 設(shè)置本地設(shè)置IP地址uip_ipaddr(ipaddr, 192, 168, 1, 137);uip_sethostaddr(ipaddr);// 設(shè)置網(wǎng)關(guān)IP地址(其實(shí)就是你路由器的IP地址)uip_ipaddr(ipaddr, 192, 168, 1, 1);uip_setdraddr(ipaddr);// 設(shè)置網(wǎng)絡(luò)掩碼uip_ipaddr(ipaddr, 255, 255, 255, 0);uip_setnetmask(ipaddr);// 設(shè)置 遠(yuǎn)程客戶端IP為192.168.1.100uip_ipaddr(&remote_ip, 192, 168, 1, 100);uip_connect(&remote_ip, htons(7090)); /*連接到遠(yuǎn)程客戶端的通信端口為7090 *//* We start to listen for connections on TCP port 8090. */uip_listen(HTONS(8090));/*-----------UIP-TCP-Server---------------------*/
3. UDP 點(diǎn)播通信
uip_ipaddr_t remote_addr;struct uip_udp_conn *udp_conn;// 設(shè)置遠(yuǎn)程主機(jī)的 IP 地址為 192.168.1.100uip_ipaddr(&remote_addr, 192, 168, 1, 100);// 創(chuàng)建到 remote 192.168.1.100:1215 的 UDP 連接udp_conn = uip_udp_new(&remote_addr, HTONS(1215));if (udp_conn == NULL){UART1_Send_String("Failed to create UDP connection! \r\n");}else{uip_udp_bind(udp_conn, HTONS(1234)); // 綁定本地端口 1234}
4. UDP 廣播通信
UDP 廣播需要打開(kāi)這個(gè)宏
#ifndef UIP_CONF_BROADCAST
#define UIP_BROADCAST 1
#else /* UIP_CONF_BROADCAST /
#define UIP_BROADCAST UIP_CONF_BROADCAST
#endif / UIP_CONF_BROADCAST */
/*Broadcast IP ADDR*/uip_ipaddr_t Broadcast_addr;struct uip_udp_conn *udp_conn;// UDP廣播地址,本局域網(wǎng)中的所有設(shè)備uip_ipaddr(&Broadcast_addr, 255, 255, 255, 255);/*UDP 廣播端口1212*/udp_conn = uip_udp_new(&Broadcast_addr, HTONS(0));if (udp_conn == NULL){UART1_Send_String("Failed to create UDP connection! \r\n");}else{uip_udp_bind(udp_conn, HTONS(3956));}
5. UIP_UDP_APPCALL 里邊的處理example
/*udp rx data*/if (uip_newdata()){#if (USE_GVCP==1)gvcp_discover_callback();#elsechar *data = (char *)uip_appdata;int len = uip_datalen();memset(udp_server_databuf, 0, 1500);memcpy((char *)udp_server_databuf,(char *)uip_appdata,uip_len);// 打印接收到的數(shù)據(jù)UART1_Send_String("[UDP_RX]:");UART1_Send_String(udp_server_databuf);UART1_Send_String("\r\n");#endif}// 當(dāng)需要重發(fā)、新數(shù)據(jù)到達(dá)、數(shù)據(jù)包送達(dá),通知uip發(fā)送數(shù)據(jù)if (uip_rexmit() || uip_newdata() || uip_poll()){#if (USE_GVCP==1)#else// 將數(shù)據(jù)復(fù)制到 uIP 的應(yīng)用層數(shù)據(jù)緩沖區(qū)memcpy(uip_appdata, message, sizeof(message));// 發(fā)送數(shù)據(jù)uip_udp_send(sizeof(message)); // 發(fā)送的字節(jié)數(shù)#endif}
6. TCP數(shù)據(jù)處理 ,UIP_APPCALL調(diào)用的函數(shù)
/*** @brief 這是一個(gè)TCP 服務(wù)器應(yīng)用回調(diào)函數(shù)。* 該函數(shù)通過(guò)UIP_APPCALL(tcp_demo_appcall)調(diào)用,實(shí)現(xiàn)Web Server的功能.* 當(dāng)uip事件發(fā)生時(shí),UIP_APPCALL函數(shù)會(huì)被調(diào)用,根據(jù)所屬端口(1200),確定是否執(zhí)行該函數(shù)。* 例如 : 當(dāng)一個(gè)TCP連接被創(chuàng)建時(shí)、有新的數(shù)據(jù)到達(dá)、數(shù)據(jù)已經(jīng)被應(yīng)答、數(shù)據(jù)需要重發(fā)等事件*/
void tcp_server_demo_appcall(void)
{// 連接終止if (uip_aborted()){uip_log("TCP_Server Aborted!\r\n"); // 打印log}// 連接超時(shí)if (uip_timedout()){uip_log("TCP_Server Timeout!\r\n"); // 打印log}// 連接關(guān)閉if (uip_closed()){uip_log("TCP_server CLOSED!\r\n"); // 打印log}// 連接成功if (uip_connected()){uip_log("TCP_Server Connected OK!\r\n"); // 打印log}// 發(fā)送的數(shù)據(jù)成功送達(dá)if (uip_acked()){// uip_log("TCP_Server ACK OK!\r\n");}else{/*數(shù)據(jù)發(fā)送失敗*/// uip_log("TCP_Server NOT ACK!\r\n");}/* 接收到一個(gè)新的TCP數(shù)據(jù)包,收到客戶端發(fā)過(guò)來(lái)的數(shù)據(jù)*/if (uip_newdata()){memcpy(&request, uip_appdata, uip_len);if (PROTOCOL_REQUEST_MAGIC == request.header.nMagic){// uip_log("Recive APP SDK Update!\r\n");protocol_handle_request(&request, &response);}else{uip_log("Recive APP Bin data ERROR! \r\n");}uip_send(&response, response.buf.len);}}/*** @brief TCP應(yīng)用接口函數(shù)(UIP_APPCALL)* 完成TCP服務(wù)(包括server和client)*/
void tcp_demo_appcall(void)
{switch (uip_conn->lport) // 本地監(jiān)聽(tīng)端口80和1200{case HTONS(8090):tcp_server_demo_appcall();// uip_log("tcp_demo_appcall Local 8080 TCP Server!! \r\n");}#ifdef TCP_Client/*tcp client use */switch (uip_conn->rport) // 遠(yuǎn)程連接8080端口{case HTONS(8080): // 一旦有來(lái)自遠(yuǎn)程主機(jī)的信息,就調(diào)用此函數(shù)// tcp_client_demo_appcall();// uip_log("tcp_demo_appcall Remote 8080 TCP Client!! \r\n");break;}#endif}