[SOLVED]QTcpServer not listening on s60 Device
-
.cpp FIle
@#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(&server,SIGNAL(newConnection()),this,SLOT(newConn()));
// connect(clientSocket,SIGNAL(readyRead()),this,SLOT(clientSocketRead()));
// connect(serverSocket,SIGNAL(readyRead()),this,SLOT(serverSocketRead()));}
MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::startServer(){
QString ownIpAddress; QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses(); for (int i = 0; i < ipAddressesList.size(); ++i) { if (ipAddressesList.at(i) != QHostAddress::LocalHost && ipAddressesList.at(i).toIPv4Address()) { ownIpAddress = ipAddressesList.at(i).toString(); break; } } if (ownIpAddress.isEmpty()) ownIpAddress = QHostAddress(QHostAddress::LocalHost).toString(); if(!server.listen((QHostAddress)ownIpAddress,1235)){ qDebug()<<"server can not be started"; } else{ qDebug()<<"Server started at"<<server.serverAddress()<<"Port"<<server.serverPort(); ui->textBrowser->insertPlainText(tr("server started at : %1 and %2 ").arg(server.serverAddress().toString()).arg(server.serverPort())); } }
void MainWindow::newConn(){
char *data = new char();
qDebug()<<"New Connection";
ui->textBrowser->insertPlainText("New Connection");
// qDebug()<<"threads "<<threads.count()<<" Textbrowsers "<<textBrowsers.count()<<"Sockets "<<connections.count();
uint i;
i=1024;
// QString name;
serverSocket =server.nextPendingConnection();
connect(serverSocket,SIGNAL(readyRead()),this,SLOT(serverSocketRead()));
QByteArray block;
QDataStream out(&block,QIODevice::WriteOnly);
// QString a=tr("(iq,bhatti");
//// out<<a.toAscii();
// serverSocket->write(a.toUtf8());
// serverSocket->flush();}
void MainWindow::connectToServer(){
// char *data = new char();
char *data = new char();
uint i;
i=1024;
uint port;
port = 1235;
QTcpSocket *clientSocket = new QTcpSocket(this);
connect(clientSocket,SIGNAL(readyRead()),this,SLOT(clientSocketRead()));
clientSocket->connectToHost((QHostAddress)"192.168.10.119",port,QIODevice::ReadWrite);
clientSocket->waitForConnected();
if(clientSocket->state()== QAbstractSocket::ConnectedState){
qDebug()<<"Connected to Server";
ui->textBrowser->insertPlainText("Connected to Server");
// clientSocket->waitForReadyRead();
// clientSocket->readLine(data,i);
// qDebug()<<data;
// QString datain = data;
}
}
void MainWindow::on_pushButton_clicked()
{
startServer();
}void MainWindow::clientSocketRead(){
char *data = new char();
uint i;
i=1024;
QString a;
clientSocket->readLine(data,i);
a = tr("/n Other party Said;")+ data;
ui->textBrowser->insertPlainText(a);
}void MainWindow::serverSocketRead(){
char *data = new char();
QByteArray b;// = new QByteArray();
QByteArray b2;
uint i;
i=1024;
QString a;b = serverSocket->read(i); if(b.at(0)=='�'){ b.remove(0,2); //b.append('�'); }
// QDataStream in(b2);
// for(int k=0;k<b.length();k++){
// *(data+k) = *(b+k);
// }
//data = b.data();
// a.fromUtf8(data,20);
//b.remove(0,2);
//b.append('�');
//serverSocin<<b;
//a.fromUtf8(serverSocin);a = b; qDebug()<<a;
// a = tr("/n Other party Said;")+ b.to
ui->textBrowser->insertPlainText(a);
}void MainWindow::on_pushButton_3_clicked()
{
// char *data = new char();
uint i;
i=1024;
QByteArray b;
// data = ui->lineEdit->text().;
// if(clientSocket->state()== QAbstractSocket::ConnectedState){
// clientSocket->write(ui->lineEdit->text().toLocal8Bit());
// }
if(serverSocket->state()== QAbstractSocket::ConnectedState){QDataStream out(&b,QIODevice::WriteOnly); out<<ui->lineEdit->text().toUtf8(); serverSocket->write(b); serverSocket->flush(); }
}
void MainWindow::on_pushButton_2_clicked()
{
connectToServer();
}
@ -
-
You do have the networking capability set, don't you?
-
Yes, i have capabilities set see .pro file
@
TARGET.CAPABILITY += LocalServices NetworkServices
@and Yes i have tried
@server.listen(QHostAddress::Any,1235)@server.serverAddress returned 0.0.0.0
Source of my app is as i have pasted above."blex" if you can just test @startServer() @ on your device and let me know that it works on WLAN ip.
the main.cpp for this app is as below
@#include <QtGui/QApplication>
#include "mainwindow.h"int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
#if defined(Q_WS_S60)
w.showMaximized();
#else
w.show();
#endifreturn a.exec();
}@
And Thanks for taking interest in mu problem
-
Return of 0.0.0.0 is normal, it means that connection may be established from any address.
I see that some characters are broken in the post, so, please, share the archive with sources.
Also, how should I check that it works? I need client to connect to the server.
-
Problem Solved, there was nothing wrong in the code, it was issues with my WLAN i have manual ip for WLAN, which was not working for my app, i got mac of device bind with an ip in DHCP, now device get ip from DHCP and every thing is good.
Thanks 'Blex' for taking interest in my problem.