ورود

View Full Version : مشکل در Multi Thread



hanita
یک شنبه 15 آذر 1394, 08:45 صبح
سلام
یه برنامه کلاینت سرور ساده نوشتم که سرورباید multi thread باشه. میشه راهنماییم کنید کجاش مشکل داره؟ ممنونم
myserver.cpp

#include"myserver.h" #include"mythread.h"

myserver::myserver(QObject*parent):
QObject(parent) { }
voidmyserver::startserver() {
intport=1234;
if(s_server.listen(QHostAddress::Any,port)) { qDebug()<<"Couldnotstartserver"; } else { qDebug()<<"Listeningtoport"; }
}
voidmyserver::acceptConnection() {
c_client=s_server.nextPendingConnection();
connect(c_client,SIGNAL(readyRead()),
this,SLOT(startRead()));
qDebug()<<"Connecting...";
mythread*thread=newmythread(socketDescriptor,this) ;
connect(thread,SIGNAL(finished()),thread,SLOT(dele teLater()));
thread->start();
}





mythread.cpp:
#include "mythread.h" #include "myserver.h"
mythread::mythread(qintptr ID, QObject *parent) : QThread(parent) { this->socketDescriptor = ID; }

void mythread::run() {
qDebug() << " Thread started"; }


void mythread::startRead() { char buffer[1024] = {0};
c_client->read(buffer, c_client->bytesAvailable());
qDebug() << buffer;
}
void mythread::readyRead() { QByteArray Data = socket->readAll();
qDebug() << socketDescriptor << " Data in: " << Data;
socket->write(Data); }
void mythread::disconnected() { qDebug() << socketDescriptor << " Disconnected";

socket->deleteLater(); exit(0); }

pswin.pooya
یک شنبه 15 آذر 1394, 15:28 عصر
کد رو بهتره داخل تگهای کد (code) قرار بدید که بشه اون رو خوند.

hanita
دوشنبه 16 آذر 1394, 08:32 صبح
اینکارو کردم اما کلا ناخوانا شد و کد رو به حال 0و 1 نشون میداد...

mohammad0773
دوشنبه 16 آذر 1394, 13:15 عصر
myserver.cpp

#include"myserver.h" #include"mythread.h"

myserver::myserver(QObject*parent):
QObject(parent) { }
voidmyserver::startserver() {
intport=1234;
if(s_server.listen(QHostAddress::Any,port)) { qDebug()<<"Couldnotstartserver"; } else { qDebug()<<"Listeningtoport"; }
}
voidmyserver::acceptConnection() {
c_client=s_server.nextPendingConnection();
connect(c_client,SIGNAL(readyRead()),
this,SLOT(startRead()));
qDebug()<<"Connecting...";
mythread*thread=newmythread(socketDescriptor,this) ;
connect(thread,SIGNAL(finished()),thread,SLOT(dele teLater()));
thread->start();
}





mythread.cpp:
#include"mythread.h" #include"myserver.h"
mythread::mythread(qintptrID,QObject*parent): QThread(parent) { this->socketDescriptor=ID; }

voidmythread::run() {
qDebug()<<"Threadstarted"; }


voidmythread::startRead() { charbuffer[1024]={0};
c_client->read(buffer,c_client->bytesAvailable());
qDebug()<<buffer;
}
voidmythread::readyRead() { QByteArrayData=socket->readAll();
qDebug()<<socketDescriptor<<"Datain:"<<Data;
socket->write(Data); }
voidmythread::disconnected() { qDebug()<<socketDescriptor<<"Disconnected";

socket->deleteLater(); exit(0); }

حامد مصافی
دوشنبه 16 آذر 1394, 17:27 عصر
شما بگین کجای این کد درسته؟؟؟!!!

اول: از متد پروتکتت newIncomingConnection کلاس QTcpSockerServer برای آگاهی از یک اتصال جدید استفاده کنید.
دوم: سوکت کی به ترد منتقل میشه؟
سوم: این کد شما رو کامپیوتر میفهمه. حالا ما هیچی، خودتون سردرگم نمیشید؟؟
چهارم: در این پروژه من (https://github.com/HamedMasafi/Noron/) به طور کامل چند رسیمانی در سوکت‌سرور اعمال شده. می‌تونید کد‌های مربوط به سرور اونو مطالعه کنید.

hanita
چهارشنبه 18 آذر 1394, 08:46 صبح
شما بگین کجای این کد درسته؟؟؟!!!

اول: از متد پروتکتت newIncomingConnection کلاس QTcpSockerServer برای آگاهی از یک اتصال جدید استفاده کنید.
دوم: سوکت کی به ترد منتقل میشه؟
سوم: این کد شما رو کامپیوتر میفهمه. حالا ما هیچی، خودتون سردرگم نمیشید؟؟
چهارم: در این پروژه من (https://github.com/HamedMasafi/Noron/) به طور کامل چند رسیمانی در سوکت‌سرور اعمال شده. می‌تونید کد‌های مربوط به سرور اونو مطالعه کنید.

ممنون از راهنماییتون .. راستش من اولین باره که دارم با Qt کار میکنم و اینم اولین پروژه ست برای همین هنوز خیلی مشکل دارم باهاش.
خیلی لطف کردین ممنون از توضیحاتتون.

الان من نمیدونم چی رو باید از سرور به ترد منتقل کنم...؟؟:افسرده:

حامد مصافی
چهارشنبه 18 آذر 1394, 09:23 صبح
الان من نمیدونم چی رو باید از سرور به ترد منتقل کنم...؟؟
نسخه‌ای از QTcpSocket رو که ایجاد شده با متد moveToThread به ترد جدید منتقل کنید.

hanita
چهارشنبه 18 آذر 1394, 11:18 صبح
نسخه‌ای از QTcpSocket رو که ایجاد شده با متد moveToThread به ترد جدید منتقل کنید.

من به این صورت نوشتم اما بازم خروجی رو نشون نمیده[CPP]#include"myserver.h" #include"mythread.h"
#include "myserver.h" #include "mythread.h"

myserver::myserver(QObject *parent) :
QObject(parent)
{
}
void myserver::startserver() { int port = 6666;
if(m_server.listen(QHostAddress::Any, port))
{ qDebug() << "Listening to port " ;
connect(m_client,SIGNAL(nextPendingConnection()), m_client ,SLOT(nextPendingConnection())); }
else { qDebug() << "Could not start server "<<m_server.errorString(); } }

void myserver::newConnection()
{
m_client = m_server.nextPendingConnection();
qDebug() << " Connecting...";
mythread *thread = new mythread(m_client,this);
connect(m_client, SIGNAL(finished()),m_client, SLOT(deleteLater()));
thread->start();
}

hanita
چهارشنبه 18 آذر 1394, 13:56 عصر
میدونم مشکل تو این قسمته ولی نمیدونم چه طوری باید حلش کنم ...

connect(m_client,SIGNAL(nextPendingConnection()), this ,SLOT(nextPendingConnection()));

حامد مصافی
یک شنبه 22 آذر 1394, 16:48 عصر
شما یک کلاس ایجاد کنید که از QTcpSocket مشتق شده باشد و اتصال کلاینت جدید را در این کلاس مدیریت کنید. (با پیاده سازی متد newIncomingConnection)