// tcpip.h
#ifndef TCPIP_H
#define TCPIP_H
#include <QThread>
#include <QtGui/QWidget>
#include "ui_tcpip.h"
class tcpip_client;
{
Q_OBJECT
public:
~tcpip();
private:
Ui::tcpipClass ui;
ushort port;
private slots:
void StartTCPServer();
void StartTCPClient();
void str_client(tcpip_client* tcpip_x, Ui::tcpipClass ui);
};
{
Ui::tcpipClass ui;
ushort port;
public:
tcpipThread( Ui::tcpipClass uiparm, unsigned portParam):ui(uiparm),port(portParam)
{}
void run(){}
void start();
};
#endif // TCPIP_H
// tcpip.cpp
#include "tcpip.h"
#include "tcpip_server.cpp"
#include "tcpip_client.cpp"
#include <iostream>
//#include <QtDebug>
{
ui.setupUi(this);
connect( ui.pushButtonStartTCPServer, SIGNAL(clicked()), this, SLOT(StartTCPServer()) );
connect( ui.pushButtonStartTCPClient, SIGNAL(clicked()), this, SLOT(StartTCPClient()) );
ui.
listWidgetServer->addItem
(QString("server constructor"));
}
tcpip::~tcpip()
{
}
void tcpip::StartTCPServer()
{
ui.
listWidgetServer->addItem
(QString("server StartTCPServer"));
// QApplication::processEvents();
bool ok = true;
QString qstr
= ui.
lineEditTCPIPServerPortServer->text
();
port = qstr.toInt(&ok);
//qDebug()<<" server port "<<port;
ui.lineEditServerStatus->setText("server started");
tcpipThread tcpipThreadStart(ui, port);
tcpipThreadStart.start();
// tcpipThreadStart.wait(ULONG_MAX);
}
void tcpip::StartTCPClient()
{
ui.
listWidgetClient->addItem
(QString("client StartTCPClient"));
bool ok = true;
QString qstr
= ui.
lineEditTCPIPServerPortClient->text
();
//QApplication::processEvents();
ushort port = qstr.toInt(&ok);
//qDebug()<<"client port = "<<port;
tcpip_client* p_client = new tcpip_client((int)19999,port,FALSE, ui);
str_client(p_client, ui); /* process the request */
close();
}
//
#define MAXLINE 512
// str_client is the client processing program
// particular to the application and depends on
// what the server is doing
void tcpip::str_client(tcpip_client* tcpip_x, Ui::tcpipClass ui)
{
int n;
char sendline[MAXLINE], recvline[MAXLINE + 1];
bool done=FALSE;
while (!done)
{
cout<<"enter keyboard string"<<endl;
cin.getline(sendline,256);
n = strlen(sendline);
if(n==0) break;
sendline[n] = '\n'; /* newline terminate */
sendline[n+1] = 0; /* newline terminate */
n++;
// use our client object to write to the server
ui.
listWidgetClient->addItem
(QString("client constructorstr_client"));
tcpip_x->tcp_write(sendline,n);
cout<<"sendline:"<<sendline<<endl;
/*
* Now read a line from the socket and write it to
* our standard output.
*/
n = tcpip_x->tcp_read(recvline);
recvline[n] = '\n'; /* null terminate */
recvline[n+1] = 0; /* null terminate */
n++;
cout<<"bytes received "<<n;
cout<<"client received:echo = "<<recvline<<endl;
}
}
void tcpip
::currChanged(QWidget*){};
void tcpipThread::start()
{
tcpip_server* p_serv = new tcpip_server((int)19999,port,tcpip_server::str_echo, ui);
qDebug()<<"oops...back from CTOR";
delete p_serv;
}
// tcpip.h
#ifndef TCPIP_H
#define TCPIP_H
#include <QThread>
#include <QtGui/QWidget>
#include "ui_tcpip.h"
class tcpip_client;
class tcpip : public QWidget
{
Q_OBJECT
public:
tcpip(QWidget *parent = 0);
~tcpip();
private:
Ui::tcpipClass ui;
ushort port;
private slots:
void StartTCPServer();
void StartTCPClient();
void str_client(tcpip_client* tcpip_x, Ui::tcpipClass ui);
void currChanged(QWidget*);
};
class tcpipThread : public QThread
{
Ui::tcpipClass ui;
ushort port;
public:
tcpipThread( Ui::tcpipClass uiparm, unsigned portParam):ui(uiparm),port(portParam)
{}
void run(){}
void start();
};
#endif // TCPIP_H
// tcpip.cpp
#include "tcpip.h"
#include "tcpip_server.cpp"
#include "tcpip_client.cpp"
#include <iostream>
//#include <QtDebug>
tcpip::tcpip(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
connect( ui.pushButtonStartTCPServer, SIGNAL(clicked()), this, SLOT(StartTCPServer()) );
connect( ui.pushButtonStartTCPClient, SIGNAL(clicked()), this, SLOT(StartTCPClient()) );
ui.listWidgetServer->addItem(QString("server constructor"));
}
tcpip::~tcpip()
{
}
void tcpip::StartTCPServer()
{
ui.listWidgetServer->addItem(QString("server StartTCPServer"));
// QApplication::processEvents();
bool ok = true;
QString qstr = ui.lineEditTCPIPServerPortServer->text();
port = qstr.toInt(&ok);
//qDebug()<<" server port "<<port;
ui.lineEditServerStatus->setText("server started");
QApplication::processEvents();
tcpipThread tcpipThreadStart(ui, port);
tcpipThreadStart.start();
// tcpipThreadStart.wait(ULONG_MAX);
}
void tcpip::StartTCPClient()
{
ui.listWidgetClient->addItem(QString("client StartTCPClient"));
bool ok = true;
QString qstr = ui.lineEditTCPIPServerPortClient->text();
//QApplication::processEvents();
ushort port = qstr.toInt(&ok);
//qDebug()<<"client port = "<<port;
tcpip_client* p_client = new tcpip_client((int)19999,port,FALSE, ui);
str_client(p_client, ui); /* process the request */
close();
}
//
#define MAXLINE 512
// str_client is the client processing program
// particular to the application and depends on
// what the server is doing
void tcpip::str_client(tcpip_client* tcpip_x, Ui::tcpipClass ui)
{
int n;
char sendline[MAXLINE], recvline[MAXLINE + 1];
bool done=FALSE;
while (!done)
{
cout<<"enter keyboard string"<<endl;
cin.getline(sendline,256);
n = strlen(sendline);
if(n==0) break;
sendline[n] = '\n'; /* newline terminate */
sendline[n+1] = 0; /* newline terminate */
n++;
// use our client object to write to the server
ui.listWidgetClient->addItem(QString("client constructorstr_client"));
tcpip_x->tcp_write(sendline,n);
cout<<"sendline:"<<sendline<<endl;
/*
* Now read a line from the socket and write it to
* our standard output.
*/
n = tcpip_x->tcp_read(recvline);
recvline[n] = '\n'; /* null terminate */
recvline[n+1] = 0; /* null terminate */
n++;
cout<<"bytes received "<<n;
cout<<"client received:echo = "<<recvline<<endl;
}
}
void tcpip::currChanged(QWidget*){};
void tcpipThread::start()
{
tcpip_server* p_serv = new tcpip_server((int)19999,port,tcpip_server::str_echo, ui);
qDebug()<<"oops...back from CTOR";
delete p_serv;
}
To copy to clipboard, switch view to plain text mode
Bookmarks