some problem in UDP programming under linux - Networking

This is a discussion on some problem in UDP programming under linux - Networking ; Dear All : I have some problem in UDP clinet programming, my source code under line. #include #include #include #include #include #include #include #define PORT 65536 #define SERVER_IP "10.l10.138.200" main() { int s,len; struct sockaddr_in addr; int addr_len = sizeof(struct ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: some problem in UDP programming under linux

  1. some problem in UDP programming under linux

    Dear All :
    I have some problem in UDP clinet programming, my source code
    under line.

    #include
    #include
    #include
    #include
    #include
    #include
    #include

    #define PORT 65536
    #define SERVER_IP "10.l10.138.200"

    main()
    {
    int s,len;
    struct sockaddr_in addr;
    int addr_len = sizeof(struct sockaddr_in);
    char buffer[256];
    if ((s = socket(AF_INET, SOCK_DGRAM, 0))<0)
    {
    perror("socket");
    exit(1);
    }
    bzero(&addr, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);
    addr.sin_addr.s_addr = inet_addr(SERVER_IP);
    while(1)
    {
    bzero(buffer, sizeof(buffer));
    len=read(STDIN_FILENO, buffer, sizeof(buffer));
    sendto(s, buffer, len, 0, &addr, addr_len);
    len = recvfrom(s, buffer, sizeof(buffer), 0, &addr, &addr_len);
    printf("receive: %s",buffer);
    }
    }

    But I complier in Red Hat 9 have three bug
    udpclient1.C: In function 'int main()'
    udpclient1.C:34: cannot convert 'sockaddr_in*' to 'const sockaddr*'
    for argument '5' to 'ssize_t sendto(int, const void*, unsigned int,
    int, const sockaddr* . unsigned int)'
    udpclient1.C:35: cannot convert 'sockaddr_in*' to 'const sockaddr*'
    for argument '5' to 'ssize_t recvfrom(int, void*, unsigned int, int,
    const sockaddr* . socklen_t*)'

    Does somebody have an idea to resolve my problem?
    Thank you.


  2. Re: some problem in UDP programming under linux

    In news:1172538974.957648.123840@z35g2000cwz.googlegr oups.com,
    s9327620@csie.cyut.edu.tw wrote:

    > I have some problem in UDP clinet programming, my source code
    > under line.

    ....
    > #define SERVER_IP "10.l10.138.200"

    ....
    > But I complier in Red Hat 9 have three bug
    > udpclient1.C: In function 'int main()'
    > udpclient1.C:34: cannot convert 'sockaddr_in*' to 'const sockaddr*'
    > for argument '5' to 'ssize_t sendto(int, const void*, unsigned int,
    > int, const sockaddr* . unsigned int)'
    > udpclient1.C:35: cannot convert 'sockaddr_in*' to 'const sockaddr*'
    > for argument '5' to 'ssize_t recvfrom(int, void*, unsigned int, int,
    > const sockaddr* . socklen_t*)'
    >
    > Does somebody have an idea to resolve my problem?


    Try fixing the typo in SERVER_IP.

  3. Re: some problem in UDP programming under linux

    On Feb 26, 8:16 pm, s9327...@csie.cyut.edu.tw wrote:
    > Dear All :
    > I have some problem in UDP clinet programming, my source code
    > under line.
    >
    > #include
    > #include
    > #include
    > #include
    > #include
    > #include
    > #include
    >
    > #define PORT 65536


    /*
    ** since port numbers range from 1 to 65535,
    ** the original port number of 65536 is out of range
    ** and will not work. Use a different port
    */
    #define PORT 65534

    > #define SERVER_IP "10.l10.138.200"


    /*
    ** The server IP must either be a legal dotted decimal
    ** number, or a DNS name. The original is neither.
    ** Use a corrected dotted decimal IP address
    */
    #define SERVER_IP "10.10.138.200"


    > main()
    > {
    > int s,len;
    > struct sockaddr_in addr;
    > int addr_len = sizeof(struct sockaddr_in);
    > char buffer[256];
    > if ((s = socket(AF_INET, SOCK_DGRAM, 0))<0)
    > {
    > perror("socket");
    > exit(1);}
    >
    > bzero(&addr, sizeof(addr));
    > addr.sin_family = AF_INET;
    > addr.sin_port = htons(PORT);
    > addr.sin_addr.s_addr = inet_addr(SERVER_IP);
    > while(1)
    > {
    > bzero(buffer, sizeof(buffer));
    > len=read(STDIN_FILENO, buffer, sizeof(buffer));
    > sendto(s, buffer, len, 0, &addr, addr_len);



    /*
    ** sendto() takes a generic sockaddr pointer, but we have a
    ** TCP/IP sockaddr_in pointer. Cast the sockaddr_in pointer to
    ** the generic sockaddr pointer, and continue.
    */
    sendto(s, buffer, len, 0, (struct sockaddr *)&addr, addr_len);

    > len = recvfrom(s, buffer, sizeof(buffer), 0, &addr, &addr_len);



    /*
    ** recvfrom() takes a generic sockaddr pointer, but we have a
    ** TCP/IP sockaddr_in pointer. Cast the sockaddr_in pointer to
    ** the generic sockaddr pointer, and continue.
    */
    len = recvfrom(s, buffer, sizeof(buffer), 0 (struct sockaddr
    *)&addr, &addr_len);

    > printf("receive: %s",buffer);
    >
    > }
    > }
    >
    > But I complier in Red Hat 9 have three bug
    > udpclient1.C: In function 'int main()'
    > udpclient1.C:34: cannot convert 'sockaddr_in*' to 'const sockaddr*'
    > for argument '5' to 'ssize_t sendto(int, const void*, unsigned int,
    > int, const sockaddr* . unsigned int)'
    > udpclient1.C:35: cannot convert 'sockaddr_in*' to 'const sockaddr*'
    > for argument '5' to 'ssize_t recvfrom(int, void*, unsigned int, int,
    > const sockaddr* . socklen_t*)'


    HTH
    --
    Lew


  4. Re: some problem in UDP programming under linux

    I modify
    sendto(s, buffer, len, 0, (struct sockaddr *)&addr, addr_len) and
    len = recvfrom(s, buffer, sizeof(buffer), 0 (struct sockaddr *)&addr,
    &addr_len) bug , but have some new bug

    udpclient1.C: In function 'int main()';
    udpclient1.C: 37: invaild conversion from 'int*' to 'socklen_t*'

    please help me


  5. Re: some problem in UDP programming under linux

    On Feb 26, 6:56 pm, s9327...@csie.cyut.edu.tw wrote:
    > I modify
    > sendto(s, buffer, len, 0, (struct sockaddr *)&addr, addr_len) and
    > len = recvfrom(s, buffer, sizeof(buffer), 0 (struct sockaddr *)&addr,
    > &addr_len) bug , but have some new bug
    >
    > udpclient1.C: In function 'int main()';
    > udpclient1.C: 37: invaild conversion from 'int*' to 'socklen_t*'
    >
    > please help me


    You want 'addr_len' to be a 'socklen_t', not an 'int'.

    DS


+ Reply to Thread