|
@@ -47,6 +47,7 @@ OF SUCH DAMAGE.
|
|
|
#include "main.h"
|
|
|
#include "lwip/api.h"
|
|
|
|
|
|
+
|
|
|
#define UDP_TASK_PRIO ( tskIDLE_PRIORITY + 5)
|
|
|
#define MAX_BUF_SIZE 50
|
|
|
|
|
@@ -158,56 +159,40 @@ static void udp_task(void *arg)
|
|
|
\param[out] none
|
|
|
\retval none
|
|
|
*/
|
|
|
-static void udp_task(void *arg)
|
|
|
+volatile int8_t sockfd=-1;
|
|
|
+#define UDP_LOCAL_PORT 12345
|
|
|
+#define UDP_REMOTE_PORT 54321
|
|
|
+#define SERCER_IP_ADDRESS "192.168.2.22" //接收服务器ip
|
|
|
+struct sockaddr_in remote_addr;
|
|
|
+static int udp()
|
|
|
{
|
|
|
- int ret, recvnum, sockfd = -1;
|
|
|
- int udp_port = 1025;
|
|
|
- struct sockaddr_in rmt_addr, bod_addr;
|
|
|
- char buf[100];
|
|
|
- u32_t len;
|
|
|
- struct ip_addr ipaddr;
|
|
|
+ int ret;
|
|
|
+ struct sockaddr_in local_addr;
|
|
|
+ // 创建UDP套接字
|
|
|
+ sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
+ if (sockfd < -1) {
|
|
|
+ perror("Error creating socket");
|
|
|
+ goto exit;
|
|
|
+ }
|
|
|
+ local_addr.sin_family=AF_INET;
|
|
|
+ local_addr.sin_port=htons(UDP_LOCAL_PORT);
|
|
|
+ local_addr.sin_addr.s_addr=INADDR_ANY;
|
|
|
+ ret = bind(sockfd, (struct sockaddr *)&local_addr, sizeof(local_addr));
|
|
|
+ if(ret < 0)
|
|
|
+ {
|
|
|
+ lwip_close(sockfd);
|
|
|
+ sockfd = -1;
|
|
|
+ goto exit;
|
|
|
+ }
|
|
|
+ //设置远程服务器地址 TODO:服务器地址配置后自动获取
|
|
|
+ remote_addr.sin_family=AF_INET;
|
|
|
+ remote_addr.sin_port=htons(UDP_REMOTE_PORT);
|
|
|
+ inet_aton(SERCER_IP_ADDRESS,&(remote_addr.sin_addr));
|
|
|
+exit:
|
|
|
+ return -1;
|
|
|
+}
|
|
|
|
|
|
- IP4_ADDR(&ipaddr, IP_S_ADDR0, IP_S_ADDR1, IP_S_ADDR2, IP_S_ADDR3);
|
|
|
|
|
|
- /* set up address to connect to */
|
|
|
- rmt_addr.sin_family = AF_INET;
|
|
|
- rmt_addr.sin_port = htons(udp_port);
|
|
|
- rmt_addr.sin_addr.s_addr = ipaddr.addr;
|
|
|
-
|
|
|
- bod_addr.sin_family = AF_INET;
|
|
|
- bod_addr.sin_port = htons(udp_port);
|
|
|
- bod_addr.sin_addr.s_addr = htons(INADDR_ANY);
|
|
|
-
|
|
|
- while (1){
|
|
|
- /* create the socket */
|
|
|
- sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
- if(sockfd < 0){
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- ret = bind(sockfd, (struct sockaddr *)&bod_addr, sizeof(bod_addr));
|
|
|
-
|
|
|
- if(ret < 0){
|
|
|
- lwip_close(sockfd);
|
|
|
- sockfd = -1;
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- len = sizeof(rmt_addr);
|
|
|
- /* reveive packets from rmt_addr, and limit a reception to MAX_BUF_SIZE bytes */
|
|
|
- recvnum = recvfrom(sockfd, buf, MAX_BUF_SIZE, 0, (struct sockaddr *)&rmt_addr, &len);
|
|
|
-
|
|
|
- while(-1 != sockfd){
|
|
|
- if(recvnum > 0){
|
|
|
- /* send packets to rmt_addr */
|
|
|
- sendto(sockfd, buf, recvnum, 0, (struct sockaddr *)&rmt_addr, sizeof(rmt_addr));
|
|
|
- }
|
|
|
- recvnum = recvfrom(sockfd, buf, MAX_BUF_SIZE, 0, (struct sockaddr *)&rmt_addr, &len);
|
|
|
- }
|
|
|
-
|
|
|
- lwip_close(sockfd);
|
|
|
- }
|
|
|
-}
|
|
|
|
|
|
#endif /* LWIP_SOCKET */
|
|
|
|
|
@@ -220,5 +205,17 @@ static void udp_task(void *arg)
|
|
|
*/
|
|
|
void udp_echo_init(void)
|
|
|
{
|
|
|
- xTaskCreate(udp_task, "UDP", DEFAULT_THREAD_STACKSIZE, NULL, UDP_TASK_PRIO, NULL);
|
|
|
+ //xTaskCreate(udp, "UDP", DEFAULT_THREAD_STACKSIZE, NULL, UDP_TASK_PRIO, NULL);
|
|
|
+ udp();
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+int fputc(int ch, FILE *f)
|
|
|
+{
|
|
|
+ if(sockfd>=0)
|
|
|
+ {
|
|
|
+ sendto(sockfd, &ch, 1, 0, (struct sockaddr *)&remote_addr,sizeof(remote_addr));
|
|
|
+ return ch;
|
|
|
+ }
|
|
|
}
|