Hi, I’m little confused by qt events. I’m writing simple chat server and client. Somehow, I’m getting SIGSEGV signal on qDebug() or “double free or corruption” on unknown function (it shows only disassembled code). Can anyone explain me what I’m doing wrong? also, it is console based application.
Regards.
#define chunk_size 1024
...
void TcpSocket::client_readyRead()
{
// client data available.
qDebug() << this->bytesAvailable() << "bytes available from" << this->socketDescriptor();
// get data size.
if(!this->blockSize)
{
if((quint64)this->bytesAvailable() < sizeof(quint64))
return;
else
this->read((char *)&this->blockSize, sizeof(quint64));
}
if(this->blockSize)
{
// we got data to read.
qDebug() << "expecting" << this->blockSize << "bytes to read."; // it crashes here. type of blockSize is quint64
int data_size;
if(this->blockSize != chunk_size)
data_size = this->blockSize % chunk_size;
else
data_size = this->blockSize;
char temp[data_size];
qint64 received = this->read(temp, data_size);
if(received < 0)
{
// error receiving data.
qDebug() << "error receiving data from" << this->socketDescriptor();
this->blockSize = 0;
return;
}
else
{
if(received)
{
qDebug() << "received" << received << "bytes from" << this->socketDescriptor();
this->blockSize -= received;
this->chunkArray.append(temp, received);
if(!this->blockSize)
{
qDebug() << this->chunkArray;
//emit dataReady(this->chunkArray);
}
}
}
}
}
sending part is like this:
int client_send(int sockfd, const char *data, unsigned long long datalen)
{
unsigned long long newlen = datalen + sizeof(unsigned long long);
int offset = 0;
char temp[newlen];
memmove(temp, (char *)&datalen, sizeof(unsigned long long));
memmove(temp + sizeof(unsigned long long), data, datalen);
while(newlen)
{
int data_size = 0;
if(newlen != chunk_size)
data_size = newlen % chunk_size;
else
data_size = chunk_size;
int sent = send(sockfd, temp + offset, data_size, 0);
if(sent != -1)
{
if(sent)
{
offset += sent;
newlen -= sent;
}
}
else
{
newlen = 0;
offset = 0;
}
}
return offset;
}
...
int main(int argc, char* argv[])
{
client_send(sockfd, "hello ", 6);
client_send(sockfd, "world ", 6);
client_send(sockfd, "this ", 5);
client_send(sockfd, "is ", 3);
client_send(sockfd, "test", 4);
return 0;
}
↧