通常情况下在linux上写程序不需要GUI进行展示,但有时做测试还是有个前台界面更加方便,省去了很多繁琐的输入过程,同时也更易于观察输出内容。因此这两天开始回顾了一下QT的东西。做为基础的功能,首先做了个QT和后台进程(c语言实现)交互的模块,在此基础上今后再针对具体需求做些修改便可完成前后台之间的配合。因为QT本身是跨平台的框架,因此以后前端程序移植到其他平台依然能很好的运行。
QT前台: window下客户端, 通过执行ip和端口发送字符串并等待接收。使用QT提供的对socket封装过
的类QTcpSocket和相关函数
后台进程: 虚拟机linux上c语言实现,通过系统的socket函数接收字符串,并将字符串中的小写字符转换为大
写,并返回给客户端。
QT前台截图:
后台截图:
前台代码:
main.cpp
1 2 3 4 5 6 7 8 9 10 11 |
#include "dialog.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Dialog w; w.show(); return a.exec(); } |
dialog.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include <QLabel> #include <QLineEdit> #include <QTextEdit> #include <QTcpSocket> #include <QPushButton> class Dialog : public QDialog { Q_OBJECT public: Dialog(QWidget *parent = 0); ~Dialog(); private: QLabel* remoteIPLabel; QLabel* remotePortLabel; QLabel* sendStringLabel; QLabel* serverReturnLabel; QLineEdit* remoteIPLineEdit; QLineEdit* remotePortLineEdit; QLineEdit* sendStringLineEdit; QTextEdit* serverReturnTextEdit; QPushButton* sendBtn; QTcpSocket* tcpSocket; private slots: void errMsg(QAbstractSocket::SocketError); void onSendBtnClicked(); void recvMsg(); }; #endif // DIALOG_H |
dialog.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
#include "dialog.h" #include <QHostAddress> #include <QGridLayout> #include <QHBoxLayout> #include <QVBoxLayout> Dialog::Dialog(QWidget *parent) : QDialog(parent) { remoteIPLabel = new QLabel(tr("对方IP:")); remotePortLabel = new QLabel(tr("对方端口:")); sendStringLabel = new QLabel(tr("发送字符串:")); serverReturnLabel = new QLabel(tr("服务端返回:")); remoteIPLineEdit = new QLineEdit; remotePortLineEdit = new QLineEdit; sendStringLineEdit = new QLineEdit(tr("I am in beijing")); serverReturnTextEdit = new QTextEdit(tr("")); sendBtn = new QPushButton(tr("发送")); tcpSocket = new QTcpSocket(this); remoteIPLineEdit->setText("192.168.0.20"); remotePortLineEdit->setText("2010"); connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(errMsg(QAbstractSocket::SocketError))); connect(sendBtn, SIGNAL(clicked()), this, SLOT(onSendBtnClicked())); connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(recvMsg())); QGridLayout* leftLayout = new QGridLayout; leftLayout->addWidget(remoteIPLabel, 0, 0); leftLayout->addWidget(remoteIPLineEdit, 0, 1); leftLayout->addWidget(remotePortLabel, 1, 0); leftLayout->addWidget(remotePortLineEdit, 1, 1); leftLayout->addWidget(sendStringLabel, 2, 0); leftLayout->addWidget(sendStringLineEdit, 2, 1); leftLayout->addWidget(serverReturnLabel, 3, 0, 1, 2); leftLayout->addWidget(serverReturnTextEdit, 4, 0, 1, 2); QVBoxLayout* rightLayout = new QVBoxLayout; rightLayout->addWidget(sendBtn); rightLayout->addStretch(); QHBoxLayout* mainLayout = new QHBoxLayout; mainLayout->addLayout(leftLayout); mainLayout->addLayout(rightLayout); setLayout(mainLayout); } Dialog::~Dialog() { } void Dialog::errMsg(QAbstractSocket::SocketError) { qWarning("this is err!!!"); } void Dialog::onSendBtnClicked() { QString port; int port_Int; port = remotePortLineEdit->text(); port_Int = port.toInt(); tcpSocket->connectToHost(QHostAddress(remoteIPLineEdit->text()), port_Int); //waitForConnected的参数是超时时间 if (!tcpSocket->waitForConnected(2000000)) { qDebug() << "超时"; tcpSocket->disconnectFromHost(); return; } // QTextStream out(tcpSocket); // out << sendStringLineEdit->text() << endl; QString sendStr; sendStr = sendStringLineEdit->text(); qDebug() << "send: " << sendStr; tcpSocket->write(sendStr.toUtf8()); } void Dialog::recvMsg() { QString res; QByteArray tmp; tmp = tcpSocket->readAll(); res += QString::fromUtf8(tmp); qDebug() << "res:" << res; serverReturnTextEdit->setText(tmp); serverReturnTextEdit->moveCursor(QTextCursor::End); tcpSocket->disconnectFromHost(); } |
QtFront.pro
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#------------------------------------------------- # # Project created by QtCreator 2015-06-30T17:17:06 # #------------------------------------------------- QT += core gui QT += network greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = QtFront TEMPLATE = app SOURCES += main.cpp\ dialog.cpp HEADERS += dialog.h |
后台代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/socket.h> #include <arpa/inet.h> #define RCV_BUF_LEN 200 int main() { int sock; int client_Sock; int ret; char inputBuf[200]; char recvBuf[RCV_BUF_LEN]; struct sockaddr_in svrAddr; unsigned int i; int val = 1; /** 创建监听套接字 **/ if ( (sock = socket(AF_INET, SOCK_STREAM, 0)) < 0 ){ printf("socket error!\n"); return -1; } /** 指定监听端口和地址 **/ memset( &svrAddr, 0x00, sizeof(svrAddr) ); svrAddr.sin_family =AF_INET; svrAddr.sin_addr.s_addr = INADDR_ANY; svrAddr.sin_port = htons( 2010 ); setsockopt(sock,SOL_SOCKET,SO_REUSEADDR, (char *)&val, sizeof(val) ); /** 绑定监听套接字和端口 **/ if ( bind(sock, (struct sockaddr *)&svrAddr, sizeof(svrAddr)) != 0 ){ printf("bind error!\n"); return -1; } /** 监听套接字 */ if ( listen( sock, 10 ) != 0 ){ printf("listen error!\n"); return -1; } while( 1 ){ /** 等待连接请求 **/ printf("等待客户端连接请求..\n"); if ( (client_Sock = accept( sock, NULL, 0 )) < 0 ){ printf("accept error!\n"); return -1; } printf("客户端已连入..\n"); while( 1 ){ memset( recvBuf, 0x00, RCV_BUF_LEN ); ret = recv( client_Sock, recvBuf, RCV_BUF_LEN, 0 ); recvBuf[strlen(recvBuf)] = 0; if ( ret > 0 ){ printf("接收到: [%s]\n", recvBuf); for( i = 0; i < strlen(recvBuf); i++ ){ if ( recvBuf[i] > 96 && recvBuf[i] < 123 ){ recvBuf[i] -= 32; } } ret = send( client_Sock, recvBuf, strlen(recvBuf), 0 ); if ( ret > 0 ){ printf("处理并返回成功..\n"); } else if ( ret < 0 ){ printf("返回客户端错误..\n"); } } else if ( ret == 0 ){ printf("客户端断开..\n"); break; } else{ printf("rcv error!\n"); break; } } } return 0; } |
发表评论
要发表评论,您必须先登录。