| | |
Data loss in Type Conversion.
![]() |
•
•
Join Date: Sep 2004
Posts: 22
Reputation:
Solved Threads: 0
Hello Everyone,
I'm working on a couple of programs, client/server. I'm using winsock library to handle data transfer between the two programs. I'm having a problem when it comes to sending int values, they must be converted to (char *) since that's the data type of the buffers used in the socket Send() and Recv() functions. to elaborate, consider the following:
Client side:
-----------
unsigned char rnd[4];
int noncer =0;
// Generating a random number
RAND_pseudo_bytes(rnd,sizeof(rnd));
noncer = (int)rnd;
non1=(char*)noncer;
// These two lines produce the same output which is 1244556
cout <<"\n Noncer :" << noncer << "\n";
cout.flush();
cout <<"\n Noncer char :" << (int)non1 << "\n";
cout.flush();
SendData(Socket, non1);
Server Side:
-----------
int client_nonce;
char String[50];
String_initialization(String);
// Reading the nonce identifier
RecieveMessage(socket, String, number_of_bytes);
client_nonce = (int)String;
// this results in 1244588 !!
cout<<"\n the nonce is:"<<client_nonce <<endl;
cout.flush();
If anyone can tell me what's causing the change of data i'd really appreciate it.
Thank you all in advance.
Layla.
I'm working on a couple of programs, client/server. I'm using winsock library to handle data transfer between the two programs. I'm having a problem when it comes to sending int values, they must be converted to (char *) since that's the data type of the buffers used in the socket Send() and Recv() functions. to elaborate, consider the following:
Client side:
-----------
unsigned char rnd[4];
int noncer =0;
// Generating a random number
RAND_pseudo_bytes(rnd,sizeof(rnd));
noncer = (int)rnd;
non1=(char*)noncer;
// These two lines produce the same output which is 1244556
cout <<"\n Noncer :" << noncer << "\n";
cout.flush();
cout <<"\n Noncer char :" << (int)non1 << "\n";
cout.flush();
SendData(Socket, non1);
Server Side:
-----------
int client_nonce;
char String[50];
String_initialization(String);
// Reading the nonce identifier
RecieveMessage(socket, String, number_of_bytes);
client_nonce = (int)String;
// this results in 1244588 !!
cout<<"\n the nonce is:"<<client_nonce <<endl;
cout.flush();
If anyone can tell me what's causing the change of data i'd really appreciate it.
Thank you all in advance.
Layla.
Don't play around with casting like that unless you know what you're doing. Type punning can be tricky business, but barring transmission issues with your sockets, this should work:
C++ Syntax (Toggle Plain Text)
#include <cstring> #include <iostream> using namespace std; int main() { int i1 = 12345; char buffer[sizeof ( int )]; memcpy ( buffer, (unsigned char *)&i1, sizeof ( int ) ); // Simulate send/recv int *pi = (int *)buffer; cout<< *pi <<endl; }
![]() |
Similar Threads
- News Story: BNP data loss (Existing Scripts)
- error: expected constructor, destructor, or type conversion before '<' to (C++)
- News Story: Worse government data loss ever caused by a bit of a muddle say police (Network Security)
- A doubt in Type Conversion Program (C++)
- type conversion (C)
Other Threads in the C++ Forum
- Previous Thread: <string>
- Next Thread: Validation Program
| Thread Tools | Search this Thread |
api array based binary bitmap business c++ c/c++ char class classes code codesamplerunwhilecommands coding commentinghelp compile console conversion count decide delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error faq file forms fstream function functions game givemetehcodez graph guess gui hash homeworkhelp homeworkhelper iamthwee ifpug ifstream incrementoperators infinite input int integer java lib linkedlist linker listing loop looping loops map math matrix memory multiple news node output pointer port problem proficiency program programming project python random read recursion reference rpg string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets





