QT前台与后台程序通过socket通信

本文转自:http://blog.chinaunix.net/u3/103866/showart_2099036.html  

 

通常情况下在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;
}

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值