Home » Category » Microsoft Visual C & C++

Microsoft Visual C & C++: Dissappearing applications!?!?

202| Fri, 08 Feb 2008 14:36:00 GMT| boywonder| Comments (6)
Hi guys, i'm still a noob. I'm experimenting with the CArchive -> CSocketFile -> CSocket structured transmission method. Here's what i've got:

BOOL CJisMessengerDlg::OnInitDialog()
{
CDialog::OnInitDialog();

. . . . .

UINT nPort = 666;

CSocket sockServ, sockClient, sockRecv;
sockServ.Create(nPort);
sockClient.Create();
sockServ.Listen();
sockClient.Connect("192.168.0.1", nPort);
sockServ.Accept(sockRecv);

CSocketFile fileRecv(&sockRecv), fileClient(&sockClient);

CArchive arInServ(&fileRecv, CArchive::load);
CArchive arOutClient(&fileClient, CArchive::store);

int x = 123;
arOutClient << x;
arInServ >> x;
m_sReceive = x;
return TRUE; // return TRUE unless you set the focus to a control
}

I don't see anything wrong (which doesn't mean much), but when I run the program it disappears. It's still running, because i see it as a process in windows task manager, but it's not on the taskbar or anything. I know the program is ATLEAST initializing because my firewall picked it up. I turned my FW off just to be safe (kinda ironic), but the program did the same thing. I'm stuck. Please help.

Keywords & Tags: dissappearing, applications, microsoft, visual c++, vc

URL: http://www.7prog.com/visual-c-c++/92770/
 
«« Prev - Next »» 6 helpful answers below.
First of all I assume you are creating all these sockets in your OnInitDialog() just for test purposes? Since they will all go outof scope and be destroyed as soon as you return from this function.

Secondly you cannot just call "accept" on a socket.
You really need to derive a new class (Call is CListeningSocket) to listen for new connections.

When CListeningSocket hears a connection attempt it will fire OnAccept() at which point you should create a 'new' instance of CSocket (on the heap by using the new keyword)and then call Accept() passing in the address of this newly created socket. The of course you need a method of keeping a track of all the new sockets you created so you can 'delete' them again later.

There really is no way to do this connect/accept method within single method call like you have tried!

If you use debug mode and set a break point at
CSocketFile fileRecv(&sockRecv), fileClient(&sockClient); then use F11 to step into the code, and F10 to step over each line of code you'll see your programm ASSERT (ie blow up) when it validates the socket handle within sockRecv.

littlebrother | Sun, 11 Nov 2007 02:04:00 GMT |

Bah, i just followed this (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/_core_Windows_Sockets.3a_.Sequence_of_Operations.asp) MSDN article. Guess it's more complicated than they explained. It seemed to make sense in the article, but now i'm lost. Thanks lil'bro

PS, could you post an example of using CListeningSocket?

boywonder | Sun, 11 Nov 2007 02:05:00 GMT |

Ok, so I've created a CSocket derived class named CListenSocket. The overridden OnAccept() is as follows:

void CListenSocket::OnAccept(int nErrorCode)
{
CSocket sockRecv;
sockRecv.Accept(sockRecv);
}

then back at my OnInitDialog() I have the following:

. . . . .
UINT nPort = 666;

CListenSocket sockServ, sockClient;
sockServ.Create(nPort);
sockClient.Create();
sockServ.Listen();
sockClient.Connect("192.168.0.1", nPort);

CSocketFile fileRecv(&sockServ), fileClient(&sockClient);

CArchive arInServ(&fileRecv, CArchive::load);
CArchive arOutClient(&fileClient, CArchive::store);

CString out = "Connected", in;
arOutClient << out;
//arInServ >> in; I will handle the input to the server later because
// this is just to test for a connection
m_sReceive = in;
. . . . .

So should this set up a client to send data (i'll handle the server next)?

boywonder | Sun, 11 Nov 2007 02:06:00 GMT |

(a) You're not handling error conditions from the return values from the socket functions. This should give you some idea of what is going on.
(b) You need to put TRY..CATCH blocks around the << and >> sections using the CArchive. This is very probably your problem. The << and >> sections are probably throwing an exception (I think a CArchiveException is used, but you'll have to look it up in MSDN) and causing the OS to attempt to shut down your program.

Darwen.

darwen | Sun, 11 Nov 2007 02:07:00 GMT |

Yeah, actually, I know that is what is happening, but I couldn't figure out how to use TRY and CATCH. I really would like to be able to handle errors. If someone could post an example or point me to an article that could, I would REALLY appreciate it. MSDN does a poor job of explaining these. Sorry i'm such a noob.

Is this the correct way to use TRY and CATCH:

TRY
{
arInServ >> in;
}
CATCH(CException, e){AfxMessageBox("failed to init");}
END_CATCH

Think i may have been forgetting the END_CATCH

boywonder | Sun, 11 Nov 2007 02:08:00 GMT |

Ok, so i've done some debugging, and come to the conclusion that the CArchive arInServ gives me a generic CFileException error when i use the >> operator. Now if I knew what this meant, I might be able to fix it.

Here's my updated code:
UINT nPort = 666;
CListenSocket sockServ;
ASSERT(sockServ);
if (sockServ.Create(nPort) == 0)
AfxMessageBox("Could not Create() sockServ");
if (sockServ.Listen() == 0)
AfxMessageBox("Could not Listen() on sockServ");

CSocketFile fileRecv(&sockServ);
ASSERT_VALID(fileRecv);
CArchive arInServ(&fileRecv, CArchive::load);
ASSERT_VALID(arInServ);

int in;
if(arInServ.IsBufferEmpty())
{
TRY
{
arInServ >> in;
}
CATCH(CFileException, e)
{
e->ThrowOsError(e->m_lOsError);
}
END_CATCH
}
m_sReceive = in;
UpdateData(0);

boywonder | Sun, 11 Nov 2007 02:09:00 GMT |

Microsoft Visual C & C++ Hot Answers

Microsoft Visual C & C++ New questions

Microsoft Visual C & C++ Related Categories