LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices



Reply
 
Search this Thread
Old 12-26-2012, 12:25 AM   #1
lohith.nayak
LQ Newbie
 
Registered: Sep 2012
Posts: 12

Rep: Reputation: Disabled
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.
 
Old 12-26-2012, 03:19 PM   #2
nini09
Senior Member
 
Registered: Apr 2009
Posts: 1,058

Rep: Reputation: 88
You don't need bind source IP address.
 
Old 12-27-2012, 01:29 AM   #3
lohith.nayak
LQ Newbie
 
Registered: Sep 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
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.
 
Old 12-27-2012, 03:20 PM   #4
nini09
Senior Member
 
Registered: Apr 2009
Posts: 1,058

Rep: Reputation: 88
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);
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Httpd: "Invalid argument: alloc_listener: failed to get a socket for (null)" nirox Slackware 10 07-22-2012 04:13 PM
Why socket option "IPV6_ROUTER_ALERT" is invalid? heartstring Programming 2 01-16-2010 09:26 PM
alsa mixer sayin "function snd_mixer_load failed:invalid argument" callmeleroi Fedora 0 03-11-2007 10:05 AM
Quagga "Can't bind socket: Invalid argument" JJX Linux - Networking 0 04-29-2004 11:21 AM
Read() function In "socket.h" RAW_STREAM Penguinizer Programming 3 02-22-2003 02:14 AM


All times are GMT -5. The time now is 11:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration