Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
09-10-2010, 09:55 PM
|
#1
|
LQ Newbie
Registered: Sep 2010
Posts: 5
Rep:
|
sendto() returning -1 - Invalid argument
Hello,
I am trying to write a program that sends a UDP packet over the network. When I am using the sendto function it is returning with -1 and errno "Invalid argument". I am using the cast to make sure arg 5 is being passed in as a pointer to a sockaddr struct.
I have looked at lots of examples and my code seems to follow what the examples have so I am not sure what is wring here. I am a newbie so any pointers would be great!
STATUS send_request(const packet_t * pkt)
{
int fd = -1; /* socket filedescriptor */
int result = 0; /* sendto status */
struct sockaddr_in sa; /* structure for handling internet addressing */
/* create UDP socket PF_PACKET for post 2.0 kernel */
fd = socket(PF_PACKET, SOCK_DGRAM, IPPROTO_UDP);
if (-1 == fd)
{
/* error case */
printf ("%s:%d socket failed %s", __FUNCTION__, __LINE__, strerror(errno) );
return FAILED;
}
/* setup sockaddr_in struct */
memset((char *) &sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = htons(SEND_PORT);
/* if (0 == inet_aton(SVR_IP, &sa.sin_addr))
{
// error case
printf ("%s:%d inet_aton failed", __FUNCTION__, __LINE__ );
if (-1 == (close(fd)))
{
printf ("%s:%d close failed %s", __FUNCTION__, __LINE__, strerror(errno) );
}
return FAILED;
}
*/
/* send packet */
printf ("%s:%d sendto fd:%d sizeof(packet_t):%d sizeof(pkt):%d", __FUNCTION__, __LINE__,fd, sizeof(packet_t), sizeof(pkt));
result = sendto(fd, pkt, sizeof(packet_t), 0, (struct sockaddr *)&sa, sizeof(sa));
if(-1 == result)
{
/* error case */
printf ("%s:%d sendto failed %d : %s", __FUNCTION__, __LINE__,result, strerror(errno) );
if (-1 == (close(fd)))
{
printf ("%s:%d close failed %s", __FUNCTION__, __LINE__, strerror(errno) );
}
return FAILED;
}
else if (sizeof(packet_t) == result)
{
/* success case */
printf ("%s:%d sendto successful", __FUNCTION__, __LINE__ );
if (-1 == (close(fd)))
{
printf ("%s:%d close failed %s", __FUNCTION__, __LINE__, strerror(errno) );
return FAILED;
}
}
else
{
/* catch all */
printf ("%s:%d sendto %d : %s", __FUNCTION__, __LINE__,result, strerror(errno) );
if (-1 == (close(fd)))
{
printf ("%s:%d close failed %s", __FUNCTION__, __LINE__, strerror(errno) );
}
return FAILED;
}
return SUCCESS;
}
|
|
|
09-11-2010, 01:32 AM
|
#2
|
Member
Registered: May 2010
Posts: 83
Rep:
|
Hi -
As far as I can tell, the only reason you'd be getting an error in "sendto()" is because you failed to assign an IP address. Here's your code, slightly reformatted, and using code blocks:
Code:
STATUS
send_request(const char * data, int len)
{
int fd = -1; /* socket filedescriptor */
int result = 0; /* sendto status */
struct sockaddr_in sa; /* structure for handling internet addressing */
/* create UDP socket PF_PACKET for post 2.0 kernel */
fd = socket(PF_PACKET, SOCK_DGRAM, IPPROTO_UDP);
if (-1 == fd)
{
printf ("%s:%d socket failed %s", __FUNCTION__, __LINE__, strerror(errno) );
return FAILED;
}
/* setup sockaddr_in struct */
memset((char *) &sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = htons(SEND_PORT);
/* ??? sa.sin_addr.s_addr ??? */
/* send packet */
printf ("%s:%d sendto fd:%d sizeof(packet_t):%d sizeof(pkt):%d",
__FUNCTION__, __LINE__,fd, sizeof(packet_t), sizeof(pkt));
result = sendto(fd, data, len, 0, (struct sockaddr *)&sa, sizeof(sa));
if(-1 == result)
{
...
|
|
|
09-11-2010, 11:13 AM
|
#3
|
LQ Newbie
Registered: Sep 2010
Posts: 5
Original Poster
Rep:
|
Thank you for your response. I tried adding the IP but still see the same problem. Here are the code change:
Code:
if (0 == inet_aton("255.255.255.255", &sa.sin_addr))
{
// error case
printf (logbuf, sizeof(logbuf), "%s:%d inet_aton failed", __FUNCTION__, __LINE__ );
if (-1 == (close(fd)))
{
printf ("%s:%d close failed %s", __FUNCTION__, __LINE__, strerror(errno) );
}
return FAILED;
}
|
|
|
All times are GMT -5. The time now is 06:34 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|