Author Topic: [C++] Socket Help  (Read 3531 times)

0 Members and 1 Guest are viewing this topic.

Offline rabbit

  • x86
  • Hero Member
  • *****
  • Posts: 8092
  • I speak for the entire clan (except Joe)
    • View Profile
[C++] Socket Help
« on: September 15, 2006, 09:18:32 pm »
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:
Quote
USER 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 ><

Code: [Select]
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();
}

Code: [Select]
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));
}
« Last Edit: September 15, 2006, 09:22:51 pm by rabbit »

Offline nslay

  • Hero Member
  • *****
  • Posts: 786
  • Giraffe meat, mmm
    • View Profile
Re: [C++] Socket Help
« Reply #1 on: September 18, 2006, 12:31:00 am »
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.

« Last Edit: September 18, 2006, 12:43:43 am by nslay »
An adorable giant isopod!