C sendmsg () buffer space not available

I am not an expert in C programming, but I'm trying to write a fairly simple program using sendmsg () and recvmsg () to send a message between the client and server (both are on the same machine, so basically I send the message to localhost).

After initializing the required structures (as in iovec and msghdr) and successfully connecting the client to the server, my sendmsg () call failed with errno "out of buffer space".

This is what linux reports about this type of error:

The output queue for the network interface has been full. This usually indicates that the interface has stopped sending, but may be caused by transient congestion. (This usually does not happen on Linux. Packets are simply silently deleted when the device queue is full.)

I looked on the Internet, and as a result I found out that sendmsg () is not widely used, and no one can relate to this type of error. The only good advice I found was to check for possible exceeding of open sockets, but again I always close EVERY socket that I create.

So, I’m stuck, mainly because, being a completely noob, I don’t know exactly where to look for this problem.

If anyone knows how to act, it would be great. (And please do not tell me not to use sendmsg (), because the whole purpose of my work is to understand this scenario, and not send a message to myself)

Here is the code that I have written so far on pastebin: client and server

- solvable -

Many thanks. I was able to solve the problem, and I fixed other errors that I made, so the code for sendmsg () and recvmsg () working when sending messages: Client and Server works here

+6
source share
3 answers

As others have pointed out, iovlen should be 1. But also you want to reset mh before initializing some of your fields, since you are probably sending garbage in uninitialized fields, and syscall gets confused. In addition, it does not really make sense to set msg_name and msg_namelen, since you are connected and cannot change your mind where to send data.

This is what works for me in your client code:

/* The message header contains parameters for sendmsg. */ memset(&mh, 0, sizeof(mh)); mh.msg_iov = iov; mh.msg_iovlen = 1; printf("mh structure initialized \n"); 
+2
source

The msg_iovlen field contains the number of elements in the iov array, not its size in bytes.

The system interpreted the following uninitialized memory as iov elements, which resulted in a packet that was larger than the available socket buffer space, and thus refused to send data.

+3
source

Ok, so in your code, I found this:

 mh.msg_iovlen = sizeof(iov); 

Defines the msg_iovlen member of size struct iovec . But the documentation says about this field:

size_t msg_iovlen; / * # elements in msg_iov * /

So your code is incorrect, it tells sendmsg() that it will send more elements than you actually initialize.

+2
source

Source: https://habr.com/ru/post/927353/


All Articles