qt5 tcp收发数据(服务端.ccp)

#ifndef SERVER_H
#define SERVER_H
 
#include <QWidget>
#include<QTcpServer>
#include<QTcpSocket>
#include<QDataStream>
#include<QFileDialog>
#include<QTextCodec>
#include<QString>
#include<QFile>
#include<windows.h>
#include<QThread>
#include<QThread>
 
typedef struct
{
  int start;
  char buf[512];
  int end;
}info_t;
namespace Ui {
class server;
}
 
class server : public QWidget
{
    Q_OBJECT
 
public:
    explicit server(QWidget *parent = 0);
    ~server();
    void connect_client_slot();
    void read_message_slot();
    void thread_message_slot();
 
private:
    Ui::server *ui;
    QTcpServer *tcpServer;
    QTcpSocket *confd;
};
#endif // SERVER_H
 
#include "server.h"
#include "ui_server.h"
info_t a;
QList<QTcpSocket*> list_inof;
server::server(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::server)
{
    ui->setupUi(this);
    tcpServer = new QTcpServer(this);
    if(!tcpServer->listen( QHostAddress::Any,6666))
    {
        qDebug()<<tcpServer->errorString();
    }
    connect(tcpServer,&QTcpServer::newConnection,this,&server::connect_client_slot);
 
}
 
void server::connect_client_slot()
{
    confd =tcpServer->nextPendingConnection();
    list_inof.append(confd);
    connect(confd,&QTcpSocket::readyRead,this,&server::read_message_slot);
}
 
server::~server()
{
    delete ui;
}
 
void server::read_message_slot()
{
    int i;
    for( i=0;i<list_inof.length();i++)
    {
        unsigned char judge[6] = {0};
        list_inof.at(i)->read((char*)judge ,6);
        QString message((char*)judge);
        if(!(message.isEmpty()))
        {
            break;
        }
    }
    FILE *fd=fopen("C:/Users/Hanjianyu/Desktop/try/XRPSG1_20151230_0000_DQ.WPD","r");
    a.start=68;
    a.end=16;
    while(fread(a.buf,sizeof(char),512,fd)>0)
    {
       list_inof.at(i)->write((char*)&a,sizeof(a));
       Sleep(1);
       memset(a.buf,0,sizeof(a.buf));
 
   }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值