CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2008
    Posts
    5

    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...

  2. #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
  •  





Click Here to Expand Forum to Full Width

Featured