Question

I am facing some issues in UDP socket programming. I want to use the sendmsg() and recvmsg() APIs to be able to communicate between client and server. The client code is returning with an error number 105 {ENOBUFS}. I am clueless for what reason it is showing this error, because as per the description of those APIs, its written

ENOBUFS

The  output  queue for a network interface was full.  This generally indicates that the interface has stopped sending, but may be caused by transient congestion.  (Normally, this does  not  occur in Linux. Packets are just silently dropped when a device queue overflows.)

so, its like this error is not common, then also i'm facing this. If anyone have faced simmilar issues, please share some information. I'm looking for a hint to the solution, rather than the ready-made solution. Also, no alternate API, please. Need the follwing piece of code to work.

Server Code

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <string.h>
#include <errno.h>

#define PORT            32000
#define BUFSIZE         8192

int main()
{
    int sockfd,n;
    struct sockaddr_in servaddr,cliaddr;
    socklen_t len;
    char mesg[1000];
    char message_control_array[BUFSIZE];
    int count = 1;
    struct iovec io;
    int ret = 0;
    int ttlval = 255;

    /* Messageheader structure to pass to recvmsg call*/
    struct msghdr header;

    /* This structure will contain the individual ancillary messages*/
    struct cmsghdr *cmsg;

    /* Filling up the control data related variables.*/
    header.msg_name         = &servaddr;
    header.msg_namelen      = sizeof (struct sockaddr_in);
    header.msg_control      = message_control_array;
    header.msg_controllen   = BUFSIZE;
    header.msg_iov          = &io;
    header.msg_iov->iov_base= mesg;
    header.msg_iov->iov_len = 1000;
    header.msg_iovlen       = 1;

    /*create UDP socket*/
    if ((sockfd=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP)) < 0)
    {
            printf("Error 1: Socket\n");
            exit(1);
    }

    ret = setsockopt (sockfd, IPPROTO_IP, IP_TTL, &ttlval, sizeof(ttlval));
    if ( ret == -1)
    {
            printf("setsockopt failing for IP_TTL, errno %d\n", errno);
    }

    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
    servaddr.sin_port=htons(PORT);

    if ((bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr))) == -1)
    {
            printf("Error 2:bind\n");
            exit(1);
    }
    while (1)
{
  //len = sizeof(cliaddr);
  //if ((n = recvfrom(sockfd,mesg,1000,0,(struct sockaddr *)&cliaddr,&len)) == -1)
  if ((n = recvmsg(sockfd,&header, 0)) == -1)
    {
            printf("recvmsg error!!!!!!!!!!!!!\n\n");
    }
    /*Send the received packet back to the sender [client]*/
    //sendto(sockfd,mesg,n,0,(struct sockaddr *)&cliaddr,sizeof(cliaddr));

    printf("-------------------------------------------------------\n");
    mesg[n] = 0;
    printf("Received the following:\n");
    printf("%s\n",mesg);
    printf("The info from the msghdr structure\n");
    printf("The source address is %s\n", inet_ntoa(servaddr.sin_addr.s_addr));
    printf("The source address length is %d\n", (int)header.msg_namelen);
    for (cmsg = CMSG_FIRSTHDR(&header); cmsg != NULL ; cmsg = CMSG_NXTHDR(&header, cmsg))
    {
            printf("number %d cmsg structure\n", count++);
            //if
    }
  printf("-------------------------------------------------------\n");
}
return 0;
}

Client Code

/* Sample UDP client */

#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/in.h>

#define PORT 3200
#define BUFSIZE 512

int main(int argc, char**argv)
{
    int sockfd,n;
    struct sockaddr_in servaddr,cliaddr;
    char sendline[BUFSIZE];
    char recvline[BUFSIZE];
    int ret = 0;
    int ttlval = 255;
            /*required for sending ancillary data using sendmsg() call*/
            struct msghdr header;
    struct cmsghdr *cmsg;
    struct iovec io;

    if (argc != 2)
    {
            printf("usage:  %s <Server IP address>\n", argv[0]);
            exit(1);
    }

    sockfd=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);

    ret = setsockopt (sockfd, IPPROTO_IP, IP_TTL, &ttlval, sizeof (ttlval));
    if (ret == -1)
    {
            printf("setsockopt failing for IP_TTL , errno %d\n", errno);
    }

    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr=inet_addr(argv[1]);
    servaddr.sin_port=htons(PORT);

    header.msg_name         = &servaddr;
    header.msg_namelen      = sizeof (struct sockaddr_in);
    header.msg_iov          = &io;
    header.msg_iov->iov_base= sendline;
    header.msg_iov->iov_len = BUFSIZE;
    header.msg_iovlen       = 1;

    while (1)
    {
 tryagain:printf("Enter the string to be sent\n");
     //scanf("%s", sendline);
     if ((fgets(sendline, 100, stdin)) == NULL)
     {
             printf("error in reading from stdin\n\n");
             goto tryagain;
     }
    header.msg_iov->iov_len = strlen(sendline) - 1;
    printf("kddkdd %d %d %s\n", sizeof(sendline), strlen(sendline), sendline);
     //sendto(sockfd,sendline,strlen(sendline),0,(struct sockaddr *)&servaddr,sizeof(servaddr));
     if ((sendmsg(sockfd, &header, 0)) == -1)
     {
             printf("Error in sendmsg,\t [errno = %d]\n", errno);
     }
#if 0
     if ((n=recvfrom(sockfd,recvline,10000,0,NULL,NULL)) == -1)
     {
             printf("recvfrom error\n");
             exit(1);
     }
     recvline[n]=0;
     fputs(recvline,stdout);
     printf("packet sent\n\n");
#endif
    }
    return 0;
}

the output is like

[root@localhost udp]# ./client 30.5.42.194
Enter the string to be sent
sourav
Error in sendmsg,        [errno = 105]
Enter the string to be sent
  
  

I'm using RHEL (Red Hat 4.1.2-48) with kernel 2.6.18-194.el5 having gcc version 4.1.2

Was it helpful?

Solution

Your are not initializing properly the msghdr struct. You need to do it either at declaration :

struct msghdr header = { 0 };

or later :

header.msg_controllen = 0;
header.msg_control = NULL;
header.msg_flags = 0;

Otherwise msg_controllen content is garbage.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top