News:

Help! We're trapped in the computer, and the computer is trapped in 2008! Someone call the time police!

Main Menu

[C++] Socket Help

Started by rabbit, September 15, 2006, 09:18:32 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

rabbit

Outline: I'm writing a simple IRC client for the PSP.  So far I connect, recv some data, and send the USER and NICK messages.  My problem is getting data and moving the buffer.

This is what I've got (clearly not done).  Basically, I'm trying to get the recv buffer to keep the data on the left, and append as it comes in.  But that's not what happens.  What ends up happing is this (ignore the error for now):


What it should look like is:
QuoteUSER rabbittest rabbittest irc.lan.st :Spencer
NICK rabbittest
:irc.lan.st NOTICE AUTH :*** Looking up your hostname...
:irc.lan.st NOTICE AUTH :*** Found your hostname (cached)
:irc.lan.st NOTICE rabbittest :*** If you are having problems connecting due to ping timeouts, please type /quote pong D379BDA9 or /raw pong D379BDA9 now.
PING :D379BDA9

IRC's messages are \r\n delimited, so what I'm trying to do is get the data up to the first \r\n, print it (for now), and then remove it, but it's not working ><

while(bytesRecv != SOCKET_ERROR)
{
bytesRecv = recv(s, recvbuf + offset, 2048, 0);

if(bytesRecv == 0)
{
printf("Connection closed\n");
break;
}

offset += bytesRecv;

buff.add(recvbuf);
ZeroMemory(recvbuf, sizeof(recvbuf));
buff.parse();
}


void Buffer::parse()
{
int pos = (int)(strstr(this->bufferdata, "\r\n") - this->bufferdata + 1);

if(pos > 1)
{
char *piece = new char[pos];
ZeroMemory(piece, sizeof(piece));

strncpy(piece, this->bufferdata, pos);
this->movenext(pos);

printf("%s\r\n", piece);
}
}

void Buffer::movenext(int length)
{
char *temp = new char[sizeof(this->bufferdata)];
this->bufferdata;

ZeroMemory(this->bufferdata, sizeof(this->bufferdata));

strncpy(this->bufferdata, temp + length, sizeof(temp));
}

nslay

#1
I wrote a bot last year and did this sort of stuff.  I could show you my algorithm, but its slightly different in use.  For me its hard to tell whats going on from what you provided, can you show me more (You can PM it or add me to messenger or something)?
Don't assume every server appends "\r\n" at the end of a message...to make your program more flexible, have it support "\n" by itself and strip the "\r".

EDIT: Actually, it looks like you're not appending a '\0' to 'piece' after you do the strncpy...which is why your proceeding printf might be weird ... strncpy doesn't automatically append the '\0'.  There is a function very popular in the BSDs called strlcpy that is supposed to do the same thing but adds the null terminator ... its meant to prevent programmer error and buffer exploits.

An adorable giant isopod!