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";
}
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";
}
To copy to clipboard, switch view to plain text mode
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
{
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);
}
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);
}
To copy to clipboard, switch view to plain text mode
What have i done mistake ? plz help
Bookmarks