How to avoid 'No buffer space available' on C socket?

Thread Tools Search this Thread
Top Forums Programming How to avoid 'No buffer space available' on C socket?
# 1  
Old 10-01-2013
How to avoid 'No buffer space available' on C socket?

Hello everybody,

Years ago i left in stand-by a project of mine where the main program was supposed to send thousands ARP frames over the socket as fast as it could; but because of a programming issue i couldn't continue it.

2 days ago I decided to solve that issue.

The thing is, when the program sends bunch of data, eventually it gives the error "No buffer space available". I increased the buffer space size and it gave more sending time, but it gave the same error message later. My question is, Is there a way to send bunch of data, fast and avoiding this socket error? I don't know, by freeing it, cleaning it, etc.

I'll appreciate any help, and please excuse my poor english.
Thank you.

Here's part of the code:
unsigned int buff;
socklen_t optlen;
optlen = sizeof(buff);
struct sockaddr sockaddr;
struct ifreq ifreq;

memset(&ifreq, 0, sizeof(ifreq));
strcpy(sockaddr.sa_data, device); /* Transmitting device is set. */
strcpy(ifreq.ifr_name, device);

if((sock = socket(AF_PACKET, SOCK_PACKET, htons(ETH_P_ALL))) < 0)
if((setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, &ifreq, sizeof(ifreq))) < 0)
        perror("setsockopt() cannot bind.");

if((resource=getsockopt(sock, SOL_SOCKET, SO_SNDBUF, &buff, &optlen))<0)

printf("Attempting to increase SNDBUF... ");
buff = 2147483647; /*MAXIMUM*/

