Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C++ sockets and functions connection persistence 1

Status
Not open for further replies.

ksoong

Programmer
Oct 3, 2000
57
CA
I am programming a class that has multiple functions inside of it.

I want this class to have an

openconnection() function and various other functions that depend on the connection that is opened in the openconnection() function.

my problem is that the connection is opening fine in the openconnection() function but it is not persisting. it dies right when the openconnection function returns its value.

so the other functions that do any recv and sending don't work.

can anyone tell me how to keep a connection to stay open between function calls?
 
Yes, you can do 1 of 3 things. By the way, your problem is due to the fact that the connection is going out of scope...

1. Declare a connection-type variable, and have your openconnection return a connection-type variable. Then, connect and return the actual connection in your main routine.

2. Declare a connection-type variable as a member of your class, so that each object has a connection that you can modify. This would probably be the pure OO approach to the deal.

3. Pass the openconnection(...) a reference to an already-delcared connection variable and then open it. This is the same as returning a value, kind of.

IF you need more explanation, post your code and I"ll show you what I'm talking about.

MWB>


Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Hi there. Thanks so much for the help. I sorta understand what you mean, except there are some questions i still have.

Probably the best way for you to help me is for you to view the code itself.

you can go to:

and

As you will see, all the declaration for socket structures is made in Efilter.h.

also, in the openconnection() member function, the first recv inside the openconnection() function receives fine. But once it branches to the receive() function the connection is lost.

please advise how this can be changed. and when you talk about a "connection-type" variable, what do you mean? can you point me to my connection-type variable in the Efilter code?

thanks again!
 
The only thing that I can think of is to put the code to connect to the server in your constructor. When I say connection-type, I mean an object that encapusulates the connection to the server, which can be passed like an int or char variable. There should be one. I don't exactly know what it is. In mysql, for example, you can say:

connection conn1 = new connection(server, uname, pword);
query(conn1, "Select * from mike.budget");

Thus, you are passing the connection and the select statement, so the sub query uses the connection to run the query, both of which are passed to it. You may have to hunt around to find it, but you could write a sub like:

connection openconnection(/*args...*/){
/* Code to connect */
/* ... */
return(connection_variable);
}

That's what I mean.

struct sockaddr_in server; may be it, but I'm not farmiliar with what you're doing, so I'm not sure. I hope that this is more clear.

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top