我想使用 socketpair 在两个线程之间发送消息。我正在编写代码以找出可以使用套接字对发送多少条消息,消息大小为 16 字节(两个指针)。我使用的代码如下:
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++;
}
没有线程/进程正在监听 readFD。所以当 writeFD 的发送缓冲区已满时,while 循环应该退出。
可以看到以下输出:
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
我预计发送的消息数量约为 4032 ( 129024/(2*16) )。
我在这里想念什么?在 AF_LOCAL 中发送消息时是否使用了固定大小的标头?