Reputation: 2935
I wrote the following function and works fine:
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;
}
When i apply it, it return in ref srcAddress and port Even buffer is full, but i can't printf buffer, how i do it? wireshakr show 000010001 it means 5 bytes, and i need to examine it and compare to my data.
Upvotes: 0
Views: 164
Reputation: 2598
unsigned int maximumPacketSize = sizeof(buffer);//frameSize ;
This line is faulty, since you're requesting the size of the pointer variable (which always equals '4' on a 32-bit system). You have to pass the maximum buffer size manually, or simply 'know' it, which shouldn't be a problem, since you instantiated it in the first place.
In the essence that means, you can never get more than 4 bytes.
Upvotes: 3