kenbond
asked on
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_famil y = 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->remote SockAddr,0 ,sizeof(ne w_socket-> remoteSock Addr));
/* connect to server */
if (connect(new_socket->sockF D,(struct sockaddr *)&new_socket->remoteSockA ddr,
sizeof(struct sockaddr)) < 0) {
perror("error, connect");
return NULL;
}
return new_socket;
}
I call this fn from main:
createClientSocket("localh ost",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.
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
/* error checking */
if (new_socket->sockFD < 0) {
perror("error, socket creation\n");
return NULL;
}
/* setting up server */
new_socket->remoteSockAddr
new_socket->remoteSockAddr
new_socket->remoteSockAddr
/* zero the struct */
memset(&new_socket->remote
/* connect to server */
if (connect(new_socket->sockF
sizeof(struct sockaddr)) < 0) {
perror("error, connect");
return NULL;
}
return new_socket;
}
I call this fn from main:
createClientSocket("localh
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.
ASKER
yea I was confused about that, like in waht format it needed to be in, DNS or ip?
thanks
thanks
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()
the ip specified should be in dotted decimal notation.
if you want to resolves via dns, you need to take a
look into gethostbyname()
ASKER
I'm still gettin "invalid arguments"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
createClientSocket("127.0.
you will also need a server listening on ur host for connect
to be successful.
hope this helps,
van_dy