if((resource = setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &buff, 


            time_t startTime;
            startTime = time(NULL);

        while(time(NULL) - startTime < (time_assigned * 60))
    printf("%u\n", (unsigned int )azimuth);        
    ret= sendto(sock, &arpmsg, sizeof(struct arpmsg),0, &sockaddr,sizeof(sockaddr));

    if(ret < 0 && ret != ENOBUFS) 

# 2  
Old 10-07-2013
Do you have code for the server program as well...if so post it here and what kind of socket are you creating a local or network socket...

Last edited by shamrock; 10-07-2013 at 02:37 PM..
# 3  
Old 10-07-2013
It's not hard for a program to fill a buffer far faster than your network can send data. That is why you're running out of buffer. You'll just have to trust the OS to send the data as fast as it can, unless you feel like writing your own driver.
# 4  
Old 10-13-2013
And isn't there a way to avoid this? I don't know... Increasing socket sending time or reducing it by SO_RCVTIMEO or SO_SNDTIMEO.

I don't care if my program has to hang a little bit between sending blocks, I just don't want it to close because of the socket error.

---------- Post updated at 06:34 PM ---------- Previous update was at 05:21 PM ----------

Corona688, You gave me the answer... TIME.

If my socket overloads its buffer it crashes, so, I solved it giving it time in this way:

while(time(NULL) - startTime < (time_assigned * 60))
 retry: /*This is a label.*/
 ret = sendto(sock, &arpmsg, sizeof(struct arpmsg),0, &sockaddr,sizeof(sockaddr));

    if(ret < 0) 
           if(errno == ENOBUFS)
              printf("Socket ran out of buffer space.\n");
              printf("Giving it some time...\n");
              goto retry;
             }else {

So, if my socket runs out of buffer space while sending, it waits some nsecs, and then goes to the label where it retries to send the message. I don't know if this ain't the best way to avoid it, but keeping it simple, It works!.

Thanks everybody for your help!
As always, the best place on the Internet to ask for a good programming advice is here. Smilie
# 5  
Old 10-21-2013
Originally Posted by Zykl0n-B
And isn't there a way to avoid this?
Usually you would leave the socket as blocking, so that when the buffer fills, your program waits. If you can't trust the OS to transmit as fast as possible, then you're stuck either way, since you can't not use the OS...

Last edited by Corona688; 10-21-2013 at 01:53 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. IP Networking

Clarification - Setting socket options at the same time when socket is listening

I need clarification on whether it is okay to set socket options on a listening socket simultaneously when it is being used in an accept() call? Following is the scenario:- -- Task 1 - is executing in a loop - polling a listen socket, lets call it 'fd', (whose file descriptor is global)... (2 Replies)
Discussion started by: jake24
2 Replies

2. Shell Programming and Scripting

How to avoid the truncating of multiple spaces into a single space while reading a line from a file?

consider the small piece of code while read line do echo $line done < example content of example file sadasdasdasdsa erwerewrwr ergdgdfgf rgerg erwererwr the output is like sadasdasdasdsa erwerewrwr ergdgdfgf rgerg erwererwr the... (4 Replies)
Discussion started by: Kesavan
4 Replies

3. Linux

Linux Device Driver: avoid mem copy from/to user/kernel space

I recently started working with Linux and wrote my first device driver for a hardware chip controlled by a host CPU running Linux 2.6.x kernel. 1. The user space process makes an IOCTL call with pointer to a user memory buffer. 2. The kernel device driver in the big switch-case of IOCTL,... (1 Reply)
Discussion started by: agaurav
1 Replies

4. Programming

Store file into a buffer to send it through a socket

Hello, I'm doing a very simple program which reads a file and sends whatever is in the file through a socket. Like the program "file2cable". Let's say i have a file containing the following, which is a hex dump of an ARP request frame: ff ff ff ff ff ff 00 1b 24 79 5a 73 08 06 00 01 08... (5 Replies)
Discussion started by: semash!
5 Replies

5. Programming

Finding used socket receive-buffer size

I have set the receive buffer size of socket to max. setsockopt(sd,SOL_SOCKET, SO_RCVBUF,&max,optval); Am reading data from the socket in a loop(say max 100 bytes per recv) while(1) { int rlen=recv(sd,(void *)buf, 100 , 0); //err handle and processing } Assume my process is slow... (2 Replies)
Discussion started by: johnbach
2 Replies

6. Shell Programming and Scripting

How to avoid the space

Hi, I have some problem with following command. $path='pwd'.”/pack”; When I check the value in the variable path , it give the following output /tmp/new /pack The pwd will be /tmp/new Now the problem is the spaces is added between pwd and the appended path. I need the output like... (5 Replies)
Discussion started by: kalpeer
5 Replies

7. UNIX for Advanced & Expert Users

connect problem for sctp socket (ipv6 socket) - Runtime fail Invalid Arguments

Hi, I was porting ipv4 application to ipv6; i was done with TCP transports. Now i am facing problem with SCTp transport at runtime. To test SCTP transport I am using following server and client socket programs. Server program runs fine, but client program fails giving Invalid Arguments for... (0 Replies)
Discussion started by: chandrutiptur
0 Replies

8. Shell Programming and Scripting

how to avoid space to run remotely

If I run the following command remotely after ssh than it works fine su - oracle -c "/oracle/product/102/db/bin/dbshut" But If I run the following command it doesn't work su - oracle -c "/oracle/product/102/db/bin/lsnrctl stop" Because I think there is a space is present between lsnrctl and... (1 Reply)
Discussion started by: madhusmita
1 Replies

9. Linux

ssh: connect to host traviata port 22: No buffer space available

What does that mean? traviata being my distant host? What am i to do? (0 Replies)
Discussion started by: penguin-friend
0 Replies

10. Programming

how to clear/clean mbufs (network buffer space)?

When I worked with client-server (socket) programming, I encountered "the socket error# 10055" which means "No buffer space available". This might be a symptom of one or more applications that didn't return system resources (like memory) properly. Temporary solution was to reboot the machine to... (7 Replies)
Discussion started by: dipti
7 Replies
Login or Register to Ask a Question