Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to work out the Exercises 7.1 in UNIX Network Programming Volume 1; but when I print the send and receive buffer of UDP, the error is "getsockopt error; Protocol not available"

Thank you very much.

the code is:
#include "unp.h"
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include "error.c"
union val
{
    int i_val;
    long l_val;
    struct linger linger_val;
    struct timeval  timeval_val;
}val;
static char* sock_str_flag(union val*, int);
static char* sock_str_int(union val*, int);
static char* sock_str_linger(union val*, int);
static char* sock_str_timeval(union val*, int);
struct sock_opts
{
        const char* opt_str;
        int opt_level;
        int opt_name;
        char *(*opt_val_str)(union val*, int);
}sock_opts[] = {
                {"SO_RCVBUF", IPPROTO_TCP, SO_RCVBUF, sock_str_int},
                {"SO_SNDBUF", IPPROTO_TCP, SO_SNDBUF, sock_str_int},
                {"SO_RCVBUF", IPPROTO_UDP, SO_RCVBUF, sock_str_int},
                {"SO_SNDBUF", IPPROTO_UDP, SO_SNDBUF, sock_str_int},
                {NULL, 0, 0, NULL}
                };
int main(int argc, char** argv)
{
    int fd;
    socklen_t len;
    struct sock_opts *ptr;
    for(ptr = sock_opts; ptr->opt_str != NULL; ptr++)
    {
        if(ptr->opt_str != NULL)
            printf("the printf is %s: \n", ptr->opt_str);
        else
        printf("NULL\n");
        if(ptr->opt_val_str == NULL)
            printf("(undefined)\n");
        else
        {
            switch(ptr->opt_level)
            {
                case IPPROTO_TCP:
                    fd = socket(AF_INET, SOCK_STREAM, 0);
                    break;
                case IPPROTO_UDP:
                    fd = socket(AF_INET, SOCK_DGRAM, 0);
                    printf("udp fd is %d\n", fd);
                    break;
                default:
                    err_quit("can't create fd for level %d\n", ptr->opt_level);
            }
        }
        len = sizeof(val);
        if(getsockopt(fd, ptr->opt_level, ptr->opt_name, &amp;val, &amp;len) == -1)
        {
            err_ret("getsockopt error");
        }
        else
            printf("default = %s \n", (*ptr->opt_val_str)(&amp;val, len));
        close(fd);
    }
    exit(0);
}
static char strres[128];
static char* sock_str_int(union val* ptr, int len)
{
    if(len != sizeof(int))
        snprintf(strres, sizeof(strres), "size (%d) is not int", len);
    else
        snprintf(strres, sizeof(strres), "%d", ptr->i_val);
    return strres;
}
Posted
Updated 20-Jan-11 3:57am
v2
Comments
pasztorpisti 20-Jan-11 9:30am    
I don't know the answer, but it is possible that your OS implementation of the network stack doesn't support setting some of the socket options for UDP. You should also mention the OS you use and its version number, maybe a unix/linux guru can tell you more then.

Linux isn't Unix. They're close, but there will be differences. Try this web site:

Linux Socket programming tutorials[^]
 
Share this answer
 
Comments
HimanshuJoshi 20-Jan-11 10:37am    
Yeah, I forgot to add that, there are some differences and one will run into problems if one is reading unix book and experimenting in linux.
The socket level for SO_SENDBUF and SO_RECVBUF is SOL_SOCKET not IPPROTO_TCP or IPPROTO_UDP. The send and receive buffer socket options are handled a socket level in getsockopt and setsockopt. Check here[^] for more information.
 
Share this answer
 
Comments
Nish Nishant 20-Jan-11 10:33am    
Voted 5, proposed as answer.
HimanshuJoshi 20-Jan-11 10:35am    
Thanks Nish

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900