C socket programming UDP

Discussion in 'C Programming' started by Ted, Nov 17, 2004.

  1. Ted

    Ted Guest

    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);
    }
    Ted, Nov 17, 2004
    #1
    1. Advertising

  2. 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
    Ulrich Eckhardt, Nov 17, 2004
    #2
    1. Advertising

  3. Ted

    Default User Guest

    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
    Default User, Nov 17, 2004
    #3
  4. Ted

    Ted Guest

    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" <> wrote in message news:<>...
    > 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
    Ted, Nov 18, 2004
    #4
  5. Ted

    Bharath Guest

    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;
    > }
    Bharath, Feb 20, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. socket programming UDP

    , Mar 26, 2005, in forum: C Programming
    Replies:
    1
    Views:
    374
    Walter Roberson
    Mar 26, 2005
  2. Laszlo Nagy
    Replies:
    1
    Views:
    4,733
    Mark Wooding
    Jan 27, 2009
  3. Jean-Paul Calderone
    Replies:
    0
    Views:
    936
    Jean-Paul Calderone
    Jan 27, 2009
  4. Laszlo Nagy
    Replies:
    0
    Views:
    515
    Laszlo Nagy
    Feb 1, 2009
  5. Kishore_R

    Socket() failed for UDP socket

    Kishore_R, Oct 11, 2005, in forum: Perl Misc
    Replies:
    2
    Views:
    215
    Anno Siegel
    Oct 11, 2005
Loading...

Share This Page