-
March 14th, 2007, 11:54 AM
#1
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?
-
March 14th, 2007, 12:26 PM
#2
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
-
March 14th, 2007, 12:43 PM
#3
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.
-
March 15th, 2007, 03:42 AM
#4
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
-
March 15th, 2007, 08:25 AM
#5
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
-
March 15th, 2007, 08:47 AM
#6
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
-
March 15th, 2007, 09:03 AM
#7
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
-
March 15th, 2007, 09:40 AM
#8
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
-
March 15th, 2007, 09:43 AM
#9
Re: Inheritance and using static variables
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
|