Thread: Send data with class

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    Send data with class

    I get until
    TcpClientObject.SendToSocket((char*)&DataObject, sizeof(DataObject));
    but then the data sent is like a memory address of 7 numbers.

    I think its maybe sizeof(DataObject), because DataObject is a class and its size
    is not the same as the size of a, b, c?

    Thanks!

    Code:
    #include <windows.h>
    #include <iostream>
    #pragma comment(lib,"ws2_32.lib")
    #pragma comment(lib,"user32.lib")
    
    using namespace std;
    
    class Data
    {
    public:
    int a;
    int b;
    int c;
    protected:
    private:
    
    };
    
    class TcpClient
    {
    public:
        void InitWinsock()
        {
            Result = WSAStartup(MAKEWORD(2, 2), &WSAdata);
            if(Result != 0)
            {printf("%s\n","Setting Up Winsock Client Failed!"); WSACleanup();}
            else
             printf("%s\n","Setting Up Winsock Client Success!");
    
        }
    
        void SetAddressInfo()
        {
            memset(&sins, 0, sizeof(sins));
            ConnectSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
            sins.sin_family = AF_INET;
            sins.sin_port = htons(80);
            sins.sin_addr.s_addr = inet_addr("127.0.0.1");
        }
    
        void ConnectInfo()
        {
           if(connect(ConnectSock, (sockaddr*)&sins, sizeof(sins))== SOCKET_ERROR)
                       {printf("%s\n","Failed To Connect!"); WSACleanup();}
    
           else        printf("%s\n","Connected To Server!");
    
        }
    
        void SendToSocket(char* Data, int size)
        {
            while(1)
            {
              // printf("%s\n","Enter Something To Send!");
              // cin >> Something;
               Result = send(ConnectSock,Data,size,0);
               if(Result == -1) {printf("%s\n","Failed To Send Data!"); WSACleanup(); break;}
               else{printf("Data Sent! %i\n", Data); Sleep(3000);}
            }
        }
    
    protected:
    
    private:
        WSADATA WSAdata;
        SOCKET ConnectSock;
        sockaddr_in sins;
        int Result;
        char Something[64];
    
    
    };
    /**********************************************************************************/
    /******************************THE  SERVER****************************************/
    class TcpServer
    {
    public:
        void InitWinsock()
        {
            Result = WSAStartup(MAKEWORD(2, 2), &WSAdata);
            if(Result == -1)
            {printf("%s\n","Setting Up Winsock Server Failed!"); WSACleanup();}
            else
            {printf("%s\n","Setting Up Winsock Server Success!");}
    
        }
    
        void SetAddressInfo()
        {
            memset(&sins, 0, sizeof(sins));
            ListenSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
            sins.sin_family = AF_INET;
            sins.sin_port = htons(80);
            sins.sin_addr.s_addr = inet_addr("127.0.0.1");
        }
    
        void BindnListen()
        {
            if( bind( ListenSock, (struct sockaddr*)&sins, sizeof( struct sockaddr)) == SOCKET_ERROR ){
                      printf("%s\n","Failed To Bind To Address!");WSACleanup();}
            else      printf("%s\n","Binded To Address Success!");
    
            if(listen(ListenSock, 1)==SOCKET_ERROR){
                      printf("%s\n","Failed To Listen On Address!"); WSACleanup();}
            else      printf("%s\n","Listening On Address!");
    
        }
    
        void WaitForConnections()
        {
            while(1)
            {
                printf("%s\n","Waiting For Connections!");
                int clnt = sizeof(sins);
    
                AcceptSock = accept(ListenSock ,(struct sockaddr*)&sins, &clnt);
                if(AcceptSock == INVALID_SOCKET)
                {
                    printf("%s\n","No Connection Made Yet!");
                }
                else
                {
                    printf("%s\n","Connection Made!");
                    break;
                }
            }
        }
    
        void RecieveFromSocket()
        {
            while(1)
            {
                if(AcceptSock != INVALID_SOCKET)
                {
                    recv(AcceptSock,(char*)&DataObject,sizeof(DataObject),0);
                   
                    printf("%d\n",DataObject.a);
                    printf("%d\n",DataObject.b);
                    printf("%d\n",DataObject.c);
                }
                else
                {
                    printf("%s\n","No Connection Yet!");
                }
            }
        }
    
    protected:
    
    private:
        WSADATA WSAdata;
        SOCKET ListenSock;
        SOCKET AcceptSock;
        sockaddr_in sins;
        int Result;
        char RecvBuffer[512];
        Data DataObject;
    
    };
    
    /**********************************THE SERVER END********************************/
    
    int main(int argc, char* argv[])
    {
    
        Data DataObject;
        DataObject.a = 50;
        DataObject.b = 100;
        DataObject.c = 200;
        
        TcpClient TcpClientObject;
        TcpClientObject.InitWinsock();
        TcpClientObject.SetAddressInfo();
    
    
        TcpServer TcpServerObject;
        TcpServerObject.InitWinsock();
        TcpServerObject.SetAddressInfo();
        TcpServerObject.BindnListen();
    
        TcpClientObject.ConnectInfo();
        TcpServerObject.WaitForConnections();
    
        TcpClientObject.SendToSocket((char*)&DataObject, sizeof(DataObject));
        TcpServerObject.RecieveFromSocket();
    
        WSACleanup();
        return 0;
    }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    You are sending the memory address of the class, not the class itself.

    You would basically need to send the info to serialize to a class. Maybe name / value pairs or something like that.

    Code:
    SendToSocket("a=10&b=15&c=20", lengthOfString);
    Something like that. You don't want to take the memory address though for sure. It isn't doing what you think it is.
    Woop?

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thank you prog-bman!

    I dont see right now how to solve this because if i send the info directly why would i need the class?
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    No... I think.

    Try taking the loop out of the send function. The receive function is never called in your code, cause of while(1). You'll either need to create two programs to test this (one client, one server) or create a separate thread to test it all either a client or server thread, whichever you want.

    My output (with a break on success of send in the send function):
    Quote Originally Posted by myProgram
    Setting Up Winsock Client Success!
    Setting Up Winsock Server Success!
    Binded To Address Success!
    Listening On Address!
    Connected To Server!
    Waiting For Connections!
    Connection Made!
    Data Sent! 1308984
    50
    100
    200

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Wow you're right its working!

    I just added the 2 break; where you suggested and its working without creating a thread or two separate programs.

    Its interesting though as to why would it print out the memory address and the 3 datas too
    when there are only 3 printf() in the RecieveFromSocket() function.

    Thank you twomers!
    Using Windows 10 with Code Blocks and MingW.

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Not really. You're printf("Data Sent! %i\n", Data)-ing... Data is a char* type. If you were to dereference it and overload the operator<< method it would print the right values.

    If you do a
    Code:
    class Data
    {
    public:
      int a;
      int b;
      int c;
      void print(){printf( "%d, %d, %d\n", a, b, c ); };
    protected:
    private:
    
    };
    with a
    Code:
    printf("Data Sent! \n", Data); ((::Data*)Data)->print();
    in the send function it'll print fine for ya... My output...
    Setting Up Winsock Client Success!
    Setting Up Winsock Server Success!
    Binded To Address Success!
    Listening On Address!
    Connected To Server!
    Waiting For Connections!
    Connection Made!
    Data Sent!
    50, 100, 200
    50
    100
    200

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Ah ok, i understand now, thank you!
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help on class coupling
    By andrea72 in forum C++ Programming
    Replies: 4
    Last Post: 04-17-2011, 10:16 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. binary tree token problem
    By OldSchool in forum C++ Programming
    Replies: 13
    Last Post: 05-28-2006, 10:42 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM