C socket programming UDP


T

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);
}
 
Ad

Advertisements

U

Ulrich Eckhardt

Ted said:
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
 
D

Default User

Ted said:
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
 
T

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
 
Ad

Advertisements

B

Bharath

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top