Chinaunix首页 | 论坛 | 博客
  • 博客访问: 288843
  • 博文数量: 103
  • 博客积分: 2345
  • 博客等级: 大尉
  • 技术积分: 902
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-01 09:10
文章分类

全部博文(103)

文章存档

2022年(1)

2018年(3)

2017年(2)

2016年(3)

2015年(8)

2014年(8)

2013年(2)

2012年(9)

2011年(29)

2010年(20)

2009年(18)

我的朋友

分类: LINUX

2012-08-02 15:45:38

通常情况下在Linux上写程序不需要GUI进行展示,但有时做测试还是有个前台界面更加方便,省去了很多繁琐的输入过程,同时也更易于观察输出内容。因此这两天开始回顾了一下QT的东西。做为基础的功能,首先做了个QT和后台进程(c语言实现)交互的模块,在此基础上今后再针对具体需求做些修改便可完成前后台之间的配合。因为QT本身是跨平台的框架,因此以后前端程序移植到其他平台依然能很好的运行。

QT前台: Windows下客户端,通过执行ip和端口发送字符串并等待接收,使用QT提供的对socket封装过的类QTcpSocket和相关函数。

后台进程: 虚拟机Linux上c语言实现,通过系统的socket函数接收字符串,并将字符串中的小写字符转换为大写,并返回给客户端。

1. 我们新建Qt4 Gui Application,工程名为“dialog”,Base class选择QDialog。

在工程文件 dialog.pro 的QT += 一行中添加一个 空格 和 network,dialog.pro 内容如下:

#-------------------------------------------------

#

# Project created by QtCreator 2011-06-30T23:00:22

#

#-------------------------------------------------

QT += core gui network

TARGET = dialog

TEMPLATE = app

SOURCES += main.cpp\

dialog.cpp

HEADERS += dialog.h

FORMS += dialog.ui

2. dialog.ui 的设计如下:

QT前台运行截图:

后台运行截图:

前台代码:

dialog.h

#ifndef DIALOG_H

#define DIALOG_H

#include

#include

namespace Ui {

class Dialog;

}

class Dialog : public QDialog

{

Q_OBJECT

public:

explicit Dialog(QWidget *parent = 0);

~Dialog();

public slots:

void on_pushButton_clicked();

void recvMsg();

void errMsg(QAbstractSocket::SocketError);

private:

Ui::Dialog *ui;

QTcpSocket *tcpSocket;

};

#endif // DIALOG_H

dialog.cpp

Dialog::Dialog(QWidget *parent): QDialog(parent), ui(new Ui::Dialog)

{

ui->setupUi(this);

tcpSocket = new QTcpSocket(this);

ui->lineEdit->setText("192.168.1.100");

ui->lineEdit_2->setText( "2010" );

connect( tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),

this, SLOT(errMsg(QAbstractSocket::SocketError)));

connect( tcpSocket, SIGNAL(readyRead()), this, SLOT(recvMsg()) );

}

Dialog::~Dialog()

{

delete ui;

}

void Dialog::on_pushButton_clicked()

{

QString port;

int port_Int;

port = ui->lineEdit_2->text();

port_Int = port.toInt();

tcpSocket->connectToHost(QHostAddress(ui->lineEdit->text()), port_Int);

//waitForConnected的参数是超时时间

if (!tcpSocket->waitForConnected(2)) {

tcpSocket->disconnectFromHost();

return;

}

QTextStream out(tcpSocket);

out << ui->lineEdit_3->text()<< endl;

}

void Dialog::recvMsg()

{

QString res;

res += tcpSocket->readAll();

ui->textEdit->setText(res);

ui->textEdit->moveCursor(QTextCursor::End);

tcpSocket->disconnectFromHost();

}

void Dialog::errMsg( QAbstractSocket::SocketError )

{

qWarning( "this is err!!!!" );

}

后台代码:

#include

#include

#include

#include

#include

#define RCV_BUF_LEN 200

int main( int argc, char *argv[] )

{

int sock;

int client_Sock;

int ret;

char recvBuf[RCV_BUF_LEN];

struct sockaddr_in svrAddr;

int i;

int val;

/** 创建监听套接字 **/

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 = htonl( 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;

}

阅读(1523) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~