CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  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
    Join Date
    Feb 2005
    Location
    Madrid (Spain)
    Posts
    511

    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

  3. #3
    Join Date
    Dec 2006
    Location
    New York Brooklyn
    Posts
    120

    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;
    }

  4. #4
    Join Date
    Feb 2005
    Posts
    2,160

    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.

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