Chinaunix首页 | 论坛 | 博客
  • 博客访问: 38249
  • 博文数量: 20
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 145
  • 用 户 组: 普通用户
  • 注册时间: 2016-03-25 09:44
个人简介

unix

文章分类

全部博文(20)

文章存档

2016年(20)

我的朋友

分类: C/C++

2016-05-09 09:19:55

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

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

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


QT前台截图:


后台截图:



前台代码:

#include "dialog.h"
#include "ui_dialog.h"


Dialog::Dialog(QWidget *parent)
    : QDialog(parent), ui(new Ui::Dialog)
{
    ui->setupUi(this);
    tcpSocket = new QTcpSocket( this );
    ui->lineEdit->setText("192.168.244.66");
    ui->lineEdit_2->setText( "2010" );

    connect( tcpSocket, SIGNAL(error(int)), this, SLOT(errMsg(int)) );
    connect( ui->pushButton, SIGNAL(clicked()), this, SLOT(on_pushButton_clicked(bool)) );
    connect( tcpSocket, SIGNAL(readyRead()), this, SLOT(recvMsg()) );
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::on_pushButton_clicked(bool checked)
{
        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( int errNo )
{
   qWarning( "this is err!!!!" );
}


后台代码:

#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 ac, char **av )
{
        int sock;
        int client_Sock;
        int ret;
        char inputBuf[200];
        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;
        }

        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) - 1] = 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;
}


阅读(849) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:QT 使用QTcpSocket的相关操作

给主人留下些什么吧!~~