|
if I have to pass a runtime class to a function:
RUNTIME_CLASS(CMyForm)
does this mean i have no way of setting initial values for this class? this class needs to take in a filename, as well as some other parameters.
is the only way to do this by setting global variables before passing the runtime class, and then having the runtime class read from the global variables? This sounds like a bad idea for OOP, is there a better way?
thanks in advance!
-dz
|
|
|
|
|
Hi!
Could anybody suggest a good book on C++ which concentrates on classes, templates and other cool c++ specific stuff? I need an intermediate level book, not for a beginner and not for a C++ expert either.
Thanks,
NC
|
|
|
|
|
Do you know Bruce Eckel's Thinking in C++? I don't know if it's your target type of book, but as it is downloadable for free you might want to have a look at it.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Hi!
Thanks for replying.
I just looked at the table of contents. I was looking for something like that itself. Would you happen to know of any books with emphasis on Templates and STL?
Thanks,
NC
|
|
|
|
|
Then you probably want Stroustrup's "The C++ programming language"
I myself think the book is rather boring, and it is definitly not something you want to read straight through. It is, however, probably the best book if you really want to understand the language, and it's a great reference.
/moliate
|
|
|
|
|
Hi,
Try "C++ How to Program", Deitel & Deitel. Probably covers most of what you need.
|
|
|
|
|
Please read the following sample code:
In CTestDlg....
BOOL CTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
.
.
.
// TODO: Add extra initialization here
mp_Server = (CServer*)
AfxBeginThread(RUNTIME_CLASS(CServer), THREAD_PRIORITY_NORMAL,
0, CREATE_SUSPENDED);
mp_Server->ResumeThread();
return TRUE; // return TRUE unless you set the focus to a control
}
void CTestDlg::OnOK()
{
// TODO: Add extra validation here
char buff[1024];
strcpy(buff, "This is a test of Simple Socket");
CSocket Client;
Client.Create();
Client.Connect("127.0.0.1",5001);
Client.Send(buff,strlen(buff));
}
In CServer.....
int CServer::Run()
{
// TODO: Add you specialized code here and/or call the base class
char buff[1024];
strcpy(buff, "This is the look before receive the socket");
CSocket socket;
if ( svrSocket.Accept(socket))
AfxMessageBox("Socket is created Succesfully");
socket.Receive(buff,sizeof(buff))
AfxMessageBox((CString)buff);
return 0;
}
This is a sample code for CSocket (posted by my professor in university). But I don't quite understand how CSocket works. When I execute the program, a dialog pop up, and a message box "This is the look before receive the socket" pop up. When I click OK button, it will pop up "Socket is created Successfully" and then "This is a test of Simple Socket".
However, when I click the OK button one more time, there is no response!!! I can put a AfxMessageBox() in OnOK(), and I found that there is execution in OnOK() at that time (messagebox pop up). Why Run() doesn't do anything as I did send the socket to there.....
Please help!!!!
|
|
|
|
|
It's difficult to say without the full source code, but could it be that the thread has terminated, so it can not respond to the "Connect..." ?
in other words, once the run exits, the thread disappear, and there is no more server .
this line:
if ( svrSocket.Accept(socket))
is probably blocking your thread waiting for a connection, thats the reason it only works once.
Andres Manggini.
Buenos Aires - Argentina.
|
|
|
|
|
Thanks for your reply!!
So, if i add a loop to contain "if ( svrSocket.Accept(socket))", then i can ask Run() to keep checking if there is more socket arrived.......is that right?
Also, how the parameter in this function "Client.Connect("127.0.0.1",5001);" come from?
ONe more question, can the Run() in CServer can block the process in OnOK() after Run() receives a socket from OnOK(). For example, OnOK() sends a socket to Run() and "if ( svrSocket.Accept(socket))" receives it. Because how some information from this socket(or packet), Run() has to block (or pause) the process in OnOK()....can it do happen?
|
|
|
|
|
ChiYung wrote:
So, if i add a loop to contain "if ( svrSocket.Accept(socket))", then i can ask Run() to keep checking if there is more socket arrived.......is that right?
Yes, that's correct.
ChiYung wrote:
Also, how the parameter in this function "Client.Connect("127.0.0.1",5001);" come from?
first string is the IP Address, since the computer is connecting to itself, it's using 127.0.0.1 which is the same as localhost (or THIS computer
ChiYung wrote:
ONe more question, can the Run() in CServer can block the process in OnOK() after Run() receives a socket from OnOK(). For example, OnOK() sends a socket to Run() and "if ( svrSocket.Accept(socket))" receives it. Because how some information from this socket(or packet), Run() has to block (or pause) the process in OnOK()....can it do happen?
I am not quite sure to understand the question, but i'll tell you this, if
CServer is running on a different thread it shouldn't block OnOK(), but, OnOK will be blocked cause of :
CSocket Client;
Client.Create();
Client.Connect("127.0.0.1",5001);
Client.Send(buff,strlen(buff));
So, OnOK will be blocked until Client.Send(..) returns.
Hope this clarifies a bit, if you have more questions, post them here.
Andres Manggini.
Buenos Aires - Argentina.
|
|
|
|
|
THANK YOU VERY MUCH!!!!! YOU GAVE ME LOTS OF HINTS!!!!
Andres Manggini wrote:
I am not quite sure to understand the question, but i'll tell you this, if
CServer is running on a different thread it shouldn't block OnOK(), but, OnOK will be blocked cause of :
CSocket Client;
Client.Create();
Client.Connect("127.0.0.1",5001);
Client.Send(buff,strlen(buff));
So, OnOK will be blocked until Client.Send(..) returns.
Ok....how can i prevent Client.Send(..) returns..... is it suspend the thread of Run()...or......
Maybe I give you more detail....
I need to implement a DBMS program that simulate the Lock management. I use socket in CServer to implement the scheduler that handle the lock. It is one thread...... then there are hundreds of users to "access" the database object, so there are hundreds of threads. Each threads(uses) requires to get a lock permit from scheduler (CServer) in order to "access' the DB Object. This is what i'm going to do.
Why I need to block some process, because for example, thread A is trying to access the DB Object X, while thread B is reading something in Object X. Scheduler (CServer) requires to "block" thread A until thread B finished. At this time, thread A will be put in "waitqueue" (this is another issue).
|
|
|
|
|
ChiYung wrote:
THANK YOU VERY MUCH!!!!! YOU GAVE ME LOTS OF HINTS!!!!
Great .
ChiYung wrote:
Ok....how can i prevent Client.Send(..) returns..... is it suspend the thread of Run()...or......
Do you mean prevent Client.Send even after all the data has been sent ?
Cause, if you just want to prevent the return UNTIL all the data has been sent, that's already how it works, so don't have to do anything.
Andres Manggini.
Buenos Aires - Argentina.
|
|
|
|
|
Andres Manggini wrote:
Do you mean prevent Client.Send even after all the data has been sent ?
Cause, if you just want to prevent the return UNTIL all the data has been sent, that's already how it works, so don't have to do anything
I think I want the CServer to....let say suspend awhile (e.g. 20s) before the OnOK() receives the return signal from Client.Send()...........(it can make the OnOK() to suspend 20s too....right?
Sorry...I know I'm stupid.....;P
|
|
|
|
|
I don't know of you can do that just with the CSocket methods, i guess you'll have to implement a form on synchronization between the server and the clients..
maybe using socket as well, set up a small protocol between server and client to communicate (protocol as defined messages sent to each other)
not sure this is the solution for you here..
And.. you're no stupid, far from it .
Regards,
Andres Manggini.
Buenos Aires - Argentina.
|
|
|
|
|
To avoid blocking sockets you might want to use CAsyncSocket in place of CSocket.
Nish
One Nish is trouble enough.
Imagine 16 of me, dancing wild...
:jig::jig::jig::jig:
:jig::jig::jig::jig:
:jig::jig::jig::jig:
:jig::jig::jig::jig:
|
|
|
|
|
Anyone know how I can take a dialog box with 3 buttons that have no function at all and change the background colours of 2 of them to red and one gray??
Thanks.
|
|
|
|
|
Try adding an OnCtlColor (if MFC) handler to the dialog and returning a brush of the desired color based on the control IDs...
|
|
|
|
|
I'm not that experienced in visual c++ programming.. could you please tell me the syntax of how to write all that?
Thanks
|
|
|
|
|
Or you could try using CButtonST v3.4
http://www.codeproject.com/buttonctrl/cbuttonst.asp
Nish
One Nish is trouble enough.
Imagine 16 of me, dancing wild...
:jig::jig::jig::jig:
:jig::jig::jig::jig:
:jig::jig::jig::jig:
:jig::jig::jig::jig:
|
|
|
|
|
Actually, I was asleep at the switch in that reply - but if you search the articles here for 'owner draw' you'll get lots of info on what can be done.
|
|
|
|
|
Tim Deveaux wrote:
Actually, I was asleep at the switch in that reply
Still feelin sheepish abt that eh, Tim?
Nish
One Nish is trouble enough.
Imagine 16 of me, dancing wild...
:jig::jig::jig::jig:
:jig::jig::jig::jig:
:jig::jig::jig::jig:
:jig::jig::jig::jig:
|
|
|
|
|
Ruined my whole day
Now, about this '16 Nish' concept...
|
|
|
|
|
This wont work for buttons anymore...
Nish
One Nish is trouble enough.
Imagine 16 of me, dancing wild...
:jig::jig::jig::jig:
:jig::jig::jig::jig:
:jig::jig::jig::jig:
:jig::jig::jig::jig:
|
|
|
|
|
Yes - I goofed - need to have an owner draw button...
|
|
|
|
|
You can use a CBitmapButton
Nish
One Nish is trouble enough.
Imagine 16 of me, dancing wild...
:jig::jig::jig::jig:
:jig::jig::jig::jig:
:jig::jig::jig::jig:
:jig::jig::jig::jig:
|
|
|
|