creating client socket, using connect(), invalid arguments?

Posted on 2004-11-14
Last Modified: 2010-08-05
Here's my client socket creation code:

void *createClientSocket(const char *hostname, const int port)
  struct Socket *new_socket;
      /* allocate memory for struct */
      new_socket = (struct Socket *)malloc(sizeof(struct Socket));

      printf("creating client socket\n");
      printf("client port: %d\n",port);
      /* creating client socket */
      new_socket->sockFD = socket(PF_INET,SOCK_STREAM,0);
      /* error checking */
      if (new_socket->sockFD < 0) {
            perror("error, socket creation\n");
            return NULL;

      /* setting up server */
      new_socket->remoteSockAddr.sin_family = PF_INET;
      new_socket->remoteSockAddr.sin_port = htons(port);
      new_socket->remoteSockAddr.sin_addr.s_addr = inet_addr(hostname);
      /* zero the struct */
      /* connect to server */
      if (connect(new_socket->sockFD,(struct sockaddr *)&new_socket->remoteSockAddr,
                  sizeof(struct sockaddr)) < 0) {
            perror("error, connect");
            return NULL;
  return new_socket;

I call this fn from main:

Everything seems to check out fine, connect seems to return an error, Invalid arguments.
remoteSockAddr is struct sockaddr_in.

Hmm i dont really see anything wrong, yet the error occurs.
Question by:kenbond
    LVL 5

    Expert Comment

    >> createClientSocket("localhost",port);   //wrong call


    you will also need a server listening on ur host for connect
    to be successful.

    hope this helps,

    Author Comment

    yea I was confused about that, like in waht format it needed to be in, DNS or ip?

    LVL 5

    Expert Comment

    in_addr_t inet_addr(const char *)

    the ip specified should be in dotted decimal notation.
    if you want to resolves via dns, you need to take a
    look into gethostbyname()

    Author Comment

    I'm still gettin "invalid arguments"
    LVL 5

    Accepted Solution


    what is this for? remove it , it should work fine then

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now