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 6129891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:47:42+00:00 2026-05-23T16:47:42+00:00

I keep getting an invalid argument error when I try to connect the client

  • 0

I keep getting an invalid argument error when I try to connect the client to the server. A couple threads online said this can happen when addrlen is not right, but I tried changing it to a literal value after counting the length and that did not work. I also tried just strlen() with no luck. Anyways, relevant code –

server –

struct sockaddr name;
int main(int agrc, char** argv) {

    int sock, new_sd, adrlen;   //sock is this socket, new_sd is connection socket

    name.sa_family = AF_INET;
    strcpy(name.sa_data, "127.0.0.1");
    adrlen = strlen(name.sa_data) + sizeof(name.sa_family);

    //make socket
    sock = socket(AF_INET, SOCK_STREAM, 0);

    if (sock < 0) {
        printf("\nBind error %m", errno);
        exit(1);
    }  

    //unlink and bind
    unlink("127.0.0.1");
    if(bind (sock, &name, adrlen) < 0)
        printf("\nBind error %m", errno);

    //listen
    if(listen(sock, 5) < 0)
        printf("\nListen error %m", errno);

    //accept
    new_sd = accept(sock, &name, (socklen_t*)&adrlen);
    if( new_sd < 0) {
        printf("\nAccept error %m", errno);
        exit(1);
    }

client –

int main(int agrc, char** argv) {

    int sock, new_sd, adrlen;

    sock = socket(AF_INET, SOCK_STREAM, 0);

    if (sock < 0) {
        printf("\nserver socket failure %m", errno);
        exit(1);
    }

    //stuff for server socket
    name.sa_family = AF_INET;
    strcpy(name.sa_data, "127.0.0.1");
    adrlen = strlen(name.sa_data) + sizeof(name.sa_family);

    cout<<"\nadrlen: "<<adrlen<<"\n";
    if(connect(sock, &name, adrlen) < 0) {
        printf("\nclient connection failure %m", errno);
        exit(1);
    }

I don’t see anything that could be wrong, but I guess I might just be overlooking something or unaware of something. Any help is appreciated.

  • 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-23T16:47:42+00:00Added an answer on May 23, 2026 at 4:47 pm

    Well your big problem is completely misunderstanding the sockaddr struct!

    Firstly use sockaddr_in.

    You would then write code more like the following:

    sockaddr_in sai;
    sai.sin_family = AF_INET;
    sai.sin_port   = htons( 12345 );  /// Or whatever port you wish to use.
    inet_aton("127.0.0.1", &sai.sin_addr.s_addr);
    

    Note: htons converts a short value from host to network format. Host could be big or little endian. Network is big endian.

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

Sidebar

Related Questions

when I launch my app, I keep getting this CGContextFillEllipseInRect: invalid context error. My
I keep getting this error: Invalid Signature - This error occurs when you have
Can someone please explain to me why I keep getting this PLS-00905 error for
Keep getting this error after inserting a subdatasheet into a query and trying to
I keep getting tasks that are above my skill level. How can I address this without coming accross as grossly incompetent?
I keep getting a PermGen error on my Tomcat 6 server. I know what
I keep getting this NPE in my application and I can't seem to get
I keep getting this : DeprecationWarning: integer argument expected, got float How do I
I keep getting this error: cannot convert parameter 1 from 'char' to 'LPCWSTR' int
I keep getting a invalid identifier exception when I try to run the below

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.