By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,498 Members | 1,558 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,498 IT Pros & Developers. It's quick & easy.

C socket programming UDP

P: n/a
Ted
Hi all,

I am trying to learn C socket programming and I have a small program
which is a UP client.
The problem is, when I run the program, I get a runtime error -
"Invalid Argument" - from a call to sendto.

I was hoping that if someone has the time they could take a look at my
code posted below and let me know what i'm doing wrong?

Thanks in advance,

Ted

/******************************* start
***********************************/
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
extern int errno;

#define LINELEN 128
#define BUFFLEN 120

struct sockaddr_in * getserveraddr ( char *, char * ) ;/* return
server add */

int main( argc, argv )
int argc;
char *argv[];
{
char *host; /*used to hold name of host */
char *service; /* used to hold port no. as a string */

if( argc == 3 ) {
host = argv[ 1 ];
service = argv[ 2 ];
}
else {
fprintf( stderr, "usage: UDPConn [ host [ port ] ]\n" );
exit( 1 );
}
UDPConn( host, service );
exit( 0 );
}

/*------------------------------------------------------------------------
* UDPConn - send input to ECHO service on specified host and print
reply
*------------------------------------------------------------------------
*/
int UDPConn( host, service )
char *host;
char *service;
{
char buff[ 120 ]="whattime";
int s; /* counter */
int sockno ; /*server socket number */
struct sockaddr_in *addrptr;
//int i;
//int now;

sockno = socket( PF_INET, SOCK_DGRAM, 0 );
if( sockno < 0 ) {
perror( "Cannot open socket...\n" );
}
(struct sockaddr *)addrptr = getserveraddr( host, service );
sendto( sockno, (char *)&buff, sizeof( buff ), 0,
(struct sockaddr *)&addrptr, sizeof( struct sockaddr) );
fprintf(stderr,"after call to sendto in
Client >> %s\n",strerror( errno ) ); //error
"invalid argu..."
....
....

return 0;
}

/*------------------------------------------------------------------------
* getsererveraddr - get server address and put it in servadd
*------------------------------------------------------------------------
*/
struct sockaddr_in * getserveraddr( char *host, char *service) /*
port number as a character string */
{
static struct sockaddr_in servadd; /* structure to hold server
address */
struct hostent *phe;
int s, type;

bzero((char *)&servadd, sizeof( servadd ) );
servadd.sin_family = AF_INET; /* TCP/IP address family*/

if ( ( servadd.sin_port = htons( (ushort)atoi( service ) ) ) == 0 )
{
fprintf( stderr, "cant get \"%s\" service entry\n", service );
//exit(NULL ); // warnings here?
}

if(phe = gethostbyname(host))
bcopy (phe->h_addr,(char*)&servadd.sin_addr,phe->h_length);
else
{fprintf(stderr, "can't get \"%s\"host entry\n",host);
// exit (NULL ); // warnings here?
} return ( &servadd);
}
Nov 14 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Ted wrote:
sendto( sockno, (char *)&buff, sizeof( buff ), 0,


That call is bogus, rethink what you are doing here, in particular the
relation between a pointer and an array! As a hint, let me tell you that
you don't need any casts, adding them without understanding the reasons
why was a mistake in the first place. There are more such casts throughout
your program that only serve hiding useful compiler warnings.

Uli
Nov 14 '05 #2

P: n/a
Ted wrote:
Hi all,

I am trying to learn C socket programming and I have a small program
which is a UP client.

There is no such thing a socket programming in ISO standard C, the
topic of this newsgroup. It looks very much like you are doing UNIX
sockets, so comp.unix.programmer is likely a good newsgroup for you.


Brian
Nov 14 '05 #3

P: n/a
Ted
Hi again,

Thanks to everyone for their comments and help.
I will also try the sockets, so comp.unix.programmer group.

Thanks again,
Ted

"Default User" <fi********@boeing.com.invalid> wrote in message news:<I7********@news.boeing.com>...
Ted wrote:
Hi all,

I am trying to learn C socket programming and I have a small program
which is a UP client.

There is no such thing a socket programming in ISO standard C, the
topic of this newsgroup. It looks very much like you are doing UNIX
sockets, so comp.unix.programmer is likely a good newsgroup for you.


Brian

Nov 14 '05 #4

P: n/a
Ted,

Not sure if you are still want to read this. I think the problem is
here.

In the call to sendto, since buff and addrptr are already pointers, you
should not pass in their address. So, the call should be

sendto( sockno, buff, sizeof( buff ), 0,
(struct sockaddr *) addrptr, sizeof( struct sockaddr) );

Also, you might want to check the errorno only after you verify the
return code of sendto is -1.

Bharath

int UDPConn( host, service )
char *host;
char *service;
{
char buff[ 120 ]="whattime";
int s; /* counter */
int sockno ; /*server socket number */
struct sockaddr_in *addrptr;
//int i;
//int now;

sockno = socket( PF_INET, SOCK_DGRAM, 0 );
if( sockno < 0 ) {
perror( "Cannot open socket...\n" );
}
(struct sockaddr *)addrptr = getserveraddr( host, service );
sendto( sockno, (char *)&buff, sizeof( buff ), 0,
(struct sockaddr *)&addrptr, sizeof( struct sockaddr) );
fprintf(stderr,"after call to sendto in
Client >> %s\n",strerror( errno ) ); //error
"invalid argu..."
...
...

return 0;
}


Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.