【发布时间】:2012-08-05 17:09:27
【问题描述】:
我写了以下函数并且工作正常:
int NetworkSocket::readDatagrams(unsigned char *buffer, string &srcAddress, unsigned short int & srcPort,size_t frameSize)
{
unsigned int maximumPacketSize = sizeof(buffer);//frameSize ;
int returnValue ;
sockaddr_in from;
socklen_t fromLength = sizeof( from );
int receivedBytes;
fromLength = sizeof(this->getSocketAddressStructureOfServer());
receivedBytes = recvfrom( this->socketFD, buffer, maximumPacketSize, 0, (sockaddr *)&this->getSocketAddressStructureOfServer(), &fromLength ) ;
cout << receivedBytes << endl;
returnValue = receivedBytes;
if ( receivedBytes <= 0 )
returnValue = -1;
/// exporting data
//
srcAddress = inet_ntoa(this->getSocketAddressStructureOfServer().sin_addr);
srcPort = ntohs( ( unsigned short int)this->getSocketAddressStructureOfServer().sin_port );
return returnValue;
}
当我应用它时,它在 ref srcAddress 和端口中返回,即使缓冲区已满,但我无法 printf 缓冲区,我该怎么做? wireshakr show 000010001 这意味着 5 个字节,我需要检查它并与我的数据进行比较。
【问题讨论】:
-
请注意,
unsigned int maximumPacketSize = sizeof(buffer);将最大数据包大小设置为char*的大小,通常为 4 或 8 个字节。 -
另外,我看不到 printf 失败的确切行...或任何 printf,就此而言。
-
::是 C 中的语法错误。可能是 C++ 吗?