|
-
October 11th, 2008, 12:30 PM
#1
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...
-
October 11th, 2008, 06:31 PM
#2
Re: Sending structure from client to server
Pack your structure on 1 byte boundaries. (#include <pshpack1.h> #include <poppack.h>)
Then just cast a pointer to the structure variable to a const char * when you pass it to the send() function.
On the server end, cast a pointer to the receiving variable to a char * when you pass it to the recv() function.
Code:
#include <pshpack1.h>
struct bank{
int empid;
char name[50];
float amount;
};
#include <poppack.h>
void SendBankStruct(SOCKET hSocket,const bank * pBankVar)
{
send(hSocket,reinterpret_cast<const char *>(pBankVar),sizeof(bank),MSG_WAITALL);
}
void ReceiveBankStruct(SOCKET hSocket,bank * pBankVar)
{
recv(hSocket,reinterpret_cast<char *>(pBankVar),sizeof(bank),MSG_WAITALL);
}
Last edited by JamesSchumacher; October 11th, 2008 at 06:36 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|