PDA

View Full Version : Reading data from socket



ComaWhite
3rd December 2008, 19:47
I've been trying repeatly and rewritting it over billions of times it seems.

But when I'm connecting to a irc server with my QTcpSocket. Some lines are malformed. where it is split to a new line.

So I thought I could just do it to where check if the line starts without a ':' append it to the previous line. But apparently that causes some lines to be mixed together and what not. And I can't find hardly any resources on doing it correctly.



void
IrcSocket::readSocket() {
if(!m_ircSocket->canReadLine()) {
return;
}

QTextStream stream(m_ircSocket);
QString data;

do {
data = stream.readLine();

if(data[0] != ':') {
QStringList words = data.split(" ");

if(words.count() >= 2) {
if(words[0] == "NOTICE" && words[1] == "AUTH") {
QRegExp reg("^NOTICE AUTH :(.+)$");

if(reg.exactMatch(data)) {
emit noticeAuth(reg.cap(1));
}
} else {
data.append(stream.readLine());
}
} else {
data.append(stream.readLine());
}
} else {
QStringList words = data.split(" ");

switch(words[1].toUInt()) {
case 001: {
QRegExp reg("^[^ ]+ 001 [^ ]+ :(.+)$", Qt::CaseInsensitive);

if(reg.exactMatch(data)) {
emit welcome(reg.cap(1));
} else {
data.append(stream.readLine());
}
}
case ReplyCode::RPL_MOTDSTART: {
QRegExp reg("^[^ ]+ 375 [^ ]+ :(.+)$", Qt::CaseInsensitive);

if(reg.exactMatch(data)) {
emit motdStart(reg.cap(1));
} else {
data.append(stream.readLine());
}
break;
}
case ReplyCode::RPL_MOTD: {
QRegExp reg("^[^ ]+ 327 [^ ]+ :(.+)$", Qt::CaseInsensitive);
kDebug() << data;
if(reg.exactMatch(data)) {
kDebug() << "TEST: " << reg.cap(1);
data.clear();
} else {
data.append(stream.readLine());
}
break;
}
default: {
data.append(stream.readLine());
break;
}
}
}

kDebug() << data;
} while(!stream.atEnd());
}

jpn
4th December 2008, 18:22
How about reading the data to a buffer whenever new data comes available, and reading complete lines from the buffer?