C socket programming UDP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ted

    #1

    C socket programming UDP

    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_fam ily = AF_INET; /* TCP/IP address family*/

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

    if(phe = gethostbyname(h ost))
    bcopy (phe->h_addr,(char*) &servadd.sin_ad dr,phe->h_length);
    else
    {fprintf(stderr , "can't get \"%s\"host entry\n",host);
    // exit (NULL ); // warnings here?
    } return ( &servadd);
    }
  • Ulrich Eckhardt

    #2
    Re: C socket programming UDP

    Ted wrote:[color=blue]
    > sendto( sockno, (char *)&buff, sizeof( buff ), 0,[/color]

    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


    Comment

    • Default User

      #3
      Re: C socket programming UDP

      Ted wrote:
      [color=blue]
      > Hi all,
      >
      > I am trying to learn C socket programming and I have a small program
      > which is a UP client.[/color]


      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.progr ammer is likely a good newsgroup for you.




      Brian

      Comment

      • Ted

        #4
        Re: C socket programming UDP

        Hi again,

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

        Thanks again,
        Ted

        "Default User" <first.last@boe ing.com.invalid > wrote in message news:<I7CA54.5I G@news.boeing.c om>...[color=blue]
        > Ted wrote:
        >[color=green]
        > > Hi all,
        > >
        > > I am trying to learn C socket programming and I have a small program
        > > which is a UP client.[/color]
        >
        >
        > 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.progr ammer is likely a good newsgroup for you.
        >
        >
        >
        >
        > Brian[/color]

        Comment

        • Bharath

          #5
          Re: C socket programming UDP

          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

          [color=blue]
          > 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;
          > }[/color]

          Comment

          Working...