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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
van_dyConnect With a Mentor Commented:
>>memset(&new_socket->remoteSockAddr,0,sizeof(new_socket->remoteSockAddr));

what is this for? remove it , it should work fine then
0
 
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
All Courses

From novice to tech pro — start learning today.