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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MFC Sockets + Console App 2

Status
Not open for further replies.

apatterno

Programmer
Nov 26, 2002
368
US
I would like to use the CAsyncSocket class for socket support in a console app that links to MFC. However, after creating the socket and binding it to a port, the console program terminates. I understand why, because the main function is allowed to exit.

I guess I need some kind of while loop to act as a message pump for the console program.

So my question(s) are:

1. What do I put inside the while loop, to keep the service running until it is terminated, and to allow the framework to call the OnConnect, OnReceive, OnClose, etc. of CAsyncSocket?

2. (related) How do I keep the while loop from using up the entire time slice-- and thus bringing the system to a crawl

TIA
Will
 
the service should keep running, i am currently writting a program with CAsyncSocket as well, the program does not terminate, are you using visual c++? the program should not terminate unless you pass the IDOK function or you close the program yourself.. visual c++ is an event driven environment. maybe i misunderstood the situation please clarify and i will help you as soon as i can, i have been working with ASyncSocket class for about a year now.
 
This is a console program, i.e., its entry point is main, not WinMain.

My main pseudocode (abridged):

[tt]int _tmain(int argc, _TCHAR **argv, _TCHAR **envp)
{
AfxWinInit:):GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0);

g_pServerSocket = new CServerSocket;
g_pServerSocket->Create(1125,SOCK_STREAM);
g_pServerSocket->Listen();

while (?????) {
// What do I put here to keep the
// program from reaching the end of the function??

}

return 0;
}[/tt]

Since this isn't a normal Windows application, it doesn't make sense to put the traditional [tt]while (GetMessage(...))[/tt] here. (Since there are no actual windows in the program)
 
well if all you wanna do is prevent the program from reaching an end you can simply declare and integer and set it to a value.. say the value is 0. and then you can do this:

while(integer != 1)
{
// here is where the actual program code
// that does stuff will be

// you will have to embed an if statement at the
// end of all the code to check whether the user
// decided to quit the program (in other words
// whether the user set integer to 1, somehow)
}

so heres an example of how this can be done.. this isnt the entire program but here we go..

main()
{
int intSum;
int temp = 0;

while(temp != 1)
{
listenSocket();
intSum = 1 + 1;
cout << &quot;hi welcome&quot; << endl;
// blah blah blah, more code

cout << &quot;what would you like to do? please make selection&quot; << endl;
cout << &quot;1 to quit&quot; << endl;
cout << &quot;2 to do something&quot; << endl;
cout << &quot;3 to do something else&quot; << endl;
cin >> temp;
}

}
 
Will,

It appears you want a &quot;Socket Server&quot; based on the listen() call your making. So in your while look you want to call accept() which will block until a connection is received at which time it returns the socket that has been connected.

Then of course you perform your read/write operations on the socket.

Now the interesting part is that if you perform read/write operations on the connection socket inside your while() loop in your main thread, you of course cannot accept any more connections to your server.

A typical &quot;threaded&quot; server executes each socket connection &quot;Session&quot; in a separate thread, or an even more complex technique is to use a &quot;thread pool&quot; for the session read/write operations.

Let me know if that is to confusing or whatever.
-pete
 
okay, point taken. But how do callback functions get called, if my program is stuck in the while loop all the time?

Again...

[tt]void CServerSocket::OnAccept()
{
// ...
}

int _tmain(int argc, _TCHAR **argv, _TCHAR **envp)
{
AfxWinInit:):GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0);

g_pServerSocket = new CServerSocket;
g_pServerSocket->Create(1125,SOCK_STREAM);
g_pServerSocket->Listen();

// program will terminate on a remote request,
// so while(TRUE) should do for now

while (TRUE) {
// What do I put here to keep the
// program from reaching the end of the function...
// but at the same allowing CServerSocket::OnAccept
// to be called when a new connection occurs???

}

return 0;
}[/tt]

Is it a mistake to be using MFC's CAsyncSocket, do I need to use the Windows Sockets functions directly instead? I have no experience with this but I guess I'll learn it if I have to.
 
Sorry palbano, my previous message was posted before I saw yours.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top