//************* PROVASOCKET.H ***********************
#ifndef PROVASOCKET_H
#define PROVASOCKET_H
#include <QtGui/QMainWindow>
#include <QAbstractSocket>
#include "ui_provasocket.h"
{
Q_OBJECT
public:
provasocket
(QWidget *parent
= 0, Qt
::WFlags flags
= 0);
~provasocket();
private:
Ui::provasocketClass ui;
private slots:
void Connected(void);
void Disconnected(void);
};
#endif // PROVASOCKET_H
//************* PROVASOCKET.H ***********************
#ifndef PROVASOCKET_H
#define PROVASOCKET_H
#include <QtGui/QMainWindow>
#include <QAbstractSocket>
#include "ui_provasocket.h"
class provasocket : public QMainWindow
{
Q_OBJECT
public:
provasocket(QWidget *parent = 0, Qt::WFlags flags = 0);
~provasocket();
private:
Ui::provasocketClass ui;
private slots:
void Connected(void);
void Error(QAbstractSocket::SocketError e);
void Disconnected(void);
};
#endif // PROVASOCKET_H
To copy to clipboard, switch view to plain text mode
//************* PROVASOCKET.CPP ***********************
#include "provasocket.h"
#include <QTcpSocket>
#include <QDebug>
#include <winsock2.h>
#include <mstcpip.h>
#include <ws2tcpip.h>
#include <ws2ipdef.h>
#include <QApplication>
#include "threadread.h"
#pragma comment(lib, "Ws2_32.lib")
ThreadRead* lpThreadRead;
provasocket
::provasocket(QWidget *parent, Qt
::WFlags flags
){
ui.setupUi(this);
lpThreadRead = new ThreadRead();
connect(lpSocket,SIGNAL(connected()),this,SLOT(Connected()));
lpSocket->connectToHost("192.168.15.111",0xbbbb);
}
provasocket::~provasocket()
{
delete lpSocket;
}
void provasocket::Connected(void)
{
tcp_keepalive structSettings;
int bOptLen = sizeof (BOOL);
BOOL bOptVal = TRUE;
int nSocketDesctriptor = lpSocket->socketDescriptor();
DWORD nBytes = 0;
structSettings.onoff = 1;
structSettings.keepaliveinterval = 500;
structSettings.keepalivetime = 1000;
int nRet = ::WSAIoctl(nSocketDesctriptor, SIO_KEEPALIVE_VALS,&structSettings,sizeof(structSettings),NULL,0,&nBytes,NULL,NULL ); // succeeds, r==0
if (nRet == SOCKET_ERROR) //errori da gestire teo da fare forse
{
int nErr = WSAGetLastError();
nRet = -1;
}
nRet = setsockopt(nSocketDesctriptor,SOL_SOCKET,SO_KEEPALIVE,(char *) &bOptVal,bOptLen);
if (nRet == SOCKET_ERROR)
{
int nErr = WSAGetLastError();
nRet = -2;
}
int nVal = 0;
setsockopt(nSocketDesctriptor, IPPROTO_IP, IP_DONTFRAGMENT,(char *) &nVal, sizeof(nVal));
connect(lpSocket,SIGNAL(disconnected()),this,SLOT(Disconnected()));
lpThreadRead->start();
}
//----------------------------------------------------------------------------
{
qDebug() << "Error";
lpSocket->disconnectFromHost();
}
//----------------------------------------------------------------------------
void provasocket::Disconnected(void)
{
qDebug() << "Disconnected";
}
//----------------------------------------------------------------------------
//************* PROVASOCKET.CPP ***********************
#include "provasocket.h"
#include <QTcpSocket>
#include <QDebug>
#include <winsock2.h>
#include <mstcpip.h>
#include <ws2tcpip.h>
#include <ws2ipdef.h>
#include <QApplication>
#include "threadread.h"
#pragma comment(lib, "Ws2_32.lib")
QTcpSocket* lpSocket;
ThreadRead* lpThreadRead;
provasocket::provasocket(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
lpSocket = new QTcpSocket();
lpThreadRead = new ThreadRead();
connect(lpSocket,SIGNAL(connected()),this,SLOT(Connected()));
lpSocket->connectToHost("192.168.15.111",0xbbbb);
}
provasocket::~provasocket()
{
delete lpSocket;
}
void provasocket::Connected(void)
{
tcp_keepalive structSettings;
int bOptLen = sizeof (BOOL);
BOOL bOptVal = TRUE;
int nSocketDesctriptor = lpSocket->socketDescriptor();
DWORD nBytes = 0;
structSettings.onoff = 1;
structSettings.keepaliveinterval = 500;
structSettings.keepalivetime = 1000;
int nRet = ::WSAIoctl(nSocketDesctriptor, SIO_KEEPALIVE_VALS,&structSettings,sizeof(structSettings),NULL,0,&nBytes,NULL,NULL ); // succeeds, r==0
if (nRet == SOCKET_ERROR) //errori da gestire teo da fare forse
{
int nErr = WSAGetLastError();
nRet = -1;
}
nRet = setsockopt(nSocketDesctriptor,SOL_SOCKET,SO_KEEPALIVE,(char *) &bOptVal,bOptLen);
if (nRet == SOCKET_ERROR)
{
int nErr = WSAGetLastError();
nRet = -2;
}
int nVal = 0;
setsockopt(nSocketDesctriptor, IPPROTO_IP, IP_DONTFRAGMENT,(char *) &nVal, sizeof(nVal));
connect(lpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(Error(QAbstractSocket::SocketError)));
connect(lpSocket,SIGNAL(disconnected()),this,SLOT(Disconnected()));
lpThreadRead->start();
}
//----------------------------------------------------------------------------
void provasocket::Error(QAbstractSocket::SocketError e)
{
qDebug() << "Error";
lpSocket->disconnectFromHost();
}
//----------------------------------------------------------------------------
void provasocket::Disconnected(void)
{
qDebug() << "Disconnected";
}
//----------------------------------------------------------------------------
To copy to clipboard, switch view to plain text mode
//************* PROVASOCKET.H ***********************
#ifndef THREADREAD_H
#define THREADREAD_H
#include <QThread>
{
Q_OBJECT
public:
ThreadRead();
~ThreadRead();
volatile bool bExit;
protected:
void run(void);
};
#endif // THREADREAD_H
//************* PROVASOCKET.CPP ***********************
#include "threadread.h"
#include <QTcpSocket>
#include <QApplication>
ThreadRead::ThreadRead()
{
}
ThreadRead::~ThreadRead()
{
}
void ThreadRead::run(void)
{
bExit = false;
while (!bExit)
{
while (lpSocket->waitForReadyRead(5000))
{
if (bExit)
{
return;
}
}
}
}
//************* PROVASOCKET.H ***********************
#ifndef THREADREAD_H
#define THREADREAD_H
#include <QThread>
class ThreadRead : public QThread
{
Q_OBJECT
public:
ThreadRead();
~ThreadRead();
volatile bool bExit;
protected:
void run(void);
};
#endif // THREADREAD_H
//************* PROVASOCKET.CPP ***********************
#include "threadread.h"
#include <QTcpSocket>
#include <QApplication>
extern QTcpSocket* lpSocket;
ThreadRead::ThreadRead()
: QThread()
{
}
ThreadRead::~ThreadRead()
{
}
void ThreadRead::run(void)
{
bExit = false;
while (!bExit)
{
while (lpSocket->waitForReadyRead(5000))
{
if (bExit)
{
return;
}
QApplication::processEvents();
}
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks