Hello,
I need to send a UDP package with this function( netUDP_Send )
https://www.keil.com/pack/doc/mw/Network/html/group__udp__user__api.html#ga7a82db98b82300e5fd03ce1b7afb6cba
But I don't have remote IP and Port number. I have added a new variable to get remote IP and Port number but it shows me an error.
NET_ADDR4 addrUDP;uint32_t udp_cb_func (int32_t socket, const NET_ADDR *addr, const uint8_t *buf, uint32_t len) { addrUDP=addr; return 0;}
How can I send a UDP package?
Parisa Mohamadi said:But I don't have remote IP and Port number
Obviously, those are essential - you can't send a packet if you don't know where it's going!
Parisa Mohamadi said:it shows me an error
What, exactly, "shows you an error" ?
And what error, exactly, does "it" show ?
I really appreciate your response.
I need to set my device IP Address to 192.168.1.100 and listen to 2050 port. Here is my code:
int main (void){ netInitialize (); Eth(); while(1){ Sendd(); osDelay(100); } } NET_ADDR4 addrUDP={ NET_ADDR_IP4, 5022, 192, 168, 1, 10 }; uint32_t udp_cb_func (int32_t socket, const NET_ADDR *addr, const uint8_t *buf, uint32_t len) { //addrUDP=addr; return 0; } void Eth(){ uint8_t mac_addr [NET_ADDR_ETH_LEN]; uint8_t ip4_addr [NET_ADDR_IP4_LEN]; netIP_aton ("192.168.1.100", NET_ADDR_IP4, ip4_addr); while (netARP_GetMAC (NET_IF_CLASS_ETH | 0, ip4_addr, mac_addr) != netOK) { netARP_CacheIP (NET_IF_CLASS_ETH | 0, ip4_addr, netARP_CacheTemporaryIP); osDelay (1000); } socket = netUDP_GetSocket(udp_cb_func); if (socket >= 0) { netUDP_Open(socket, 2050); } void Sendd(void){ sendbuf = netUDP_GetBuffer (Ether.DataSize); State=netUDP_Send (socket,(NET_ADDR *)&addrUDP, sendbuf,Ether.DataSize); if(State!=netOK){ netUDP_Close(socket); socket = netUDP_GetSocket(udp_cb_func); if (socket >= 0) { netUDP_Open(socket, 2050); netUDP_SetOption (socket, netUDP_OptionTTL, 2); } } }
After programming, I can ping my device( 192.168.1.100 is alive). But I need to do two different strategies:
1)Obtaining remote IP address without inserting in my code (192.168.1.10)
2)Send a UDP package.
I have set my device to 192.168.1.100 and it is activated in my network( it responds to ping command). But I cant received anything. Here is my code
int main (void){ netInitialize (); Eth(); while(1){ Sendd(); osDelay(100); } } NET_ADDR4 addrUDP={ NET_ADDR_IP4, 5022, 192, 168, 1, 10 }; uint32_t udp_cb_func (int32_t socket, const NET_ADDR *addr, const uint8_t *buf, uint32_t len) { //addrUDP=addr; return 0; } void Eth(){ uint8_t mac_addr [NET_ADDR_ETH_LEN]; uint8_t ip4_addr [NET_ADDR_IP4_LEN]; netIP_aton ("192.168.1.100", NET_ADDR_IP4, ip4_addr); while (netARP_GetMAC (NET_IF_CLASS_ETH | 0, ip4_addr, mac_addr) != netOK) { netARP_CacheIP (NET_IF_CLASS_ETH | 0, ip4_addr, netARP_CacheTemporaryIP); osDelay (1000); } socket = netUDP_GetSocket(udp_cb_func); if (socket >= 0) { netUDP_Open(socket, 2050); } void Sendd(void){ sprintf(buf,"H%d",1); sendbuf = netUDP_GetBuffer (2); State=netUDP_Send (socket,(NET_ADDR *)&addrUDP, buf,2); if(State!=netOK){ netUDP_Close(socket); socket = netUDP_GetSocket(udp_cb_func); if (socket >= 0) { netUDP_Open(socket, 2050); netUDP_SetOption (socket, netUDP_OptionTTL, 2); } } }
I need to do two work.
1) Obtaining remote IP address without inserting into my code (192.168.1.10)
2) Sending a simple UDP package.
So what's the error, that you're trying to set an array to a pointer? Perhaps you should be copying the content? Perhaps the pointer is dynamic/temporal in nature?
Thank you all for your cooperation.
After compiling the project:
NET_ADDR4 addrUDP={ NET_ADDR_IP4, 2050, 192, 168, 1, 10 }; uint32_t udp_cb_func (int32_t socket, const NET_ADDR *addr, const uint8_t *buf, uint32_t len) { addrUDP=addr; return 0; }
Here is the error:
Configuration.c(158): error: #513: a value of type "const NET_ADDR *" cannot be assigned to an entity of type "NET_ADDR4"
In addition for ping command it lost some of my packages which is annoying.
Parisa Mohamadi said:a value of type "const NET_ADDR *" cannot be assigned to an entity of type "NET_ADDR4"
This is basic 'C' programming - in order to assign one thing to another, they must have compatible types.
The error message clearly tells you that these two are not compatible.
So
...
Parisa Mohamadi said:for ping command it lost some of my packages
So you're going to have to find if that's due to your system, or issues in your network - or both ...
Thank you so much, I have changed my code to:
NET_ADDR addrUDP; uint32_t udp_cb_func (int32_t socket, NET_ADDR *addr, const uint8_t *buf, uint32_t len) { addrUDP.addr_type=*addr->addr_type; addrUDP.port=*addr->port; addrUDP.addr=*addr->addr; return 0; }
But I got this error:
error: #75: operand of "*" must be a pointer addrUDP.addr_type=*addr->addr_type;
In addition I have changed to:
addrUDP.addr_type=addr->addr_type; addrUDP.port=addr->port; addrUDP.addr=addr->addr;
But I got:
error: #137: expression must be a modifiable lvalue
I removed addrUDP.addr=addr->addr; but still it doesn't work. I just broadcast my UDP package to receive it in all network.
Whenever you get a message telling you that you must do something - do it!
That message tells you:
operand of "*" must be a pointer
So do that!
That's another message telling you something which you must do - so do it!
If you don't know what a "modifiable lvalue" is, look it up in your 'C' textbook (it is a standard 'C' term), or google it ...
Parisa Mohamadi said:I removed addrUDP.addr=addr->addr; but still it doesn't work
Of course it won't - we've already established that you need to have the address!
Hello, Here is my program which works fine for broadcast.
int main (void){ netInitialize (); Eth(); while(1){ Sendd(); osDelay(100); } } NET_ADDR4 addrUDP={ NET_ADDR_IP4, 2020, 192, 168, 255, 255 }; uint32_t udp_cb_func (int32_t socket, const NET_ADDR *addr, const uint8_t *buf, uint32_t len) { return 0; } void Eth(){ uint8_t mac_addr [NET_ADDR_ETH_LEN]; uint8_t ip4_addr [NET_ADDR_IP4_LEN]; socket = netUDP_GetSocket(udp_cb_func); if (socket >= 0) { netUDP_Open(socket, 2050); } void Sendd(void){ sendbuf = netUDP_GetBuffer (2); sendbuf[0]=65;sendbuf[0]=66; State=netUDP_Send (socket,(NET_ADDR *)&addrUDP, sendbuf,2); }
I received transmitted data in my PC. However, by specifying the remote IP(NET_ADDR4 addrUDP={ NET_ADDR_IP4, 2020, 192, 168, 100, 20 };) it doesn't work even it doesn't respond to ping command. I have added "netARP_CacheIP " function based on "http://www.keil.com/pack/doc/mw/Network/html/group__udp__user__api.html?_ga=2.246941447.2042184398.1570359907-552607041.1563512165" but it only helps me to ping the device properly. but it still doesn't send anything. Here is my code
int main (void){ netInitialize (); Eth(); while(1){ Sendd(); osDelay(100); } } NET_ADDR4 addrUDP={ NET_ADDR_IP4, 2020, 192, 168, 100, 20 }; uint32_t udp_cb_func (int32_t socket, const NET_ADDR *addr, const uint8_t *buf, uint32_t len) { return 0; } void Eth(){ uint8_t mac_addr [NET_ADDR_ETH_LEN]; uint8_t ip4_addr [NET_ADDR_IP4_LEN]; netIP_aton ("192.168.100.100", NET_ADDR_IP4, ip4_addr); while (netARP_GetMAC (NET_IF_CLASS_ETH | 0, ip4_addr, mac_addr) != netOK) { netARP_CacheIP (NET_IF_CLASS_ETH | 0, ip4_addr, netARP_CacheTemporaryIP); osDelay (1000); } socket = netUDP_GetSocket(udp_cb_func); if (socket >= 0) { netUDP_Open(socket, 2050); } void Sendd(void){ sendbuf = netUDP_GetBuffer (2); sendbuf[0]=65;sendbuf[0]=66; State=netUDP_Send (socket,(NET_ADDR *)&addrUDP, sendbuf,2); }
My computer address IP is 192.168.100.50 and both subnet mask is 255.255.0.0 . Ping shows me that "192.168.100.100" is available on my network but it doesn't send anything to the remote IP ( after setting broadcast IP 192.168.255.255 and removing
netIP_aton ("192.168.100.100", NET_ADDR_IP4, ip4_addr); while (netARP_GetMAC (NET_IF_CLASS_ETH | 0, ip4_addr, mac_addr) != netOK) { netARP_CacheIP (NET_IF_CLASS_ETH | 0, ip4_addr, netARP_CacheTemporaryIP); osDelay (1000); }
it sends a broadcast and complete message to my PC. what is my mistake?