Click here to Skip to main content
15,905,963 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: MAPI Pin
David Crow4-May-04 4:57
David Crow4-May-04 4:57 
GeneralRe: MAPI Pin
asv4-May-04 19:06
asv4-May-04 19:06 
GeneralMFC Exntension-DLL Resource-Access question Pin
ohadp4-May-04 3:01
ohadp4-May-04 3:01 
GeneralRe: MFC Exntension-DLL Resource-Access question Pin
Blake Miller6-May-04 8:18
Blake Miller6-May-04 8:18 
GeneralConvert HDC to long Pin
Kannan Kalyanaraman4-May-04 3:00
Kannan Kalyanaraman4-May-04 3:00 
GeneralRe: Convert HDC to long Pin
Anonymous4-May-04 3:12
Anonymous4-May-04 3:12 
GeneralRe: Convert HDC to long Pin
Kannan Kalyanaraman4-May-04 3:35
Kannan Kalyanaraman4-May-04 3:35 
GeneralO.O All my knowledge about asynchronous sockets and message pumps broken.. help.. Pin
Kuniva4-May-04 2:09
Kuniva4-May-04 2:09 
Ok, i have a very serious problem (allthough it is probably just due to something rediculous i overlooked like these problems usually are). I have been programming with winsock and windows for a few years now so i like to think i know what i'm doing. During these years i have always kind of avoided MFC, so now i thought, why not make it easy on myself and use MFC.

This is the situation, and i will try to sketch it as clearly as possible:

I have an MFC dialog based application, one main dialog window based on the CDialog class and then i have a socket class based on CSocket in which i override the OnClose(), OnConnect(), OnReceive() and OnSend() methods which all call a function in my main Dialog class (using a parent pointer which i set in OnInitDialog() of my main dialog). Now.. the application needs to connect to a server when a button is pressed or whatever, but because i want to be able to tell when a connection fails (either refused or timed out) i keep the socket in blocking mode (which is the default for CSockets anyway) and try to connect. Then when the Connect() method succeeds (returns TRUE) then i use AsyncSelect() on the socket to register to right notifications (FD_CLOSE, FD_CONNECT, FD_READ and FD_WRITE). So of course OnConnect() will never be called, thats obvious, because the connection was established when the socket was still in blocking mode. But the OnReceive() event should work, and it does. Only the ABNORMALLY WEIRD (to me anyway) thing is.. well after the connection succeeded i send a string to the server, and the server replies with two strings (each command is terminated by '\n') which i parse in OnReceive() like this:

char pText[4096];
CString strTemp;
    //MessageBox("msg1"); MESSAGE 1
int nResult = m_hSocket.Receive(pText, sizeof(pText));
//MessageBox("test"); MESSAGE 2
if (SOCKET_ERROR == nResult)
{
    MessageBox("An error occured while receiving data...",NULL,MB_ICONERROR);
}
else
{

    pText[nResult] = NULL;

    strTemp = CString(pText);
    //MessageBox((LPCTSTR)strTemp);

    for(int i=0;i<strTemp.GetLength();i++)
    {
        if(strTemp[i]=='\n')
        {
            OnReceiveCommand(szLineBuffer);
            szLineBuffer.Empty();
        }
        else
        {
            szLineBuffer += strTemp[i];
        }
    }
}


Where szLineBuffer is just a global variable to my class which is used to store text when a command did not fit into one received chunk. Now notice where i put MESSAGE 1 and MESSAGE 2. This is the odd part, when i uncomment MESSAGE 1 to display a messagebox, and i run my app, when it receives the two commands from the server it is supposed to go like this:

The asynchronous socket calls my OnReceive function because there is data to be read, so my OnReceive reads the data then parses it depending on how many lines there are. When i run it, i get two messageboxes, the first one for the first command, then AFTER that one another one for the second command (the way it should be). BUT when i uncomment MESSAGE 2 this odd thing happens, which is what causes the error in my app which i cant seem to resolve:

When i uncomment MESSAGE 2 and run it, I GET TWO MESSAGEBOXES SIMULTANEOUSLY. O.O I'm completely stunned by this.. I have no explanation whatsoever. I've thought on it for hours and hours and cannot come to any sensible explanation. Because in my mind, the message pump of the dialog cannot continue untill OnReceive() has completed, and yet somehow, it manages to display the two messageboxes at once.. Now, i did realise that MFC is probably using a dummy window for the asynchronous messages to then pass them on the overrided methods, so i tried Sending messages to my main dialog window using SendMessage() in the socket class, and same effect. Of course that was rather stupid because it doesnt make a difference since the message loop of the dummy window the MFC socket class uses should block also on calling the methods from my dialog.

I hope this shows u what my problem is, and how nasty and weird it seems to me.. and of course most of all i hope that someone can explain to me why it is doing this! Or i will have a lot of hair to loose over this one yet. Smile | :)

Thanks to anyone who can help.

Kuniva
--------------------------------------------
GeneralRe: O.O All my knowledge about asynchronous sockets and message pumps broken.. help.. Pin
Kuniva4-May-04 2:15
Kuniva4-May-04 2:15 
GeneralRe: O.O All my knowledge about asynchronous sockets and message pumps broken.. help.. Pin
Mike Dimmick4-May-04 5:14
Mike Dimmick4-May-04 5:14 
GeneralRe: O.O All my knowledge about asynchronous sockets and message pumps broken.. help.. Pin
Kuniva4-May-04 8:11
Kuniva4-May-04 8:11 
GeneralRe: O.O All my knowledge about asynchronous sockets and message pumps broken.. help.. Pin
Mike Dimmick4-May-04 12:11
Mike Dimmick4-May-04 12:11 
GeneralCFile size in bytes Pin
Jamie Kenyon4-May-04 2:02
Jamie Kenyon4-May-04 2:02 
GeneralRe: CFile size in bytes Pin
David Crow4-May-04 2:16
David Crow4-May-04 2:16 
GeneralRe: CFile size in bytes Pin
andyxia4-May-04 2:33
andyxia4-May-04 2:33 
GeneralRe: CFile size in bytes Pin
Alexander M.,4-May-04 3:18
Alexander M.,4-May-04 3:18 
GeneralRe: CFile size in bytes Pin
Jamie Kenyon4-May-04 4:50
Jamie Kenyon4-May-04 4:50 
QuestionSetDIBitsToDevice causes image to flip, why? Pin
uus994-May-04 2:02
uus994-May-04 2:02 
AnswerRe: SetDIBitsToDevice causes image to flip, why? Pin
Monty24-May-04 2:15
Monty24-May-04 2:15 
GeneralRe: SetDIBitsToDevice causes image to flip, why? Pin
uus994-May-04 2:27
uus994-May-04 2:27 
GeneralC2440: 'static_cast' Error Pin
sweep1234-May-04 1:51
sweep1234-May-04 1:51 
GeneralRe: C2440: 'static_cast' Error Pin
Mike Dimmick4-May-04 5:17
Mike Dimmick4-May-04 5:17 
GeneralRe: C2440: 'static_cast' Error Pin
sweep1234-May-04 5:56
sweep1234-May-04 5:56 
GeneralFile drag/drop problem, waiting for your help! Pin
andyxia4-May-04 1:21
andyxia4-May-04 1:21 
GeneralHook in dll Pin
yingkou4-May-04 0:31
yingkou4-May-04 0:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.