it's been a while since i made this post, and I want to thank the people that responded. but despite the fact that I have been pointed in the right direction, not everything is clear to me.. I have been trying some stuff, and i get the idea..
Now I have encountered the next problem as I'm trying to send and receive data from a Flash client application that uses XMLSockets. Ill supply the code from the client and the server. I believe the problem lies in the c++ code, but I can be wrong.
I hope someoned can help me once more
Flash Client Code
Code:
public function Main()
{
// socket is created to send and receive data
// Next I create a byteArray that will contain the data to be sent
var byteArray:ByteArray = new ByteArray();
//Add value to byteArray here
byteArray.writeUnsignedInt(1000);
// strTotal is a string for testing purposes
var strTotal:String = new String();
trace("length: " + byteArray.length);
var j:Number = 0;
for (var i:Number = 0; i < byteArray.length; i++)
{
strTotal += byteArray[i];
strTotal += " ";
j++;
if (j == 4)
{
strTotal += "| ";
j = 0;
}
}
trace("strTotal: " + strTotal);
socket.send(byteArray);
}
Result:
length: 4
strTotal: 0 0 3 232 |
Connected
dataHandler data: Hallo
As you can see, I'm sending an unsigned int with the value of 1000. This gets printed as
0 0 3 232
this is the value that is in the array.
binary value =
0000 0000 0000 0000 0000 0011 1110 1000
I have recalculated this to decimal and it is 1000.
Everyone still with me this far?
Now i want to receive this data on my server. This has been made in c++ and I am
using winsock to send and receive data. I have finished making my socket, and all of it works..
My application is waiting for data to be received.
I receive data with the recv() method
Code:
char tempBuffer[128];
int retval = recv(tSocket, tempBuffer, sizeof(tempBuffer), 0);
When I try to show the data on the screen I get wrong information
this is done in the next piece of code
Code:
if (retval == 0)
{
break;
// Connection has been closed
// Keep open maybe doing nothing??
}
else if (retval == SOCKET_ERROR)
{
throw ErrorLogging("Socket error while receiving");
}
else
{
// Handle received data
cout << "Incoming buffer length: " << retval << endl;
cout << "Buffer content: " << tempBuffer << endl;
// Byte 0 & 1 are empty, they are empty here as well
cout << "0: " << (unsigned short)tempBuffer[0] << endl;
cout << "1: " << (unsigned short)tempBuffer[1] << endl;
// For bytes 3 & 4 I try several things, but they all seem to interpret the data wrong
cout << "uint 2: " << (unsigned int)tempBuffer[2] << endl;
cout << "ushort 2: " << (unsigned short)tempBuffer[2] << endl;
cout << "char 2: " << (char)tempBuffer[2] << endl;
cout << "uchar 2: " << (unsigned char)tempBuffer[2] << endl;
cout << "uint 3: " << (unsigned int)tempBuffer[3] << endl;
cout << "ushort 3: " << (unsigned short)tempBuffer[3] << endl;
cout << "char 3: " << (char)tempBuffer[3] << endl;
cout << "uchar 3: " << (unsigned char)tempBuffer[3] << endl;
// This is what i should be getting, and iFirstValue should be 1000..
int iFirstValue = ((unsigned short)tempBuffer[0] << 24) + ((unsigned short)tempBuffer[1]
<< 16) + ((unsigned short)tempBuffer[2] << 8) + (unsigned short)tempBuffer[3];
cout << "First value = " << iFirstValue << endl;
for (int i = 0; i < retval; i++)
{
// Here i show the bytes again,
cout << "Byte " << i << ": " << (unsigned short)tempBuffer[i] << endl;
cout << "Binary byte " << i << ": " << bitset<CHAR_BIT>(tempBuffer[i]) << endl;
}
if (send(tSocket, temp, retvalue + 1, 0)==SOCKET_ERROR)
{
throw ErrorLogging("Socket error while sending.");
}
else
{
//Success in sending
cout << "Sending is a succes." << endl;
}
Here I have an image of the output that is made.
http://img267.imageshack.us/img267/8079/outputx.jpg
Can someone plz explain why i cannot get the correct value that is sent.. I seem to be missing something or doing something wrong
many thanks in advance