LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sendto failed -1 : Invalid argument (http://www.linuxquestions.org/questions/linux-newbie-8/sendto-failed-1-invalid-argument-831477/)

gr1980 09-10-2010 07:07 PM

sendto failed -1 : Invalid argument
 
I'm not sure why the sndto() is failing. 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;
}

Tinkster 09-11-2010 02:01 AM

Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate.


http://www.linuxquestions.org/questi...gument-831489/


In this case we closed the original in favour of the more
sensibly placed duplicate in programming.


All times are GMT -5. The time now is 02:56 PM.