• C

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

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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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


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

hope this helps,
kenbondAuthor Commented:
yea I was confused about that, like in waht format it needed to be in, DNS or ip?

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()
kenbondAuthor Commented:
I'm still gettin "invalid arguments"

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.