Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
RT-Thread一般讨论
使用lwip实现tcp client客户端出现大量LAST_ACK
发布于 2014-01-25 19:29:26 浏览:4500
订阅该版
源码就是视频教程里用的lewei post请求的客户端源码,鄙人稍作改动。在实际运行时通过list_tcps()会发现每发送一次数据在列表里就会增加LAST_ACK状态的条目。并且线程运行一段时间后会死掉。 开发环境是 BSP:修改过STM32F10x 网卡:ENC28J60 目标MCU:STM32F103RB RTT版本:1.2.0 LWIP版本:1.4.1 rtconfig中关于LWIP的配置: ```/* SECTION: lwip, a lighwight TCP/IP protocol stack */ #define RT_USING_LWIP /* LwIP uses RT-Thread Memory Management */ #define RT_LWIP_USING_RT_MEM /* Enable ICMP protocol*/ #define RT_LWIP_ICMP /* Enable UDP protocol*/ #define RT_LWIP_UDP /* Enable TCP protocol*/ #define RT_LWIP_TCP /* Enable DNS */ #define RT_LWIP_DNS /* the number of simulatenously active TCP connections*/ #define RT_LWIP_TCP_PCB_NUM 3 /* Using DHCP */ #define RT_LWIP_DHCP /* ip address of target*/ #define RT_LWIP_IPADDR0 192 #define RT_LWIP_IPADDR1 168 #define RT_LWIP_IPADDR2 1 #define RT_LWIP_IPADDR3 30 /* gateway address of target*/ #define RT_LWIP_GWADDR0 192 #define RT_LWIP_GWADDR1 168 #define RT_LWIP_GWADDR2 1 #define RT_LWIP_GWADDR3 1 /* mask address of target*/ #define RT_LWIP_MSKADDR0 255 #define RT_LWIP_MSKADDR1 255 #define RT_LWIP_MSKADDR2 255 #define RT_LWIP_MSKADDR3 0 /* tcp thread options */ #define RT_LWIP_TCPTHREAD_PRIORITY 12 #define RT_LWIP_TCPTHREAD_MBOX_SIZE 10 #define RT_LWIP_TCPTHREAD_STACKSIZE 1024 /* ethernet if thread options */ #define RT_LWIP_ETHTHREAD_PRIORITY 15 #define RT_LWIP_ETHTHREAD_MBOX_SIZE 10 #define RT_LWIP_ETHTHREAD_STACKSIZE 512 /* TCP sender buffer space */ #define RT_LWIP_TCP_SND_BUF 6144 /* TCP receive window. */ #define RT_LWIP_TCP_WND 6144``` ```list_tcps() Active PCB states: #0 192.168.1.7:49267 <==> 42.121.254.11:80 snd_nxt 0x00049352 rcv_nxt 0x717BFDF5 state: LAST_ACK #1 192.168.1.7:49266 <==> 42.121.254.11:80 snd_nxt 0x0004800C rcv_nxt 0x94DE573D state: LAST_ACK #2 192.168.1.7:49265 <==> 42.121.254.11:80 snd_nxt 0x00046CEE rcv_nxt 0x1D2579E1 state: LAST_ACK #3 192.168.1.7:49264 <==> 42.121.254.11:80 snd_nxt 0x000459FA rcv_nxt 0x1C421329 state: LAST_ACK #4 192.168.1.7:49263 <==> 42.121.254.11:80 snd_nxt 0x00044730 rcv_nxt 0xFBEBD8D7 state: LAST_ACK #5 192.168.1.7:49262 <==> 42.121.254.11:80 snd_nxt 0x00043490 rcv_nxt 0x2AC7EE25 state: LAST_ACK #6 192.168.1.7:49261 <==> 42.121.254.11:80 snd_nxt 0x00042218 rcv_nxt 0x94081464 state: LAST_ACK #7 192.168.1.7:49260 <==> 42.121.254.11:80 snd_nxt 0x00040FCA rcv_nxt 0xC61D5942 state: LAST_ACK #8 192.168.1.7:49259 <==> 42.121.254.11:80 snd_nxt 0x0003FDA6 rcv_nxt 0x1AE6DC77 state: LAST_ACK #9 192.168.1.7:49258 <==> 42.121.254.11:80 snd_nxt 0x0003EBAC rcv_nxt 0xC5698353 state: LAST_ACK #10 192.168.1.7:49257 <==> 42.121.254.11:80 snd_nxt 0x0003D9DC rcv_nxt 0xE1ED398A state: LAST_ACK Listen PCB states: TIME-WAIT PCB states:``` ```#include
#include
#include
#include
#include "time.h" #define DEBUG_MODE #ifdef DEBUG_MODE #define DEBUG_PRINTF rt_kprintf #else #define DEBUG_PRINTF(...) #endif const char server[] = "www.lewei50.com"; char head[143]; char *user_data = RT_NULL; int user_str_length = 0; rt_bool_t begin = RT_FALSE; rt_bool_t end = RT_FALSE; #ifdef RT_USING_FINSH #include
int lewei_append(const char * name, int value) { int length; char * ptr; if(begin == RT_FALSE) { user_data = (char *)rt_malloc(2); if(user_data == NULL) { return -1; } user_data[0] = '['; user_data[1] = 0; user_str_length = 1; begin = RT_TRUE; } if(user_data == NULL) { return -1; } length = 23; /* >>{"Name":"","Value":""},<< */ length += 8; /* name */ length += 10; /* value */ ptr = (char *)rt_realloc(user_data, user_str_length + length + 1); if(ptr == NULL) { return -1; } user_data = ptr; ptr = user_data + user_str_length; length = rt_sprintf(ptr, "{"Name":"%s","Value":"%d"},", name, value); user_str_length += length; DEBUG_PRINTF(" append:%s ", ptr); return RT_EOK; } void leweiclient_init(const char * user_key, const char * gateway) { char *ptr = head; int head_length = 0; int tmp; user_data = NULL; user_str_length = 0; begin = RT_FALSE; end = RT_FALSE; // build head. tmp = rt_sprintf(ptr, "POST /api/V1/gateway/UpdateSensors/%s HTTP/1.1 ", gateway); head_length += tmp; ptr += tmp; // build userkey. tmp = rt_sprintf(ptr, "userkey: %s ", user_key); head_length += tmp; ptr += tmp; // build Host. tmp = rt_sprintf(ptr, "Host: www.lewei50.com "); head_length += tmp; ptr += tmp; // build User-Agent. tmp = rt_sprintf(ptr, "User-Agent: RT-Thread ART "); head_length += tmp; ptr += tmp; } int lewei_connect(void) { int sock; const char *url; struct hostent *host; struct sockaddr_in server_addr; url = server; host = gethostbyname(url); if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) { rt_kprintf("Socket() failed "); return -RT_ERROR; } server_addr.sin_family = AF_INET; server_addr.sin_port = htons(80); server_addr.sin_addr = *((struct in_addr *)host->h_addr); rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero)); if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) { rt_kprintf("Connect() failed "); closesocket(sock); return -RT_ERROR; } return sock; } int lewei_send(void) { int result = 0; int bytes, sock; char send_data_lewei[256]; char recv_data[128]; sock = lewei_connect(); bytes = send(sock, head, strlen(head), 0); rt_kprintf("%d bytes, %s ", bytes, head); rt_memset(send_data_lewei, ' ', sizeof(send_data_lewei)); rt_sprintf(send_data_lewei, "Content-Length: %d ", strlen(user_data) + 1); bytes = send(sock, send_data_lewei, strlen(send_data_lewei), 0); DEBUG_PRINTF("%d bytes, %s ", bytes, send_data_lewei); rt_memset(send_data_lewei, ' ', sizeof(send_data_lewei)); rt_sprintf(send_data_lewei, "Connection: close "); bytes = send(sock, send_data_lewei, strlen(send_data_lewei), 0); DEBUG_PRINTF("%d bytes, %s ", bytes, send_data_lewei);; rt_memset(send_data_lewei, ' ', sizeof(send_data_lewei)); rt_sprintf(send_data_lewei, " "); bytes = send(sock, send_data_lewei, strlen(send_data_lewei), 0); DEBUG_PRINTF("%d bytes, %s ", bytes, send_data_lewei); bytes = send(sock, user_data, strlen(user_data), 0); DEBUG_PRINTF("%d bytes, %s ", bytes, user_data); rt_memset(send_data_lewei, ' ', sizeof(send_data_lewei)); rt_sprintf(send_data_lewei, "]"); bytes = send(sock, send_data_lewei, strlen(send_data_lewei), 0); DEBUG_PRINTF("%d bytes, %s ", bytes, send_data_lewei); DEBUG_PRINTF("data:%s] ", user_data); DEBUG_PRINTF("Response from host: ********************************************************* "); while ((bytes = recv(sock, recv_data, sizeof(recv_data)-1, 0)) > 0) { DEBUG_PRINTF(recv_data); rt_memset(recv_data, ' ', sizeof(recv_data)); }; DEBUG_PRINTF(" ********************************************************* "); result = 0; send_exit: begin = RT_FALSE; end = RT_FALSE; if(closesocket(sock)<0) { rt_kprintf("Socket closed error! "); } if(user_data != NULL) { rt_free(user_data); } return result; } ALIGN(RT_ALIGN_SIZE) static rt_uint8_t lewei_updata_stack[1024]; static struct rt_thread lewei_updata_thread; static void lewei_updata_thread_entry(void* parameter) { unsigned char count=0; time_t now; leweiclient_init("XXXXXXXXXXXXXXXXXXXXXXX","01"); while (1) { rt_thread_delay( RT_TICK_PER_SECOND*10 ); /* sleep 10 seconds and switch to other thread */ time(&now); rt_kprintf("%s ", ctime(&now)); lewei_append("ETH_T",count++); lewei_send(); } } rt_err_t lewei_starup(void) { rt_err_t result; /* init lewei_updata thread */ result = rt_thread_init(&lewei_updata_thread, "leweiup", lewei_updata_thread_entry, RT_NULL, (rt_uint8_t*)&lewei_updata_stack[0], sizeof(lewei_updata_stack), 20, 5); if (result == RT_EOK) { rt_thread_startup(&lewei_updata_thread); } return result; } FINSH_FUNCTION_EXPORT(lewei_send, send data to www.lewei50.com) FINSH_FUNCTION_EXPORT(lewei_append, append data) FINSH_FUNCTION_EXPORT(lewei_starup, start lewei updata thread) #endif ```
查看更多
1
个回答
默认排序
按发布时间排序
撰写答案
登录
注册新账号
关注者
0
被浏览
4.5k
关于作者
eeyrw
这家伙很懒,什么也没写!
提问
2
回答
5
被采纳
0
关注TA
发私信
相关问题
1
有关动态模块加载的一篇论文
2
最近的调程序总结
3
晕掉了,这么久都不见layer2的踪影啊
4
继续K9ii的历程
5
[GUI相关] FreeType 2
6
[GUI相关]嵌入式系统中文输入法的设计
7
20081101 RT-Thread开发者聚会总结
8
嵌入式系统基础
9
linux2.4.19在at91rm9200 上的寄存器设置
10
[转]基于嵌入式Linux的通用触摸屏校准程序
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
最新文章
1
【NXP-MCXA153】 定时器驱动移植
2
GD32F450 看门狗驱动适配
3
【NXP-MCXA153】看门狗驱动移植
4
RT-Thread Studio V2.2.9 Release Note
5
CherryUSB的bootuf2配置
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
ESP8266
I2C_IIC
UART
WIZnet_W5500
ota在线升级
PWM
freemodbus
flash
cubemx
packages_软件包
BSP
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
中断
编译报错
Debug
rt_mq_消息队列_msg_queue
SFUD
msh
keil_MDK
ulog
C++_cpp
MicroPython
本月问答贡献
踩姑娘的小蘑菇
7
个答案
2
次被采纳
a1012112796
18
个答案
1
次被采纳
Ryan_CW
5
个答案
1
次被采纳
红枫
4
个答案
1
次被采纳
张世争
4
个答案
1
次被采纳
本月文章贡献
YZRD
3
篇文章
6
次点赞
catcatbing
3
篇文章
6
次点赞
lizimu
2
篇文章
9
次点赞
qq1078249029
2
篇文章
2
次点赞
xnosky
2
篇文章
1
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部