i was checking out chat applications in c++ and found this
server receives char array and use it to fill in "Buffer" struct with the client number to send it to the rest of clients (it obtain the client number as argument from a thread per client call)
what i found interesting was that
if(recv(sConnect, buffer, sizeof(sbuffer), NULL))
{
memcpy(&sbuffer, buffer, sizeof(sbuffer));
cout << "<Client " << sbuffer.ID << ":> " << sbuffer.Message <<endl;
}
how it could assign a char array (char pointer) to an struct of type Buffer
yeah both are void pointers but what happens behind the scene?
was it because the char array only contains 2 different types it was able to initialize that struct object members with them depending on the type ?
then what if i did the same but with 2 similar types ? ex. 2 ushorts at the same buffer , would it still be able to assign each ? depending on the order of the struct it got ?
i've heard something about that before (about how stupid the wrapper method people use and that there is a better way of getting this done)
sorry for asking too many questions but that saves me load of time to study something else instead of searching :P
Code:
int ServerThread(int ID)
{
Buffer sbuffer;
char* Recv = new char[256];
ZeroMemory(Recv, 256);
// In Send we will copy the content of the struct
// and after this we will send "Send" to the client
char* Send = new char[sizeof(Buffer)];
ZeroMemory(Send, sizeof(Buffer));
for(;; Sleep(10))
{
// Same here!
if(recv(Connections[ID], Recv, 256, NULL))
{
sbuffer.ID = ID;
memcpy(sbuffer.Message, Recv, 256);
memcpy(Send, &sbuffer, sizeof(Buffer));
for(int a = 0; a != ConCounter; a++)
{
if(Connections[a] == Connections[ID])
{
}
else
{
send(Connections[a], Send, sizeof(Buffer), NULL);
}
}
ZeroMemory(Recv, 256);
}
}
return 0;
}
Code:
struct Buffer
{
int ID;
char Message[256];
};
int ClientThread()
{
Buffer sbuffer;
char buffer[sizeof(sbuffer)] = {0};
for(;; Sleep(10))
{
// The server will send a struct to the client
// containing message and ID
// But send only accepts a char as buffer parameter
// so here we need to recv a char buffer and then
// we copy the content of this buffer to our struct
if(recv(sConnect, buffer, sizeof(sbuffer), NULL))
{
memcpy(&sbuffer, buffer, sizeof(sbuffer));
cout << "<Client " << sbuffer.ID << ":> " << sbuffer.Message <<endl;
}
}
return 0;
}
what i found interesting was that
if(recv(sConnect, buffer, sizeof(sbuffer), NULL))
{
memcpy(&sbuffer, buffer, sizeof(sbuffer));
cout << "<Client " << sbuffer.ID << ":> " << sbuffer.Message <<endl;
}
how it could assign a char array (char pointer) to an struct of type Buffer
yeah both are void pointers but what happens behind the scene?
was it because the char array only contains 2 different types it was able to initialize that struct object members with them depending on the type ?
then what if i did the same but with 2 similar types ? ex. 2 ushorts at the same buffer , would it still be able to assign each ? depending on the order of the struct it got ?
i've heard something about that before (about how stupid the wrapper method people use and that there is a better way of getting this done)
sorry for asking too many questions but that saves me load of time to study something else instead of searching :P