PDA

View Full Version : Qtcpserver problem



kingslee
30th August 2008, 22:22
Hello Everyone!

I have a basic question.

In fortune server client example, its the client request the data from the server.

So the server receives newconnection() slot and then sends the data.

How to send data to client from server, without any signals from client.

Hope I made my question clearer?

looking forward to your reply

thanks

jacek
31st August 2008, 00:02
If you have a connection with a client, you just write to the socket and client should get the data. If you don't have a connection, you have to turn your client into a server and the server into a client.

kingslee
2nd September 2008, 10:56
thanks jacek for your reply.

Actually, my application runs in threads. Client as well as Server. First of all, sorry for posting freakingly long. I want to complete it by myself but i am stumped.


Client side inside thread

void CEthernet::DoWork() // run()
{
Lock();

if(m_pSocket->waitForReadyRead()== true)
{
QByteArray m_aServerPaket, m_aServerData ;
m_aServerPaket = m_pSocket->readAll();


Server side- Dialog class, server class and Thread class

In server class overwritting the incoming connection and sending the socketDescriptor to Dialog class


void EthernetServer::incomingConnection(int socketDescriptor)
{
m_socketDescriptor = socketDescriptor;
SerialProtocolTestWidget m_Widget;
m_Widget.onNewEthernetConnection(socketDescriptor) ;
}

In Dialog class

only initalizing the new thread instance. communication should be done only after button press in the dialog.

void SerialProtocolTestWidget::onNewEthernetConnection( int socketDescriptor)
{
m_EthernetThread1 = new EthernetThread(socketDescriptor,this);
}

Data transfer should only be done after button press in the dialog.


void SerialProtocolTestWidget::on_butProgrammwahlEthern et_clicked()
{
m_EthernetThread1->SetData(eProgramChoice, totalSize, id, pData, len,true);

m_EthernetThread1->start();
}


In Thread class


EthernetThread::EthernetThread(int socketDescriptor, QObject *parent)
: QThread(parent), socketDescriptor(socketDescriptor), m_bRun1(false)
{
if (!tcpSocket.setSocketDescriptor(socketDescriptor,Q AbstractSocket::ConnectedState, QIODevice::WriteOnly))
{
emit error(tcpSocket.error());
return;
}
}

EthernetThread::~EthernetThread(){}

void EthernetThread::run()
{
int WrittenData = tcpSocket.write((char*)pBuff,len);
}
void EthernetThread::SetData(ECommand command, UINT32 payloadSize, UINT32 id, void *pData, UINT32 &packageLength, bool m_run)
{
//m_bRun1 = m_run;
pBuff = MakeDataPackage(command,payloadSize,id,pData,packa geLength);
len = packageLength;
}



Thanks for looking upto this.
Your suggestion will be highly appreciated.


P.S: (reduced info)
Dialog starts the server and it has many buttons which sends respective data on click to the connected client. Both server and client runs in different threads.

kingslee
3rd September 2008, 18:34
Hello jacek and Everyone,
I found the solution.
I removed the threads and used signals/slots and its working .
thanks again