# UDP BROADCAST problem with NetOS6.3 using C

**URL:** <https://forums.digi.com/t/udp-broadcast-problem-with-netos6-3-using-c/4781>\
**Category:** NET+OS\
**Created:** [August 16, 2011, 10:12am UTC](https://forums.digi.com/t/udp-broadcast-problem-with-netos6-3-using-c/4781 "2011-08-16T10:12:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kotireddy](https://avatars.discourse-cdn.com/v4/letter/k/e36b37/32.png) [@kotireddy](https://forums.digi.com/u/kotireddy)\
**Post date:** [August 16, 2011, 10:12am UTC](https://forums.digi.com/t/udp-broadcast-problem-with-netos6-3-using-c/4781/1 "2011-08-16T10:12:29Z")

</div>

hi all,  
i am using ns9360 board and NetOS6.3 version for writing an application in C for data exchange between ns9360 board and a PC with VB application  
Here i am able to create a socket and bind it and i am getting the status for sendto command as 4 bytes i.e i am sending “5555”…but the problem is on VB side i could n’t get the data.Nothing happening in VB side.I am getting correct return type for sendto command but still why i am not getting the data on VB side at the same time whenever i tried to send the data fron VB to NetOS board also data not receiving in Board side.  
I am giving the source code at the bottom i have written for the reference…  
what could be the problem pls any one can suggest me on this …or mail me at kotireddy@bhelrnd.co.in

thanks&regards  
Kotireddy

UDP code on NetOS6.3 in C is:  
int sFd,idd,bytes;  
struct sockaddr\_in serverAddr; /\* server’s socket address _/  
struct sockaddr\_in clientAddr; /_ client’s socket address _/  
int sockAddrSize; /_ size of socket address structure \*/  
char ack[1024]=“5555”;

sFd=socket(AF\_INET,SOCK\_DGRAM,0);  
if (sFd\<0)  
printf("  
socket creation failed  
“);  
else  
printf(”  
socket creation success  
");  
serverAddr.sin\_family = AF\_INET;  
serverAddr.sin\_port = htons (6000);  
serverAddr.sin\_addr.s\_addr = htonl (INADDR\_ANY);  
idd=bind(sFd,&serverAddr,sizeof(serverAddr));

```
if (idd==0)
	printf(" udp bind success 

```

“);  
else if (idd==-1)  
printf(”  
udp bind failure  
“);  
clientAddr.sin\_family = AF\_INET;  
clientAddr.sin\_port = htons (6000);  
clientAddr.sin\_addr.s\_addr = htonl(“192.168.12.33”);  
sockAddrSize=sizeof(clientAddr);  
while(1)  
{  
bytes = sendto (sFd, ack, strlen (ack), 0, &clientAddr, sizeof(clientAddr));  
if (bytes==-1)  
{  
printf(”  
ERROR in Send :%s",ack);  
}  
else  
{  
printf("  
%s %d",ack,bytes);  
}  
}

my VB side code on a PC with XP-sp2 is:

WINSOCK1 object settings on VB form are:protocol is UDPPROTOCOL  
remotehostip:192.168.12.33  
remoteport:6000  
localport:6000

Private Sub Form\_Load()  
With Winsock1  
.RemotePort = 6000 ’ Port to connect to.  
.Bind 6000 ’ Bind to the local port.  
End With  
end sub

Private Sub Winsock1\_DataArrival(ByVal bytesTotal As Long)  
Dim NewLine As String  
Winsock1.GetData str  
end sub

---

<div class="post-metadata">

**Author:** ![bobthomas13](https://avatars.discourse-cdn.com/v4/letter/b/958977/32.png) [@bobthomas13](https://forums.digi.com/u/bobthomas13)\
**Post date:** [August 17, 2011, 11:04am UTC](https://forums.digi.com/t/udp-broadcast-problem-with-netos6-3-using-c/4781/2 "2011-08-17T11:04:09Z")

</div>

i guess you should edit like below:-

//struct sockaddr\_in serverAddr; /\* server’s socket address _/  
//struct sockaddr\_in clientAddr; /_ client’s socket address \*/

struct sockaddr\_storage serverAddr;  
struct sockaddr\_storage clientAddr;

.  
.  
.  
.

-\>serverAddr.sin\_addr.s\_addr = htonl (INADDR\_ANY);  
use inet\_pton() and only “serverAddr.sin\_addr” is enough.

use memset(&clientAddr, 0, sizeof(clientAddr)); also…

in bind  
-\>\>idd=bind(sFd,&serverAddr,sizeof(serverAddr));  
replace like below  
idd=bind(sFd,(struct sockaddr \*) &serverAddr,sizeof(serverAddr)) ;

sendto and recvfrom also should contain

-\>\>sendto (sFd, ack, strlen (ack), 0, (struct sockaddr\*)&clientAddr, sizeof(clientAddr));

I do not have 6.3, i have 7.5… so not sure whether inet\_pton works or not.  
Try this out…

and VB…he he sorry… No idea 🙂

---

<div class="post-metadata">

**Author:** ![kotireddy](https://avatars.discourse-cdn.com/v4/letter/k/e36b37/32.png) [@kotireddy](https://forums.digi.com/u/kotireddy)\
**Post date:** [August 18, 2011, 10:42am UTC](https://forums.digi.com/t/udp-broadcast-problem-with-netos6-3-using-c/4781/3 "2011-08-18T10:42:18Z")

</div>

Thank u bob for the reply,

I tried even this but no result.  
It is not at all taking (struct sockaddr\*) in send to command.It is giving error that incompatable argument number 5 in sendto function.  
sendto (sFd, ack, strlen (ack), 0, (struct sockaddr\*)&clientAddr, sizeof(clientAddr)); /\*_giving error/  
sendto (sFd, ack, strlen (ack), 0, (struct sockaddr\_in_)&clientAddr, sizeof(clientAddr)); /\*\*_is working and acknowledge ment also giving correct_/

But today i observed the main problem as: i set client address as 192.168.12.35 and my NetOS board address (server)as 192.168.12.30 in my NetOS board side code and given sendto command.I am getting acknowledge correctly for sendto command as 6 for the data “ffffff”.  
But when i observed the packets on the network through WIRESHARK from my client PC i observed ARP protocol with target address as 0.7.226.168 (though i set 192.168.12.35) and underlying protocol is UDP).how come this 0.7.226.168 came as my target address and is keeps changing.
