LinuxQuestions.org

LinuxQuestions.org (http://www.linuxquestions.org/questions/index.php)
-   Linux - Networking (http://www.linuxquestions.org/questions/forumdisplay.php?f=3)
-   -   Invalid return value for "sendto" socket function (http://www.linuxquestions.org/questions/showthread.php?t=4175442942)

lohith.nayak 12-25-2012 11:25 PM

Invalid return value for "sendto" socket function
 
Hello,

I am using Ubuntu 10.04 on virtual box. (Host OS : Windows XP)
I am trying to send UDP packets to another machine using socket programming.
Here is a piece of code.



Code:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <string.h>


//#define SRC_IP_ADDR        0x87246EFB
#define SRC_IP_ADDR        0x0A00020F
#define DST_IP_ADDR        0x8724B48C


struct in_addr  macLocalIP ; /* MAC local IP address */
unsigned short int  macLocalPort          = 3001;      /* MAC local UDP port */
unsigned short int  macRemotePort          = 3000;      /* MAC local UDP port */
struct sockaddr_in x_sockaddr_in;
struct sockaddr_in x_sockaddr_out;
ssize_t ret_x_r;



TxSocket()
{
        char temp_buf[1024];
       
        macXsocket = socket(AF_INET, SOCK_DGRAM, 0);
        if(macXsocket < 0 )
        {
                printf("\n Socket creation is failed \n");
                return -1;
        }
        printf("\n Socket creation is successful: %x",macXsocket);
       
      macLocalIP.s_addr =  SRC_IP_ADDR;
      x_sockaddr_in.sin_family= AF_INET;
      x_sockaddr_in.sin_addr.s_addr = htonl(SRC_IP_ADDR);
        x_sockaddr_in.sin_port = htonl(macLocalPort);
        macXbind = bind( macXsocket,
                        (const struct sockaddr *) &x_sockaddr_in,
                        sizeof(x_sockaddr_in)
                      );

        if(macXbind < 0 )
        {
                printf("\n Binding is failed \n");
                printf("Error =%s, %x\n", strerror(errno), errno);
                return -1;
        }
        printf("\n Binding is successful: %x",macXbind);

        x_sockaddr_out.sin_family= AF_INET;
        x_sockaddr_out.sin_addr.s_addr = htonl(DST_IP_ADDR);
        x_sockaddr_out.sin_port = htonl(macRemotePort);
        bzero(&(x_sockaddr_out.sin_zero),8);
        printf("\n size of socket = %x",sizeof(x_sockaddr_out));

        while(1){
        ret_x_r = sendto(macXsocket,
                        (const void *) &temp_buf[0],
                        1024,
                        0,
                        (const struct sockaddr *) &x_sockaddr_out,
                        sizeof(x_sockaddr_out));
               
        if(ret_x_r < 0)
        {
                printf("Error =%s, %x\n", strerror(errno), errno);
        }

        sleep(10);
        }


Here, sendto function always returns "-1" and printed error message is "Error =Invalid argument, 16"

I cross verified the parameter values, it seems to be fine. Could anybody point me if I am doing anything wrong here???

Thanks in advance.

nini09 12-26-2012 02:19 PM

You don't need bind source IP address.

lohith.nayak 12-27-2012 12:29 AM

I commented out "Bind function".
Yet Sendto function returns "Error =Invalid argument, 16".

I was able to run this code successfully on PC where LINUX is host OS.

nini09 12-27-2012 02:20 PM

Change code like this.

Original code:
x_sockaddr_out.sin_family= AF_INET;
x_sockaddr_out.sin_addr.s_addr = htonl(DST_IP_ADDR);
x_sockaddr_out.sin_port = htonl(macRemotePort);
bzero(&(x_sockaddr_out.sin_zero),8);

New code
bzero(&x_sockaddr_out,sizeof(x_sockaddr_out))
x_sockaddr_out.sin_family= AF_INET;
x_sockaddr_out.sin_addr.s_addr = htonl(DST_IP_ADDR);
x_sockaddr_out.sin_port = htonl(macRemotePort);


All times are GMT -5. The time now is 05:36 AM.