Advertisement

IRC messages are all messed up :(

Started by February 16, 2005 03:34 PM
6 comments, last by hplus0603 20 years ago
Hey, I've been trying to parse my irc messages i'm getting form the server for the past day and a half, and I cant get it down right, this is what i'm doing to try to do to separate the messages: I know that irc messages end with a carrigereturn-line feed pair char(13) char(10) so i tried to separate my messages whenever i saw those characters basically, what my code is doing is: -it first recieves the messages and stores it in a char array called recvbuf that is 512 characters long. -then i convert that char array into a string object (s1)so i can manipulate it slightly better. -I then add whatever s1 to s, which is a string that holds all the messges it gets - i then find the position of the linefeed character (char 10) in my s string. -now i'm inside my WHILE loop while(i != -1){ - i now create another string called subs that will be the substring of s that starts at position 0 and contains all the characters up to, but not including the line feed character (char 10). -now i have to remove the carrige return character frmo the very end of subs, so i erase the last character. -i then push that string onto a queue i have created to hold all the server messages i got. -i now clear the subs string, and erase all the characters in s, starting from 0 up to and includeing the line feed character (char 10); -i then find the next line-feed characer (char 10) and start the loop over again until there are no more line-feed characters. Here's the code: string s; while(1){ bytesRecv = recv( m_socket, recvbuf, 512, 0 ); string s1(recvbuf); s = s + s1; int i = s.find( char(10) ); while(i != -1){ subs = s.substr(0 , i); subs.erase( subs.length()-1, 1); msgQueue.push( subs ); subs.clear(); s.erase(0,i+1); i = s.find( char(10) ); } } I figure this should work, but all my messages i get from the server after i pop from the msgQueue are garbled up some how, like this. I cant figure out why i'm getting these weird symbols in front of the PING message and such. NOTICE AUTH :*** Looking up your hostname └ÿ∩&åNOTICE AUTH :*** Checking Ident NOTICE AUTH :*** Found your hostname └Ö░NÇαj╨☻Çj╨≈9«NÇ☻NOTICE AUTH :*** No ident response ICE AUTH :*** Found your hostname └Ö░NÇαj╨☻Çj╨≈9«NÇ☻PING :1198091985 o ident response ICE AUTH :*** Found your hostname These garbled characters are different each time i run my program. And sometimes i even get the ping messages attached to the end of another message and its not separated by a char(10)char(13) pair. Anyone have expeirence in parsing irc messages that could give me some pointers on how to fix this? Any help would be greatly appreaciated. Thanks
-------------------------------------Physics Labhttp://www.physics-lab.netC++ Labhttp://cpp.physics-lab.net
Quote:

string s1(recvbuf);



recv() does not zero-terminate the receiving buffer. Thus, this construction of the string may gobble up whatever gunk is in the buffer after the actual received data, until it happens to hit a nul character.

You should zero-terminate the buffer yourself, or use the string constructor that takes pointer and length. You should also check the return value from recv(); if it's less than 0 you have an error and need to deal with it.
enum Bool { True, False, FileNotFound };
Advertisement
I tried doing that, but it didn't help much
I'm starting to think its the undernet server that i'm joining, i think they're not using the standard protocol, this is the one i'm basing it off

ftp://ftp.rfc-editor.org/in-notes/rfc1459.txt

-------------------------------------Physics Labhttp://www.physics-lab.netC++ Labhttp://cpp.physics-lab.net
I'm surprised you're having so many troubles. I personally used these specs., along with this as a guide, and I didn't have too many troubles. Might I suggest that rather than trying to parse your messages, just your application so you can receive messages, *then* work on parsing them.

My implementation may have had something to do with it. I made 2 threads, 1 for receiving and printing data (which quite often wrote over whatever I was writing at the time, because it was only a *very* basic console application), and another to send whatever it was I chose to write (followed by '\r\n'), both terminating once the connection was broken (usually after I typed in 'QUIT' and hit enter). Another thing I did was set all of the memory of my buffers to zero, that way the only way it would ever print garbage was if it managed to read all 512 bytes, which is a highly unlikely event.
Cool thanks for the info, i'll take a look at the code in a little bit, I got a Discreet Transformations exam in an hour
-------------------------------------Physics Labhttp://www.physics-lab.netC++ Labhttp://cpp.physics-lab.net
Hey Anonymous poster!

Thanks so much, you're code really helped me figure out how to extract the messages.

My client is working great.


But I just have one more question.

Does anyone know of any good thread classes that are easy to use, one that my irc class can just inherit from? The one that i'm using right now isn't very good.

Thanks
-------------------------------------Physics Labhttp://www.physics-lab.netC++ Labhttp://cpp.physics-lab.net
Advertisement
Gavinl: yeah, be glad you're taking Discreet Transformations. They're so much better than those Conspicuous Transformations.

:-)
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement