PDA

View Full Version : problem in using pthread in QT



rinku
26th September 2012, 17:57
I am trying to implement a single server & multiclient application for that i am using pthread.My server is a GUI in which all the connected clients should be shown ..ultimatly i have to do some file transfer to multiple clients parallaly.But i am facing problems.

In server side,I have created thread in class MainWindow


void MainWindow::new_connect()
{

ui->label->setText("connected");
sock1=server->nextPendingConnection();
pthread_t t;
if(pthread_create (&t,NULL,&myserver::func,this)<0)
{
qDebug()<<"error in thread";
}
else
{
qDebug()<<"thread create";
}

then i have added one class myserver where thread function "func" is defined,


void* myserver::func(void * parm)

{

try
{

pthread_mutex_lock(&mtx);

char *bb="server";

MainWindow *mn = (MainWindow*)parm;
sleep(4);
if(mn->sock1->write(bb))//first "server" string is send to the client
{
mn->sock1->waitForBytesWritten(3000);
qDebug()<<"server send";
}
else
{
qDebug()<<"server not send";
}
while(1)
{
sleep(2);
memset(rr,0,sizeof(rr));
if(mn->sock1->read(rr,mn->sock1->bytesAvailable())) //Now it will receive client host name
{
QString str=(char *)rr;
QString time=QDateTime::currentDateTime().toString("dd.MM.yyyy hh:mm");
n=n+1;
// ***Connected Clients are Shown in the GUI with date and time****
mn->ui->textEdit->append(QString::number(n)+": "+str+"------ "+time);

break;

}

else{

mn->ui->textEdit->setText("not recv"); break;}

}

catch (const char* e)
{
cout << "Exception caught : " << e << endl;
}

pthread_mutex_unlock(&mtx);

}

What have i done mistake ? plz help

pradeepreddyg95
26th September 2012, 18:34
can you plz mention what problem your facing ....

wysota
26th September 2012, 19:56
I am trying to implement a single server & multiclient application for that i am using pthread.My server is a GUI in which all the connected clients should be shown ..ultimatly i have to do some file transfer to multiple clients parallaly.But i am facing problems.
Is there any particular reason why you are using pthreads and not QThread?

While we're at it... is there any particular reason why you are using threads at all?

With your current construction you'll have lots of trouble because you can't access widgets from within worker threads (regardless if you use mutexes or not).

Networking in Qt can (and usually should) be done in a single thread since Qt's networking routines are non-blocking.

rinku
27th September 2012, 13:01
@pradeep m facing problems like sometime my server got crash or sometimes in synchronization.
@wysota there is no such reason.. i have just tried..i am using thread to handle multiple clients.. i tried this application using QList also. but i faced problem.

void MainWindow::new_connect()

{
ui->label->setText("connected");
QTcpsocket *client=server->nextPendingConnection();
clientconnection.append(client);
mainwindow m;
m.send_message(client);
}

and In send_message() i want to display the connected client name but my GUI become hang.. so doesnt print anything..

wysota
27th September 2012, 13:07
@wysota there is no such reason.. i have just tried..i am using thread to handle multiple clients..
Then don't use threads.


void MainWindow::new_connect()

{
ui->label->setText("connected");
QTcpsocket *client=server->nextPendingConnection();
clientconnection.append(client);
mainwindow m;
m.send_message(client);
}

and In send_message() i want to display the connected client name but my GUI become hang.. so doesnt print anything..
Your code above doesn't make any sense.