[C++]Socket Receive

Newbie Spellweaver
Joined
Jun 8, 2008
Messages
8
Reaction score
0
Hey,

I'm currently developing a Habbo server from zero in C++ (Console). Now, I'm using winsock.h for the socket. I created a thread (_beginthreadex) with the following code to receive the data sent by the Habbo Client:

Code:
unsigned __stdcall sReceive( void* pArguments )
{
    char recvBuff[] = "";
    string strPacket;
    memset(recvBuff, 0, sizeof(recvBuff));
    int bytesRecv = 0;
    bytesRecv = recv(aSocket, recvBuff, 4096, 0);
    if(bytesRecv>0)
    {
                     string strRecv = recvBuff;
                     string output = strRecv.substr(0, bytesRecv);
                     strPacket = strRecv.substr(0, 2); //Get the first two chars of strRecv
                     cout << output << endl;
    }
       
    return 0; 
}

But this doesn't output anything... It says that I'm connected in my server, but it doesn't output the data..
Anyone who can help me with this?

Thanks
 
char recvBuff[] = "";

recvbuff has no length, set it this way instead:

Code:
char recvBuff[4096];
memset(recvBuff, 0, sizeof(recvBuff));
int bytesRecv = recv(aSocket, recvBuff, 4096, 0);
if(bytesRecv>0)
{
    std::string strRecv(recvBuff);
    std::cout << strRecv.substr(0, 2) << '\n';
}

PS: it's generally bad to use "using namespace std;"
 
Thanks for your reply. I replaced all cout's etc. with std::cout's, and I used the code you posted above. But I still don't get an output. It says "New user connected", so I don't see what the problem is.
 
Does it pass

if(bytesRecv>0)

?
 
Mmh.. I placed "std::cout << "Hello" << '\n';" before the code, and it only says it ones. So it doesn't get any data (because the recv functions blocks until it gets data). The Habbo Client sends data when you click login or register, right? So I don't see whats the problem..

Edit: I'm gonna use Asynchronous Sockets... So, I will update my code, and maybe it works then... Will let you know :)
 
If im not mistaken, you will have to send "@@" on client connect to be able to recieve packets from client
 
If im not mistaken, you will have to send "@@" on client connect to be able to recieve packets from client


Yeah, client doesn't send anything until it receives @@[1]

[1] being Character 1 ("\x01")

Perhaps thats why its not working? :tongue:


Knowing a language is great, but to experiment like this you need to know about the Game (in your case habbo)

Personally i'd connect to your app via telnet rather than with the Habbo client, just so you know whats being send (And whats being received)
 
uhm, why ?
in header file, I know it's really bad but in source files ??

Using 'using' in header files/libraries is considered as a bug (and makes the complete namespacing useless!), but in source code it's not _that_ bad. You have more possible names left without, and won't get nasty errors when you do this:

using namespace std;
char *string; // error

since string (std::string) is a class.
 
I'm almost ready with Async Sockets... But how do you append a char(1) at a string? I thought it was something like function(stringbuffer, "string%c", char(1)) ... I forgot the function.. Anyone knows something like this?
 
std::string string;
char c = 'c';
string += c;
 
Thanks for your fast reply (again :)), but I can't use std::string, because it needs to be char* ...

Edit: Didn't saw Jeax reply yet... Gonna try that first.. Thanks!

Edit: \x01 works, thanks!
 
Back