R
Ramiro Barbosa, Jr.
All,
Any ideas why the following code prints zeros for the message id,
number of packets and sequence number as shown in the sample output
below? Somehow I am not being able to access the contents of my
'incomingDataBuffer' array. I have also included some values from the
respective MSVC 6.0 debug session.
Thank you!
-RB
-------------------------------------------------------------------------------
#include<windows.h>
#include<iostream>
#include<string>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main(int argc, char * argv[])
{
WSADATA wsaData;
SOCKADDR_IN serverAddress;
SOCKADDR_IN clientAddress;
SOCKET receivingSocket;
int port = 7797;
int length;
char incomingDataBuffer[600];
int ret = WSAStartup(MAKEWORD(2,0), &wsaData);
if (ret != 0)
{
int errorCode = WSAGetLastError();
cout<<"ERROR ["<<errorCode<<"] initializing WinSock!"<<endl;
exit(1);
}
if (LOBYTE(wsaData.wVersion)!= 2 || HIBYTE(wsaData.wVersion)!= 0)
{
cout<<"Invalid Winsock Version"<<endl;
WSACleanup();
exit(1);
}
if (port < 1)
{
cout<<"Invalid UDP port"<<endl;
exit(1);
}
if ((receivingSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
cout<<"Unable to create socket"<<endl;
memset((char*)&serverAddress,0,sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = htons(INADDR_ANY);
serverAddress.sin_port = htons(port);
int status = bind(receivingSocket,
(sockaddr*)&serverAddress,sizeof(serverAddress));
if (status < 0)
{
cout<<"Error: bind() failed."<<endl;
exit(1);
}
// join multicast group...
struct ip_mreq mreq;
mreq.imr_multiaddr.s_addr = inet_addr("225.0.0.100");
mreq.imr_interface.s_addr = INADDR_ANY;
// add membership/subscribe to group...
status = setsockopt(receivingSocket, IPPROTO_IP,
IP_ADD_MEMBERSHIP,(char*)&mreq, sizeof(mreq));
memset(&clientAddress, 0, sizeof(clientAddress));
length = sizeof(clientAddress);
for(;
{
int numberOfBytesReceived = 0;
numberOfBytesReceived =
recvfrom(receivingSocket,incomingDataBuffer,600,0,(sockaddr*)&clientAddress,&length);
if (numberOfBytesReceived == SOCKET_ERROR)
{
int wsaError = WSAGetLastError();
cout<<"Winsock error ["<<wsaError<<"]"<<endl;
// leave/unsubscribe from multicast group...
status = setsockopt(receivingSocket, IPPROTO_IP,
IP_DROP_MEMBERSHIP,(char*)&mreq, sizeof(mreq));
exit(1);
}
else
{
cout<<endl<<"Received ["<<numberOfBytesReceived<<"]
bytes"<<endl;
// the ID is the first 8 bytes of the header...
int id = atoi(strncpy(new char[32],incomingDataBuffer,8));
// the total number of packets are the 15th and 16th bytes
of the header...
int totalPackets = atoi(strncpy(new char[32],
incomingDataBuffer + 14, 2));
// the sequence number is 13th and 14th byte of the
header...
int sequenceNumber = atoi(strncpy(new char[32],
incomingDataBuffer + 12, 2));
cout<<"Message ID.....: "<<id<<endl;
cout<<"Total Packets..: "<<totalPackets<<endl;
cout<<"Sequence Number: "<<sequenceNumber<<endl;
}
}
closesocket(receivingSocket);
return(0);
}
/*
Sample output:
--------------------
Received [538] bytes
Message ID.....: 0
Total Packets..: 0
Sequence Number: 0
Received [522] bytes
Message ID.....: 0
Total Packets..: 0
Sequence Number: 0
Received [522] bytes
Message ID.....: 0
Total Packets..: 0
Sequence Number: 0
Received [530] bytes
Message ID.....: 0
Total Packets..: 0
Sequence Number: 0
Received [530] bytes
Message ID.....: 0
Total Packets..: 0
Sequence Number: 0
In the MS-Visual C++/-EE-6.0 debugger:
Values before the "recvfrom" call:
-------------------------------------
+ &clientAddress 0x0012fdd0
+ incomingDataBuffer 0x0012fb6c
"ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ"
+ &length 0x0012fdc4
numberOfBytesReceived 0
receivingSocket 2972
Values after the "recvfrom" call:
-------------------------------------
+ &clientAddress 0x0012fdd0
+ incomingDataBuffer 0x0012fb6c ""
+ &length 0x0012fdc4
numberOfBytesReceived 514
receivingSocket 2972
*/
Any ideas why the following code prints zeros for the message id,
number of packets and sequence number as shown in the sample output
below? Somehow I am not being able to access the contents of my
'incomingDataBuffer' array. I have also included some values from the
respective MSVC 6.0 debug session.
Thank you!
-RB
-------------------------------------------------------------------------------
#include<windows.h>
#include<iostream>
#include<string>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main(int argc, char * argv[])
{
WSADATA wsaData;
SOCKADDR_IN serverAddress;
SOCKADDR_IN clientAddress;
SOCKET receivingSocket;
int port = 7797;
int length;
char incomingDataBuffer[600];
int ret = WSAStartup(MAKEWORD(2,0), &wsaData);
if (ret != 0)
{
int errorCode = WSAGetLastError();
cout<<"ERROR ["<<errorCode<<"] initializing WinSock!"<<endl;
exit(1);
}
if (LOBYTE(wsaData.wVersion)!= 2 || HIBYTE(wsaData.wVersion)!= 0)
{
cout<<"Invalid Winsock Version"<<endl;
WSACleanup();
exit(1);
}
if (port < 1)
{
cout<<"Invalid UDP port"<<endl;
exit(1);
}
if ((receivingSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
cout<<"Unable to create socket"<<endl;
memset((char*)&serverAddress,0,sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = htons(INADDR_ANY);
serverAddress.sin_port = htons(port);
int status = bind(receivingSocket,
(sockaddr*)&serverAddress,sizeof(serverAddress));
if (status < 0)
{
cout<<"Error: bind() failed."<<endl;
exit(1);
}
// join multicast group...
struct ip_mreq mreq;
mreq.imr_multiaddr.s_addr = inet_addr("225.0.0.100");
mreq.imr_interface.s_addr = INADDR_ANY;
// add membership/subscribe to group...
status = setsockopt(receivingSocket, IPPROTO_IP,
IP_ADD_MEMBERSHIP,(char*)&mreq, sizeof(mreq));
memset(&clientAddress, 0, sizeof(clientAddress));
length = sizeof(clientAddress);
for(;
{
int numberOfBytesReceived = 0;
numberOfBytesReceived =
recvfrom(receivingSocket,incomingDataBuffer,600,0,(sockaddr*)&clientAddress,&length);
if (numberOfBytesReceived == SOCKET_ERROR)
{
int wsaError = WSAGetLastError();
cout<<"Winsock error ["<<wsaError<<"]"<<endl;
// leave/unsubscribe from multicast group...
status = setsockopt(receivingSocket, IPPROTO_IP,
IP_DROP_MEMBERSHIP,(char*)&mreq, sizeof(mreq));
exit(1);
}
else
{
cout<<endl<<"Received ["<<numberOfBytesReceived<<"]
bytes"<<endl;
// the ID is the first 8 bytes of the header...
int id = atoi(strncpy(new char[32],incomingDataBuffer,8));
// the total number of packets are the 15th and 16th bytes
of the header...
int totalPackets = atoi(strncpy(new char[32],
incomingDataBuffer + 14, 2));
// the sequence number is 13th and 14th byte of the
header...
int sequenceNumber = atoi(strncpy(new char[32],
incomingDataBuffer + 12, 2));
cout<<"Message ID.....: "<<id<<endl;
cout<<"Total Packets..: "<<totalPackets<<endl;
cout<<"Sequence Number: "<<sequenceNumber<<endl;
}
}
closesocket(receivingSocket);
return(0);
}
/*
Sample output:
--------------------
Received [538] bytes
Message ID.....: 0
Total Packets..: 0
Sequence Number: 0
Received [522] bytes
Message ID.....: 0
Total Packets..: 0
Sequence Number: 0
Received [522] bytes
Message ID.....: 0
Total Packets..: 0
Sequence Number: 0
Received [530] bytes
Message ID.....: 0
Total Packets..: 0
Sequence Number: 0
Received [530] bytes
Message ID.....: 0
Total Packets..: 0
Sequence Number: 0
In the MS-Visual C++/-EE-6.0 debugger:
Values before the "recvfrom" call:
-------------------------------------
+ &clientAddress 0x0012fdd0
+ incomingDataBuffer 0x0012fb6c
"ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ"
+ &length 0x0012fdc4
numberOfBytesReceived 0
receivingSocket 2972
Values after the "recvfrom" call:
-------------------------------------
+ &clientAddress 0x0012fdd0
+ incomingDataBuffer 0x0012fb6c ""
+ &length 0x0012fdc4
numberOfBytesReceived 514
receivingSocket 2972
*/