• 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 */
      memset(&new_socket->remoteSockAddr,0,sizeof(new_socket->remoteSockAddr));
      
      /* 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:
createClientSocket("localhost",port);

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.
kenbondAsked:
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.

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

createClientSocket("127.0.0.1",port);

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

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

thanks
0
van_dyCommented:
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()
0
kenbondAuthor Commented:
I'm still gettin "invalid arguments"
0
van_dyCommented:
>>memset(&new_socket->remoteSockAddr,0,sizeof(new_socket->remoteSockAddr));

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

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
C

From novice to tech pro — start learning today.