PDA

View Full Version : Socket program not working



nighil
27th January 2011, 06:39
I created a client and server program in Qt. It works when i test it from windows to linux,connected through TCP socket and also it works when i run client and server in the same system.But it is not working when i run it in two different linux OS connected in network..

tbscope
27th January 2011, 06:50
That's by far not enough information.

What did you already try to solve it?
How does your code look like?
Do these two different linux systems work as expected? In other words, can you setup another client/server connection between them? (ftp for example?)
Etc...

nighil
27th January 2011, 07:03
One system fedora was installed and in the other system ubuntu was installed.

tbscope
27th January 2011, 07:11
That's like:
I have two cookies, one with sprinkles, one without. Why doesn't my car start?

Lets try to eliminate the variables. Please post a minimal compilable example demonstrating the problem.
This way, the variable of your code being correct or wrong can be eliminated.

nighil
29th January 2011, 06:04
CLIENT

#include "dialog1.h"
#include "ui_dialog1.h"
#include "timeall.h"
#include<QByteArray>
#include <QFile>
#include <QTextStream>



QString q,s,t,o,d;
Dialog1::Dialog1(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog1)
{
ui->setupUi(this);
sock = new QTcpSocket(this);
sock->connectToHost("localhost", 40000);
connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(on_pushButton_2_clicked()));
}

Dialog1::~Dialog1()
{
delete ui;
sock->close();

}

void Dialog1::changeEvent(QEvent *e)
{
QDialog::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}

void Dialog1::on_pushButton_clicked()
{
//Time Advanced
timeall *t1=new timeall;
t1->setModal(true);
t1->show();

QFile file("dd.txt");
file.open(QIODevice::WriteOnly);
QTextStream out(&file);
q=ui->lineEdit_3->text();
s=ui->lineEdit_2->text();
if(s==NULL)
{
q.clear();
o.clear();
d="date +%Y%m%d -s ";
t=ui->lineEdit_4->text();
o+=t;
t=ui->lineEdit_3->text();
o+=t;
t=ui->lineEdit->text();
o+=t;
d+=o;

out<<d;



/*sock->write(block);
sock->close();*/

// system(m);
//d.clear();
// o.clear();
//t.clear();
}

else if(q==NULL)
{
d="date +%T -s ";
q=ui->lineEdit_2->text();
o+=q;
q=ui->lineEdit_5->text();
o+=q;
d+=o;
//const char *m = d.toLatin1();//Qstring to const *char
QByteArray block = "";
block.append(d);
//sock->write(block);
//sock->close();

}
else
{
d="date ";
o+=q;
q=ui->lineEdit->text();
o+=q;
q=ui->lineEdit_2->text();
o+=q;
q=ui->lineEdit_5->text();
o+=q;
q=ui->lineEdit_4->text();
o+=q;

d+=o;
QByteArray block = "";
block.append(d);
// sock->write(block);
// sock->close();

}

}

void Dialog1::on_pushButton_2_clicked()
{

q=ui->lineEdit_3->text();
s=ui->lineEdit_2->text();
if(s==NULL)
{
q.clear();
o.clear();
QString d="date +%Y%m%d -s ";
t=ui->lineEdit_4->text();
o+=t;
t=ui->lineEdit_3->text();
o+=t;
t=ui->lineEdit->text();
o+=t;
d+=o;

const char *m = d.toLatin1();//Qstring to const *char
QByteArray block = "";
block.append(d);
sock->write(block);
sock->close();

// system(m);
d.clear();
o.clear();
t.clear();
}

else if(q==NULL)
{
QString d="date +%T -s ";
q=ui->lineEdit_2->text();
o+=q;
q=ui->lineEdit_5->text();
o+=q;
d+=o;
const char *m = d.toLatin1();//Qstring to const *char
QByteArray block = "";
block.append(d);
sock->write(block);
sock->close();
//system(m);
d.clear();
o.clear();
q.clear();
}
else
{
QString d="date ";
o+=q;
q=ui->lineEdit->text();
o+=q;
q=ui->lineEdit_2->text();
o+=q;
q=ui->lineEdit_5->text();
o+=q;
q=ui->lineEdit_4->text();
o+=q;

d+=o;
const char *m = d.toLatin1();//Qstring to const *char
QByteArray block = "";
block.append(d);
sock->write(block);
sock->close();
// system(m);
d.clear();
o.clear();
q.clear();
}




}

//SERVER

#include "mainwindow.h"
#include "ui_mainwindow.h"


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Set up connection
server = new QTcpServer(this);

// If server does not listen
if( !server->listen(QHostAddress::Any,40000))
{

server->close();
return; // Leave constructor, e.g. if port is currently used
}
connect(server, SIGNAL(newConnection()), this, SLOT(on_new_connection()));
}

MainWindow::~MainWindow()
{
//delete ui;
server->close();
}

void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
//ui->retranslateUi(this);
break;
default:
break;
}
}
void MainWindow::on_new_connection()
{
sock = server->nextPendingConnection();
connect(sock, SIGNAL(readyRead()), this, SLOT(on_ready_read()));
}

void MainWindow::on_ready_read()
{
QByteArray msg=sock->readAll();
sock->close();
QString message(msg);
//ui->label->setText(message);
const char *m = message.toLatin1();
system(m);
}