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

  • SEARCH
  • Home
  • 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 7769583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:11:44+00:00 2026-06-01T16:11:44+00:00

I am trying to send a buffer via UDP sockets in C but I

  • 0

I am trying to send a buffer via UDP sockets in C but I always get an invalid argument error in sendto. I just don’t find the error. Could anyone maybe help me. Thanks in advance.

Here’s my code:

/**/    void    IPCSend(char *pazClientAddress, int iClientPort, char *pazBuffer )
{
    int iSocket;
    /* */
    if ((iSocket = socket(AF_INET, SOCK_DGRAM, 0)) != -1)
    {
        int iSendToReturn;
        struct sockaddr_in sinServerAddress;
        struct hostent *pstHost;
        /* */
        pstHost = (struct hostent *)gethostbyname((char *)pazClientAddress);
        /* */
        sinServerAddress.sin_family = AF_INET;
        sinServerAddress.sin_addr = *((struct in_addr *)pstHost->h_addr);
        sinServerAddress.sin_port = iPortNumber;
        bzero(&(sinServerAddress.sin_zero),8);
        /* */
        fprintf(stdout,"sending '%s' to client '%s:%d'\n",pazBuffer,pazClientAddress,iClientPort);
        iSendToReturn = sendto(iSocket, pazBuffer, sizeof(pazBuffer), 0, (struct sockaddr *)&sinServerAddress, sizeof(sinServerAddress));
        /* */
        if(iSendToReturn != -1)
            fprintf(stdout,"%d bytes sent\n",iSendToReturn);
        else
            perror("SendTo");
        /* */
        close(iSocket);
    }
    else
        fprintf(stdout,"could not connect to server\n");
}
  • 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-06-01T16:11:45+00:00Added an answer on June 1, 2026 at 4:11 pm

    Firstly, you’re making the perennial error of novice C programmers: using sizeof to get the size of a pointer. The variable pazBuffer is a pointer, not an array, so the sizeof operator will not return the array length, which is what you want. Your IPCSend function needs to take in the length of pazBuffer as a separate parameter:

    void IPCSend(char *pazClientAddress, int iClientPort, char *pazBuffer, size_t len)
    

    As for the error you’re getting – it might be related to something else. Invalid argument means that one of the parameters you’re passing to sendto is somehow invalid. Since I see that you are properly initializing the socket descriptor, the problem might be that the send buffer is somehow not valid – possibly a null pointer or bad address. This means that the problem is not apparent from the code you posted, and is likely in some code that is calling IPCSend.

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

Sidebar

Related Questions

I'm trying to send messages generated by Google Protocol Buffer code via a simple
I am trying to log via the NLog MailTarget. It works just fine, but
I am trying to send over UDP using the following code, but i'm getting
I`m trying to send e-mail via c# and use the following code: public static
I'm trying to send an email in Java but when I read the body
I am trying to send an email from a site I am building, but
I'm trying to send a UDP datagram (containing a protocol buffers message) and getting
I have a UDP server that I have being trying to send structures using
Im trying to send images over HTTP using HttpListener but some images come from
I'm trying to send a HTTP request to a servers interface. But I'm really

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.