sending structure from client to server
Hi ,
I want to send a structure (user defined) for example :
struct bank{
int empid;
char name[50];
float amount;
} per;
I want to send this from a client to server on unix.Can any1 tell me how to send this(from client) and read the data at the other end(at server).tnx in advance...
Re: sending structure from client to server
Read about CAsyncSocket class.
Here yau have an example:
http://www.codeproject.com/KB/IP/MFCSockets.aspx
Regards
Re: sending structure from client to server
My method I use for my server pretty simple..
Code:
struct Packet {
unsigend short size;
unsigned char opCode;
char* data;
};
Code:
Packet packet;
fill it up...
...
Code:
int BytesToSend = sizeof(Packet);
const char *pBytesToSend = (const char *)&packet;
while (BytesToSend > 0)
{
int BytesSent = send(socket, pBytesToSend, BytesToSend, 0);
if (BytesSent == SOCKET_ERROR)
{
int SockErr = WSAGetLastError();
if (SockErr != WSAEWOULDBLOCK)
{
// handle errors here
break;
}
BytesSent = 0;
}
pBytesToSend += BytesSent;
BytesToSend -= BytesSent;
}
Re: sending structure from client to server
Quote:
Originally Posted by mvksagar45
Hi ,
I want to send a structure (user defined) for example :
struct bank{
int empid;
char name[50];
float amount;
} per;
I want to send this from a client to server on unix.Can any1 tell me how to send this(from client) and read the data at the other end(at server).tnx in advance...
You need to make sure that the structures are aligned on the same boundaries. By default, I believe VC++ (most versions) pack structures on 64 bit boundaries. You need to be sure the receiving unix server has the same structure alignment or change your alignment to match the server using the #pragma pack directive.