• Welcome to Valhalla Legends Archive.
 

Winsock DataArrival Help

Started by Spilled, September 23, 2005, 06:42 PM

Previous topic - Next topic

Spilled

Ok first off i was trying to port my vb dataarrival to C++ but this is my first time doing this and would like your guys help/advice.
Heres what i have so far:


DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
          if ((int)lpParameter == RECV_DATA)
          {
             while (sRet != SOCKET_ERROR)
             {
                  sRet = recv(wSock,rData,256,0);
                  if (sRet != SOCKET_ERROR)
                  {
                     char recBuff[sRet];
                     char strBuffer[800];
                     int lngLen;
                     CopyMemory(strBuffer+strlen(strBuffer), recBuff, sRet);
                     while (strlen(strBuffer) > 4)
                     {
                           lngLen = (int) StrToHex(StringReverse(Mid(recBuff, 3, 2)));
                           if (strlen(strBuffer) < (int) lngLen)
                           {
                              break;
                           }
                           else
                           {
                               //would parsep (Left(strBuffer, lngLen))
                               
                           }
                     }
                  }
          }
      }
}


Any ideas why its not working? Thanks in advance!

UserLoser.

uhh,
where's sRet declared?
why are you using strlen()?
StrToHex(), StringReverse()?  Why would you even do that in VB? Try something like: "lngLen = *(uint16*)(recBuff+2);"
Probably better to do: "while(recv(whatever) != SOCKET_ERROR) { ... }", no?
Wouldn't "break;" cause your thread to die?  That's no good. 
Shouldn't you be waiting on an event before you do recv()?  Otherwise it'll return SOCKET_ERROR because there's nothing to recieve...

Ok, let me say it in English: don't convert VB to C++

Spilled

#2
wow... thats alot of problems... like ive said tho still learning c++. Can you help me with writing this or even give me a start?

Edit:

So i would do the loop more like:


DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
          if ((int)lpParameter == RECV_DATA)
          {
             while (recv(wSock,rData,256,0) != SOCKET_ERROR)
             {
                   
             }
          }
}


? - Kinda lost here. All advice is appreciated.

Imperceptus

Seach Google for C++ Tutorials, Im using this http://www.cprogramming.com/tutorial.html#c++tutorial
Also suggest MSDN

Learn the Basics, if your just trying to match keywords and functions that were done in vb then you will really have issues.  Sit down and Read, A LOT.  Theres alot to take in. 

Read an Article on C++ and VB, which breaks it down like this.  VB is RAD (Rapid App Dev), C++ is planned thought out programming.  If you can't figure out the childish fingerpaintings of C++ how do you expect to understand more complex topics?

Quote from: Hazard on August 07, 2003, 03:15 PM
Highlight your entire code. Press the delete key. Start over again using Cuphead's CSB tutorial and work your way from their rather than raping code from downloaded sources meant purely for learning purposes. If this does not fix the problem, uninstall Visual Basic and get a new hobby. I suggest Cricket.

Spilled

#4
Quote from: UserLoser on September 23, 2005, 08:20 PM
uhh,
where's sRet declared?
why are you using strlen()?
StrToHex(), StringReverse()?  Why would you even do that in VB? Try something like: "lngLen = *(uint16*)(recBuff+2);"
Probably better to do: "while(recv(whatever) != SOCKET_ERROR) { ... }", no?
Wouldn't "break;" cause your thread to die?  That's no good. 
Shouldn't you be waiting on an event before you do recv()?  Otherwise it'll return SOCKET_ERROR because there's nothing to recieve...

Ok, let me say it in English: don't convert VB to C++

Ok, going threw what you reponded with ive tried the "lngLen = *(unit16*)(recBuff+2);" it keeps saying unit16 is undeclared. Ive looked up UINT and it says i need windows.h header file and i have that included. So some help here? Thanks in advance!

Edit:
After working a few more hours, heres what i have come up with:


DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
      if ((int) lpParameter == RECV_DATA)
      {
            char buffer[256];
            int iret;
            int count = 0;
            while(iret != SOCKET_ERROR)
            {
            iret = recv(wSock,buffer,256,0);
            if (iret == SOCKET_ERROR)
            {
                     int we = WSAGetLastError();
                     switch(we)
                     {
                     case 10054:
                          cout << "Conenction Reset by Peer..." << endl;
                          break;
                     default:
                          cout << we << endl;
                          break;
                     }
                     
                }
                else
                {
                    int bufflen = iret;         
                    while(bufflen >= 4)
                    {
                               //char PacketID = (unsigned char)buffer+1;
                               unsigned short uPacketLen = *(unsigned short *)(buffer+2);
                               cout << "Packet Len: " << uPacketLen << endl; // just to see the len
                               Parsep(buffer, uPacketLen);
                               memmove(buffer, buffer+uPacketLen, bufflen-uPacketLen);
                               bufflen -= uPacketLen;
                             
                               
                    }
                    ZeroMemory(buffer,iret);
                }
            }
      }
}


Any ideas are appreciated! No flaming please trying to learn! Thanks in advance everyone.