大学生
最后登录1970-1-1
在线时间 小时
注册时间2019-11-6
|
20火花
自己移植了ucosiii 加 lwip 2.1.2,这几天测试,代码做客户端发送数据给上位机,上位机接收的数据很多都是断句,一句正常一句断的那种,测试还字发送了200字节,不知道有什么问题,希望各路大神帮忙瞧上一眼。
先上soket发送这部分代码
- #include "tcp_server_demo.h"
- #include "lwip/opt.h"
- #include "lwip_comm.h"
- #include "led.h"
- #include "lwip/lwip_sys.h"
- #include "lwip/api.h"
- #include <lwip/sockets.h>
- u8 tcp_server_recvbuf[TCP_SERVER_RX_BUFSIZE]; //TCP客户端接收数据缓冲区
- u8 tcp_server_sendbuf[]="0123456789abcdefghijklmnopqrstuvwxyz,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,YZ9876543210\r\n\
- 0123456789abcdefghijklmnopqrstuvwxyz,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,YZ9876543210 \r\n";
- u8 tcp_server_flag; //TCP服务器数据发送标志位
- const char tcp_server_addr[] = "192.168.1.124";
- //tcp服务器任务
- void tcp_server_thread(void *arg)
- {
- OS_ERR err;
- int sock_fd = -1;
- u32 num = 0;
- int error = -1,len_t = 0;
- struct sockaddr_in client_addr;
-
- OSTimeDlyHMSM(0,0,1,0,OS_OPT_TIME_HMSM_STRICT,&err);
- while(1)
- {
- //创建tcp套接字
- sock_fd = socket(AF_INET, SOCK_STREAM, 0);
- if(sock_fd < 0)
- {
- printf("Failed to request Socket memory!\r\n");
- OSTimeDlyHMSM(0,0,1,0,OS_OPT_TIME_HMSM_STRICT,&err);
- continue;
- }
-
- //设置socket 为 Non-blocking模式
- int flags = fcntl(sock_fd, F_GETFL, 0);
- fcntl(sock_fd, F_SETFL, flags|O_NONBLOCK);
-
- client_addr.sin_family = AF_INET;
- client_addr.sin_port = htons(TCP_SERVER_PORT);
- client_addr.sin_addr.s_addr = inet_addr(tcp_server_addr);
- memset(&(client_addr.sin_zero), 0, sizeof(client_addr.sin_zero));
-
- // connect 连接服务器,在Non-blocking模式下立即返回
- if(connect(sock_fd, (struct sockaddr *)&client_addr, sizeof(struct sockaddr)) < 0)
- {
- if(errno != EINPROGRESS)
- {
- printf("connect failed! len = %d, num = %d\r\n",sizeof(tcp_server_sendbuf), num);
- closesocket(sock_fd);
- OSTimeDlyHMSM(0,0,1,0,OS_OPT_TIME_HMSM_STRICT,&err);//1s 重连一次
- continue;
- }
- else
- {
- fd_set rfds, wfds;
- struct timeval tv;
- FD_ZERO(&rfds);
- FD_ZERO(&wfds);
- FD_SET(sock_fd, &rfds);
- FD_SET(sock_fd, &wfds);
- /* set select() time out */
- tv.tv_sec = 10;
- tv.tv_usec = 0;
-
- if(select(sock_fd + 1, &rfds, &wfds, NULL, &tv) > 0)
- {
- if(FD_ISSET(sock_fd, &rfds) && FD_ISSET(sock_fd, &wfds))
- {
- socklen_t len = sizeof(error);
- if(getsockopt(sock_fd, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len) < 0)
- {
- printf("getsockopt error!!!\r\n");
- printf("3connect failed! len = %d, num = %d\r\n",sizeof(tcp_server_sendbuf), num);
- closesocket(sock_fd);
- OSTimeDlyHMSM(0,0,1,0,OS_OPT_TIME_HMSM_STRICT,&err);//1s 重连一次
- continue;
- }
- else
- {
- if(error == 0)
- {
- //printf(" OOOOOOOOOOOKKKKKKKKKKKKKKKKK\r\n");
- }
- else
- {
- printf("connect getsockopt error!!!\n");
- printf("3connect failed! len = %d, num = %d\r\n",sizeof(tcp_server_sendbuf), num);
- closesocket(sock_fd);
- OSTimeDlyHMSM(0,0,1,0,OS_OPT_TIME_HMSM_STRICT,&err);//1s 重连一次
- continue;
- }
- }
- }
- else if(!FD_ISSET(sock_fd, &rfds) && FD_ISSET(sock_fd, &wfds))
- {
- //printf(" 2OOOOOOOOOOOKKKKKKKKKKKKKKKKK\r\n");
- }
- }
- else
- {
- printf("3connect failed! len = %d, num = %d\r\n",sizeof(tcp_server_sendbuf), num);
- closesocket(sock_fd);
- OSTimeDlyHMSM(0,0,1,0,OS_OPT_TIME_HMSM_STRICT,&err);//1s 重连一次
- continue;
- }
- }
- }
-
- //fcntl(sock_fd, F_SETFL, flags&~O_NONBLOCK);
- printf("Connect to server: %s successful!\r\n", tcp_server_addr);
- while(1)
- {
- //接收数据
- len_t = read(sock_fd, tcp_server_recvbuf, TCP_SERVER_RX_BUFSIZE);
- if(len_t < 0)
- {
- //printf("socket read error!\r\n");
- }
- else
- {
- printf("socket read len = %d ,buf = %s\r\n", len_t, tcp_server_recvbuf);
- }
-
- num++;
- //发送数据
- if(write(sock_fd, tcp_server_sendbuf, sizeof(tcp_server_sendbuf)) < 0)
- {
- if(errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN)
- {
- //printf();
- }
- else
- {
- printf("socket send data error!\r\n");
- break;
- }
- }
- }
-
- closesocket(sock_fd);
- }
- }
复制代码
然后配置了几个发送有关的参数,如下截图
最后是上位机接收的数据,很多都丢失了,
以下是我的工程,希望有大神指点一下,谢谢谢谢
ucosiii移植lwip2.1.2.zip
(5.51 MB, 下载次数: 4)
|
|