Thread: Datagram Socket

  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Rep Power

    Datagram Socket

    Hi guys, I decided to study Datagram sockets and my reference text is beej's tutorial. I'm getting some problems that I don't know how to solve, if anyone could enlight my I would be very thankfull.

    the code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    int main(int argc, char *argv[]){
        int sockfd;
        struct sockaddr_in dest;
        char msg[];
        msg = "Test";
        if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1){
        dest.sin_family = AF_INET;
        dest.sin_port = htons(argv[2]);
        dest.sin_addr = inet_addr(argv[1]);
        memset(&(dest.sin_zero), '\0',8);
        sendto(sockfd,msg,strlen(msg),0,(struct sockaddr *)&dest, sizeof(struct sockaddr));
        return 0;
    What GCC returns me:

    dgram.c: In function ‘main’:
    dgram.c:15:10: error: array size missing in ‘msg’
    dgram.c:17:9: error: incompatible types when assigning to type ‘char[1]’ from type ‘char *’
    dgram.c:25:5: warning: passing argument 1 of ‘htons’ makes integer from pointer without a cast [enabled by default]
    /usr/include/netinet/in.h:370:17: note: expected ‘uint16_t’ but argument is of type ‘char *’
    dgram.c:26:19: error: incompatible types when assigning to type ‘struct in_addr’ from type ‘in_addr_t’
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Rep Power
    What does "dgram.c:15:10: error: array size missing in ‘msg’" tell you?

    It seems to me you should learn to program in C before you start trying to program sockets.

    Comments on this post

    • ptr2void agrees : That is *exactly* what I was going to say!

    My blog, The Fount of Useless Information
    Free code:
    Secure Programming:
    Performance Programming:
    LinkedIn Profile:

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Rep Power
    When you declare an array, you need to tell the compiler how big it is.

    Command-line arguments (char* argv[]) are C-style strings -- if you don't believe me, then just look at the parameter list for main. If you want to use an argv as an integer value, then you must convert the string to an integer.

    There's a lot of basic C knowledge you still need to acquire.

IMN logo majestic logo threadwatch logo seochat tools logo