Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6165987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:15:45+00:00 2026-05-23T22:15:45+00:00

Im sending data on network via sockets like this: (broadcast) void sendBroad(char *dstIP, char

  • 0

Im sending data on network via sockets like this: (broadcast)

    void sendBroad(char *dstIP, char *localIP, char *localMAC)
{
    int sock;                         /* Socket */
    struct sockaddr_in broadcastAddr; /* Broadcast address */
    int broadcastPermission;          /* Socket opt to set permission to broadcast */
    unsigned int dataLen;
    char data[100]={0};

    strcat(data, localIP);
    strcat(data, " ");
    strcat(data, localMAC);
    strcat(data, " ");


    /* Create socket for sending/receiving datagrams */
    if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
        perror("socket() failed");

    /* Set socket to allow broadcast */
    broadcastPermission = 1;
    if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (void *) &broadcastPermission, 
          sizeof(broadcastPermission)) < 0)
        perror("setsockopt() failed");

    /* Construct local address structure */
    memset(&broadcastAddr, 0, sizeof(broadcastAddr));   /* Zero out structure */
    broadcastAddr.sin_family = AF_INET;                 /* Internet address family */
    broadcastAddr.sin_addr.s_addr = inet_addr(dstIP);   /* Broadcast IP address */
    broadcastAddr.sin_port = htons(BroadcastPort);      /* Broadcast port */


    dataLen = strlen(data);
    int j;
    for (j=0; j<1; j++) /* 3krat a dost */
    {
         /* Broadcast localIP in datagram to clients */
         if (sendto(sock, data, dataLen, 0, (struct sockaddr *) 
               &broadcastAddr, sizeof(broadcastAddr)) != dataLen)
             perror("sendto() sent a different number of bytes than expected");
    }
    /* NOT REACHED */
}

but I always get some weird chars in the begining when receiving, like:

X.?192.168.....

When I try to send this data 6 times, just once I get data starting with 192…, other 5 strings starts with those weird chars. Any idea what is happening here?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:15:46+00:00Added an answer on May 23, 2026 at 10:15 pm

    char data[100]; is not initialized. Accordingly, you are concatenating to the end of some undefined garbage, not to the end of an empty string. This is obviously undefined behaviour as it is not guaranteed that a ‘\0’ appears anywhere within the reserved space (to say nothing of the fact that it’s just plain undefined behaviour and the compiler may actually do what it wishes if/when it detects this).

    char data[100] = {0}; should do the trick.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to send data using ssl for data network Security when sending data
I have been sending binary data between applications lots of times over TCP sockets
I am sending data to a local network-device. If the device is off, a
Suppose we're sending some data over the network ... it is my understanding that
I'm sending some packets of data across the network and they arrive in byte[]s,
Months ago I implemented a component which receives data via UDP-network, deserializes it via
i have a service which fetches some data from network.i am sending data download
I have an application in which I am sending network data over WiFI. Everything
When sending data over HTTPS, I know the content is encrypted, however I hear
They both seem to be sending data to the server inside the body, so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.