You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.8 KiB

  1. #include "stm32f4xx_hal.h"
  2. #include "lwip.h"
  3. #include "udp.h"
  4. #include "string.h"
  5. #include "udpclient.h"
  6. #include "socket.h"
  7. #include "sockets.h"
  8. #include "def.h"
  9. /* 定义端口号 */
  10. #define UDP_REMOTE_PORT 8881 /* 远端端口 */
  11. #define UDP_LOCAL_PORT 8880 /* 本地端口 */
  12. #define SERVER_FAMILY AF_INET
  13. #define SOCKET_ERROR -1
  14. #define BUFFER_SIZE 256 // 缓冲区大小
  15. static char s_sendBuf[BUFFER_SIZE]; // 发送数据的缓冲区
  16. static char s_receBuf[BUFFER_SIZE]; // 接收数据的缓冲区
  17. SOCKET sock_Client = 0; // 客户端用于通信的Socket
  18. static struct sockaddr_in addr_server;
  19. static struct sockaddr_in addr_client;
  20. static struct sockaddr_in sock;
  21. socklen_t sock_len = sizeof(sock);
  22. void config_server(struct sockaddr_in *addr_server)
  23. {
  24. addr_server->sin_family = SERVER_FAMILY;
  25. addr_server->sin_port = htons(UDP_REMOTE_PORT);
  26. addr_server->sin_addr.s_addr = htonl(INADDR_BROADCAST);
  27. }
  28. void config_client(struct sockaddr_in *addr_client)
  29. {
  30. addr_client->sin_family = AF_INET;
  31. addr_client->sin_addr.s_addr = inet_addr("0.0.0.0");
  32. addr_client->sin_port = htons(UDP_LOCAL_PORT);
  33. }
  34. void udp_client_send_string(char *pData)
  35. {
  36. memcpy(s_sendBuf, pData, strlen(pData));
  37. if (sendto(sock_Client, s_sendBuf, strlen(pData), 0, (struct sockaddr *)&addr_server, sizeof(struct sockaddr_in)) == SOCKET_ERROR)
  38. {
  39. printf("send string error\n");
  40. }
  41. }
  42. void udp_client_init(void)
  43. {
  44. // bool bOpt = true;
  45. struct timeval tv = {0, 100000};
  46. config_server(&addr_server);
  47. config_client(&addr_client);
  48. // 创建客户端用于通信的Socket
  49. sock_Client = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  50. if (sock_Client == SOCKET_ERROR)
  51. {
  52. printf("create socket error...\n");
  53. }
  54. else
  55. {
  56. printf("create socket success!\n");
  57. }
  58. // /* 开启广播 */
  59. // if (lwip_setsockopt(sock_Client, SOL_SOCKET, SO_BROADCAST, (char *)&bOpt, sizeof(bOpt)) == SOCKET_ERROR)
  60. // {
  61. // printf("enable broadcast error...\n");
  62. // }
  63. // else
  64. // {
  65. // printf("enable broadcast success!\n");
  66. // }
  67. /* 设置超时 */
  68. if (lwip_setsockopt(sock_Client, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)) == SOCKET_ERROR)
  69. {
  70. printf("enable receive timeout error...\n");
  71. }
  72. else
  73. {
  74. printf("enable receive timeout success!\n");
  75. }
  76. /* 绑定 */
  77. if (bind(sock_Client, (struct sockaddr *)&addr_client, sizeof(addr_client)) == SOCKET_ERROR)
  78. {
  79. printf("Bind failed");
  80. return;
  81. }
  82. }
  83. void udp_client_recv(void)
  84. {
  85. int recv_datalen = recvfrom(sock_Client, s_receBuf, sizeof(s_receBuf), 0, (struct sockaddr *)&sock, &sock_len);
  86. if (recv_datalen > 0)
  87. {
  88. udp_client_send_string(s_receBuf);
  89. printf("datalen:%d,data:%s\r\n", recv_datalen, s_receBuf);
  90. }
  91. }