我正在开发qt来开发套接字编程。我在这里发布代码。 “mainwindow.h”
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
namespace Ui {
class MainWindow;
}
static void handler(int sig, siginfo_t *si, void *uc);
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QTcpSocket *socket;
void timer();
void config();
private slots:
void newconnection();
private:
Ui::MainWindow *ui;
QTcpServer *server;
};
#endif // MAINWINDOW_H
“mainwindow.cpp”
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
static char i;
ui->setupUi(this);
sock=socket;
// if(!i)
// {
// connect(ui->start_button,SIGNAL(clicked()),this,SLOT(config()));
// }
// i=1;
qDebug()<<i;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow ::newconnection()
{
static int i;
socket=server->nextPendingConnection();
//connect(socket,SIGNAL(readyRead()),this,SLOT(timer_data_recieve()));
qDebug()<<"new connection";
qDebug()<<"server is reading..."<<socket->bytesAvailable();
qDebug()<<socket->readAll();
//time->start(1000); //QT timer started
if(!i)
timer();
i=1;
}
void MainWindow::timer()
{
timer_t t_id;
struct sigaction sa;
struct sigaction arg;
struct sigevent s_evnt;
struct itimerspec timer;
/* Establish handler for notification signal */
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = handler;
sigemptyset(&sa.sa_mask);
s_evnt.sigev_notify = SIGEV_SIGNAL;
s_evnt.sigev_signo = SIGRTMAX;
s_evnt.sigev_value.sival_ptr = &t_id;
if (sigaction(SIGRTMAX, &sa,NULL) == -1)
{
perror("sigaction");
}
//==============timer vlaues==============
timer.it_value.tv_sec=0;
timer.it_value.tv_nsec=20000000;
timer.it_interval.tv_sec=0;
timer.it_interval.tv_nsec=20000000;
if(timer_create(CLOCK_REALTIME,&s_evnt,&t_id)==-1);
perror("timer create");
if(timer_settime(t_id,0,&timer,NULL)==-1)
perror("timer_set_time");
}
void MainWindow::config()
{
//time=new QTimer(this);
server=new QTcpServer(this);
socket=new QTcpSocket(this);
connect(server,SIGNAL(newConnection()),this,SLOT(newconnection()));
// connect(time,SIGNAL(timeout()),this,SLOT(timer_data_sending()));
if(server->listen(QHostAddress::LocalHost,600))
{
qDebug()<<"server started";
}
else
qDebug()<<"server not started";
qDebug()<<MainWindow::server->errorString();
}
static void handler(int sig, siginfo_t *si, void *uc)
{
// MainWindow w;
qDebug("in handler");
// MainWindow *s = static_cast<MainWindow*>(&w);
static char first_call=1;
static unsigned sec;
static long nsec;
struct timespec start;
struct timespec curr;
if (first_call) {
first_call = 0;
if (clock_gettime(CLOCK_MONOTONIC, &start) == -1)
perror("clock_gettime");
}
if (clock_gettime(CLOCK_MONOTONIC, &curr) == -1)
perror("clock_gettime");
sec = (curr.tv_sec - start.tv_sec);
nsec = (curr.tv_nsec - start.tv_nsec);
if (nsec < 0)
{
sec--;
nsec += 1000000000;
}
start = curr;
}
“的main.cpp”
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
w.config();
return a.exec();
}
这里evrything工作正常处理程序。 但是如果想要访问处理程序数据到socket以发送到客户端,一种方法是声明socket是全局的。但是如何全局decalre以在mainwindow成员函数和处理程序中访问? 请建议我。这对我来说非常重要。请不要避免它。 注意:qtimer不在这里
答案 0 :(得分:0)
您可以为.ui-selectmenu-menu
课程使用Singletone Design Pattern
架构。您需要创建TCP Socket
extends
的课程。因此,您将只使用QTcpSocket
方法创建一个对象并使用get_instance()
方法全局访问,因为在此设计模式中它是public static
。您可以浏览Singleton design pattern
答案 1 :(得分:0)
documentation包含一个很好的例子,说明如何实现Singleton并将其从C ++导出到QML。