Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CAsyncSocket problem

CAsyncSocket problem

Scheduled Pinned Locked Moved C / C++ / MFC
c++helphtmldebuggingperformance
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kazz
    wrote on last edited by
    #1

    Hi, I just started getting into C++/MFC and I've hit my first snag. I'm making a simple program that downloads the contents of a webpage and then sorts out href's. The problem comes into play in the OnReceive event handler. I'm getting 'The instruction at 0x77f475cc referenced by 0x0000000. The memory could not be "written"' right after I get the first packet. I suppose I'm doing something wrong with a pointer? Null maybe? It ONLY does this when I compile for release, it works perfectly when I compile for debug. After changing around some compiler options (turning off optimizations) it still does the same thing. I've done step by step debugging and the last thing I'm seeing is 'Receiving HTML'. I've looked over it for a long time and even showed it to a few friends, they couldn't figure it out either. My guess is it's the pBuf pointer but I don't see why, if anybody could give me some tips I would appreciate it! Here's my code.. void CReconDlg::OnReceive() { char *pBuf = new char[1024]; CString strRecvd, strConlen = "Content-Length:", actualLen; char *pConlen; int wholePacket, int result, int lenLen, int endPos, int iBufSize = 1024, int iRcvd, int packetStart; // get the data iRcvd = m_sConSocket.Receive(pBuf, iBufSize); // strip any garbage off of the buffer pBuf[iRcvd] = NULL; // error trap if (iRcvd == SOCKET_ERROR){ MessageBox("Error on receiving socket"); }else{ // debug step and append buffer to CString for manipulation m_sStat = "Receiving HTML.."; strRecvd += pBuf; } // search for the Content-Length so we know just how much data we're getting pConlen = strstr(strRecvd, strConlen); // if it's found parse out the Content-Length: header feild and append everything AFTER the header to a new string if (pConlen != NULL){ result = (int)(pConlen - strRecvd + 1) + 15; endPos = strRecvd.Find("\n", result) - 1; lenLen = (endPos - result); actualLen = strRecvd.Left(endPos); realLen = actualLen.Right(lenLen); packetStart = strRecvd.Find("\r\n\r\n", result); int packetLen1 = (strlen(strRecvd) - packetStart); m_strIn += strRecvd.Right(packetLen1); }else{ // if there's no header just throw the entire string onto our 'raw packet' m_strIn += strRecvd; } // get lengths wholePacket = strlen(m_strIn) - 4; CString display ; display.Format("%d", wholePacket); // have we got all of it? if so continue to parse.. if (rea

    M 1 Reply Last reply
    0
    • K Kazz

      Hi, I just started getting into C++/MFC and I've hit my first snag. I'm making a simple program that downloads the contents of a webpage and then sorts out href's. The problem comes into play in the OnReceive event handler. I'm getting 'The instruction at 0x77f475cc referenced by 0x0000000. The memory could not be "written"' right after I get the first packet. I suppose I'm doing something wrong with a pointer? Null maybe? It ONLY does this when I compile for release, it works perfectly when I compile for debug. After changing around some compiler options (turning off optimizations) it still does the same thing. I've done step by step debugging and the last thing I'm seeing is 'Receiving HTML'. I've looked over it for a long time and even showed it to a few friends, they couldn't figure it out either. My guess is it's the pBuf pointer but I don't see why, if anybody could give me some tips I would appreciate it! Here's my code.. void CReconDlg::OnReceive() { char *pBuf = new char[1024]; CString strRecvd, strConlen = "Content-Length:", actualLen; char *pConlen; int wholePacket, int result, int lenLen, int endPos, int iBufSize = 1024, int iRcvd, int packetStart; // get the data iRcvd = m_sConSocket.Receive(pBuf, iBufSize); // strip any garbage off of the buffer pBuf[iRcvd] = NULL; // error trap if (iRcvd == SOCKET_ERROR){ MessageBox("Error on receiving socket"); }else{ // debug step and append buffer to CString for manipulation m_sStat = "Receiving HTML.."; strRecvd += pBuf; } // search for the Content-Length so we know just how much data we're getting pConlen = strstr(strRecvd, strConlen); // if it's found parse out the Content-Length: header feild and append everything AFTER the header to a new string if (pConlen != NULL){ result = (int)(pConlen - strRecvd + 1) + 15; endPos = strRecvd.Find("\n", result) - 1; lenLen = (endPos - result); actualLen = strRecvd.Left(endPos); realLen = actualLen.Right(lenLen); packetStart = strRecvd.Find("\r\n\r\n", result); int packetLen1 = (strlen(strRecvd) - packetStart); m_strIn += strRecvd.Right(packetLen1); }else{ // if there's no header just throw the entire string onto our 'raw packet' m_strIn += strRecvd; } // get lengths wholePacket = strlen(m_strIn) - 4; CString display ; display.Format("%d", wholePacket); // have we got all of it? if so continue to parse.. if (rea

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      First thing I noticed is that this: pBuf[iRcvd] = NULL; is a buffer overrun. What happens if iRcvd is 1024? You write a 0 to one byte past the end of the array, overwriting whatever is on the stack at that point. --Mike-- The Internet is a place where absolutely nothing happens.   -- Strong Bad 1ClickPicGrabber - Grab & organize pictures from your favorite web pages, with 1 click! My really out-of-date homepage Sonork-100.19012 Acid_Helm

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups