Hi I am from a Visual Basic background, and if you're still reading this after that admission I could use your help..

I'm trying to learn VC++ and MFC and I'm just trying to work this out. In VB, you could have either a Winsock ActiveX control or use the API- either way, it's methods were public and useable by any procedure. Similarly there was an event for receiving data which fired when anything arrived.

My learning aid app is a dialog based MFC app and I'd like to be able to connect to a server, send strings to it, and see what comesback. The dialog has an Edit control (Edit1) for the server hostname, another (Edit2) for the port number, and a Button (button1) for 'Connect'.

The Edit controls ar einitialised on application start to:

// TODO: Add extra initialization here

m_strEdit1 = "pop3.blueyonder.co.uk";
m_strEdit2 = "110";

Button1 (connect) button event handler does this:

void CTest5Dlg::OnButton1() 
    // TODO: Add your control notification handler code here
    int nPort;
    nPort= atoi(m_strEdit2);
    CSocket m_sConnectSocket;
    CString m_message = "";
    int len ;
    DWORD sent;
    len =m_message.GetLength();
    sent = m_sConnectSocket.Send(m_message,len);

    char *pBuf = new char[1025];
    int iBuf = 1024;
    DWORD iRcvd;
    CString Recvd= "Test";

    iRcvd = m_sConnectSocket.Receive(pBuf,iBuf);
    pBuf[iRcvd] = NULL;

    Recvd = pBuf;

<< moderator edit: added [code][/code] tags >>

When you click the button, the connection is made and lo and behold, you get '+Blueyonder pop3 server ready.' appearing in the list box control.

Now what I want to do is, bearing in mind the TCP connection is still up, is to send more strings from an Edit control and another button- but I can't figure out for the life of me:
a) how you globally create the socket instance so it is accessible by many procedures (well, button event handlers mainly) and
b) can I globally trap incoming data..

any thoughts/suggestions welcome.. (stick to VB?!)

There is, I see, in the CSocket class an 'OnReceive' handler- how on earth do I get data from this into controls on my main dialog- a different class altogether and obviously, if I just include code like


for example, I get an error sayong the control isn't declared. Well, this is true, but how do you share controls between classes in this case?

Thanks for reading this...
Regards, Alan.

This article has been dead for over six months. Start a new discussion instead.