I want to send messages between two threads using socketpair. I am writing code to find out how many messages can be send using a socketpair, with message of size 16 bytes ( two pointers ). The code I used is below:
int fds[2];
socketpair(AF_LOCAL, SOCK_STREAM, PF_UNSPEC, fds);
int readFD=fds[0];
int writeFD=fds[1];
getsockopt(readFD, SOL_SOCKET, SO_SNDBUF, &rSndBuff, &optlen);
getsockopt(readFD, SOL_SOCKET, SO_RCVBUF, &rRcvBuff, &optlen);
cout <<"Read FD : Send Buff : "<<rSndBuff<<" Recv Buff : "<<rRcvBuff<<endl;
getsockopt(writeFD, SOL_SOCKET, SO_SNDBUF , &wSndBuff, &optlen);
getsockopt(writeFD, SOL_SOCKET, SO_RCVBUF , &wRcvBuff, &optlen);
cout <<"Write FD : Send Buff : "<<wSndBuff<<" Recv Buff : "<<wRcvBuff<<endl;
int count=0;
while ( 1 )
{
char * im[2];
int sentCount=send(writeFD, im, sizeof(im), MSG_DONTWAIT | MSG_NOSIGNAL);
if(sentCount<0)
{
ioctl ( readFD , FIONREAD , &rRcvBuff );
cout <<"Size of data sent in one message : "<<sizeof(im)<<endl;
cout <<"Recv Buff : "<<rRcvBuff<<endl;
cout <<"Sent : " <<sizeof(im)*count<<endl;
cout<<"Unable to send : "<< errno<< " "<<strerror(errno)<<endl;
cout<<"Count : " <<count<<endl;
break;
}
else if(sentCount!=sizeof(im))
{
ioctl ( readFD , FIONREAD , &rRcvBuff );
cout <<"Recv Buff : "<<rRcvBuff<<endl;
cout<<EMSGSIZE<<endl;
cout<<"Count : " <<count<<endl;
break;
}
count++;
}
No thread/process is listening to the readFD. So the while loop should exit when the send buffer of writeFD is full.
Following output is seen:
Read FD : Send Buff : 129024 Recv Buff : 129024
Write FD : Send Buff : 129024 Recv Buff : 129024
Size of data sent in one message : 16
Recv Buff : 5504
Sent : 5504
Unable to send : 11 Resource temporarily unavailable
Count : 344
I was expecting the number of messages sent to be around 4032 ( 129024/(2*16) ).
What am I missing here ? Is there a fixed sized header used while sending messages in AF_LOCAL ?
Seems similar to: AF_UNIX socket overhead?.
The limit reached should be
net.unix.max_dgram_qlen
: maximum number of datagram to enqueue.To read actual value:
To set a new value: