CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2006
    Posts
    9

    Inheritance and using static variables

    code begin:

    connection.h // base class

    class Connection{

    private:
    static int clientSockfd;
    static int serverSockfd;

    public:
    Connection() {};
    int getClientSockfd{return clientSockfd;};
    int getServerSockfd{return serverSockfd;};
    void setClientSockfd(int val) {clientSockfd=val;};
    void setServerSockfd(int val) {serverSockfd=val;};
    };

    client.h // client class

    class Client : public Connection {

    public:
    Client(){};
    void processMessageMT6523();
    };

    client.cpp //

    int main(....) {

    Client *myClient;
    int sockfd;

    sockfd = socket(.....);

    myClient->setClientSockfd(sockfd);

    // I put this statement in to debug...at this point server will have already
    kicked of and should have set it's sockfd via the "setServerSockfd" I
    just wanted to print out the value i always get 0 which is wrong.

    cout << "Server Sockfd --> " << myClient->getServerSockfd() << endl;

    }

    server.h // server class

    class Server : public Connection {

    public:
    Server(){};
    void processMessageMT4590;
    };

    server.cpp //

    int main(....) {

    Server *myServer;
    int sockfd;

    sockfd = socket(.....);

    myServer->setServerSockfd(sockfd);

    }

    code end:



    I kick off the server first so it can set its sockfd, but when i try to access "serverSockfd" via the "myClient->getServerSockfd()" i get 0.

    What am i missing?

  2. #2
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: Inheritance and using static variables

    Hi,

    please use code tags when posting source code...

    What you´re obviously missing is an instance of both myServer and myClient. You declare pointers to them but you never create an instance.
    When I understood your code correctly you´re running two separate processes (because both myclient.cpp and myserver.cpp contain a main function). Two processes have always two separate address spaces, so myClient in process A has no access to myServer in process B. Accessing a variable in another address space needs IPC (InterProcess Communication), what is often implemented by TCP/IP (now the circle closes ).
    Btw:
    What do you need the socket descriptors for? Your client does not need the sd of the server, and the server does not need the client´s one. I´m a little bit confused about what you´re trying to do.

    Regards,
    Guido
    - Guido

  3. #3
    Join Date
    Mar 2006
    Posts
    9

    Re: Inheritance and using static variables

    Sorry about not using tags....I'm writing a gateway between the client and server...(i.e. im writeing my own client and server)...so my client needs to know my servers fd so it can send messages via the server fd.

    btw: I created an instance of both myClient and myServer and i still get the same output. Do you have any other ideas on what im doing wrong?

    I realize they each have there own space, but i thought the connection(base class) was a common space for both. At least the static part. How would myClient and myServer access each others fd?


    Thanks...
    Last edited by tsnofvdr; March 14th, 2007 at 12:53 PM.

  4. #4
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: Inheritance and using static variables

    Hi again,

    sorry, but I do not understand what you are trying to do. You have a client that connects to a server (myClient and myServer). That server forwards the data coming from your client to another server (3rd Party) and redirects the answer back to the client.

    myClient <--> myServer <--> 3rd Party Server

    Am I right?

    Guido
    - Guido

  5. #5
    Join Date
    Mar 2006
    Posts
    9

    Re: Inheritance and using static variables

    guido,

    Here is the flow:

    3rd party Client->myServer->3rd party Server

    3rd party Server->myClient->3rd party Client

    My "myClient & myServer" is stuck in the middle of your normal client server model.

    Like you stated before, I think the problem im running into is that I have two main programs one for "myClient' and one for "myServer". I thought becasue i set my "clientSockfd & serverSockfd" to static that each object would have the ability to access the data.

    I guess to accomplish my task i will have to create separate threads off of a single main?

    Thanks

  6. #6
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: Inheritance and using static variables

    Ah, now I got it.

    IMHO it´s not necessary to split your application in two, because the server takes the role of both server (for 3rd party client) and client( for 3rd party server). This is called middleware.

    - do you have to translate protocols or is it simply passing-through?
    - how many clients are allowed to connect to your server simultaneously?

    Regards,
    Guido
    - Guido

  7. #7
    Join Date
    Mar 2006
    Posts
    9

    Re: Inheritance and using static variables

    I have to byte swap on the incoming message then just send it back out. Only one client will be connecting.

    Thanks

  8. #8
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: Inheritance and using static variables

    There´s a beginner´s TCP/IP server and client example on codeproject. Take a look at Simple TCP client and Simple TCP server

    Good luck,
    Guido
    - Guido

  9. #9
    Join Date
    Mar 2006
    Posts
    9

    Re: Inheritance and using static variables

    Thanks for you help.

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