the story of a socket connect and a char array :-)

K

Kifah Abbad

This part finds the destination IP in a packet, and converts into a
string.



code:--------------------------------------------------------------------------------
char destination_IP[40]; //For saving destination IP
..
..
..
..
..


//save destination IP


sprintf(&Destination_IP_temp,"%d.%d.%d.%d",(vp->packet[30] &
0xff),(vp->packet[31] & 0xff),(vp->packet[32] & 0xff),(vp->packet[33]
& 0xff));
--------------------------------------------------------------------------------



Then i have a part for socket connection, that uses this IP in a
connect part:


code:--------------------------------------------------------------------------------
..
..
..
..
their_addr.sin_addr.s_addr = inet_addr(destination_IP);
memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the
struct

if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct
sockaddr)) == -1) {
perror("connect");
exit(1);
}
--------------------------------------------------------------------------------


But when i start proggie, it keeps exiting with error:


code:--------------------------------------------------------------------------------
connect: Invalid argument
--------------------------------------------------------------------------------


Although the ip gets printed out correctly when i try to debug with:

code:--------------------------------------------------------------------------------
printf("Destination %d.%d.%d.%d\n",
(vp->packet[30] & 0xff),
(vp->packet[31] & 0xff),
(vp->packet[32] & 0xff),
(vp->packet[33] & 0xff));
 
T

Tom St Denis

Kifah Abbad said:
This part finds the destination IP in a packet, and converts into a
string.

<snip messy post>

You have to create a socket first [via socket()] before you can connect [you
connect the socket you made to the other end].

And this is OT for clc...

Tom
 
S

Sean Kenwrick

Kifah Abbad said:
This part finds the destination IP in a packet, and converts into a
string.



code:-----------------------------------------------------------------------
---------
char destination_IP[40]; file://For saving destination IP
.
.
.
.
.


file://save destination IP


sprintf(&Destination_IP_temp,"%d.%d.%d.%d",(vp->packet[30] &
0xff),(vp->packet[31] & 0xff),(vp->packet[32] & 0xff),(vp->packet[33]
& 0xff));
-------------------------------------------------------------------------- ------
This looks suspicious. sprintf() tasks a pointer to a char as it's first
parameter not a pointer to a pointer to a char. Try
sprintf(Destination_IP_temp,"%d.%d....etc...);

Sean
 
K

Kifah Abbad

Tom St Denis said:
Kifah Abbad said:
This part finds the destination IP in a packet, and converts into a
string.

<snip messy post>

You have to create a socket first [via socket()] before you can connect [you
connect the socket you made to the other end].

And this is OT for clc...


Thanks,

i did...i just did not post here

And i should add, that the programm worked fine with something like:


code:--------------------------------------------------------------------------------
their_addr.sin_addr.s_addr = inet_addr("10.10.10.11");
memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the
struct

--------------------------------------------------------------------------------
 
A

Alan Balmer

Tom St Denis said:
Kifah Abbad said:
This part finds the destination IP in a packet, and converts into a
string.

<snip messy post>

You have to create a socket first [via socket()] before you can connect [you
connect the socket you made to the other end].

And this is OT for clc...


Thanks,

i did...i just did not post here

And i should add, that the programm worked fine with something like:

No, you shouldn't add that - it's still off-topic for c.l.c.

The C question in your original post seems to be why something you
call a "proggie" prints an "invalid argument" complaint about a
non-standard function named "connect."

1. Check that the argument types match the types specified in the
function prototype.

2. Check that the values of the arguments are correct.

If this doesn't help, try posting a question to a newsgroup where
"connect" is on-topic.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top