hlp:640x480-小屏vx4齿轮泵代码提交
This commit is contained in:
@@ -0,0 +1,210 @@
|
||||
#include "filetranseceiver.h"
|
||||
#include <QDebug>
|
||||
#include <QFile>
|
||||
#include <QString>
|
||||
FileTranseceiver::FileTranseceiver()
|
||||
{
|
||||
// struct sockaddr_in server_addr;
|
||||
// bzero(&server_addr, sizeof(server_addr)); // 置字节字符串前n个字节为0,包括'\0'
|
||||
// server_addr.sin_family = AF_INET;
|
||||
// server_addr.sin_addr.s_addr = htons(INADDR_ANY); // 转小端,INADDR_ANY就是指定地址为0.0.0.0的地址
|
||||
// server_addr.sin_port = htons(5000);
|
||||
serverThead = NULL;
|
||||
sockfd = socket(PF_INET, SOCK_STREAM, 0);
|
||||
|
||||
struct sockaddr_in seraddr;
|
||||
seraddr.sin_family = AF_INET;
|
||||
seraddr.sin_port = htons(5001);
|
||||
seraddr.sin_addr.s_addr = htons(INADDR_ANY);
|
||||
int tmp = 1;
|
||||
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof(int));
|
||||
if(bind(sockfd,(struct sockaddr *)&seraddr,sizeof(seraddr)) == -1)
|
||||
{
|
||||
qDebug() << "tftp server bind error";
|
||||
return;
|
||||
}
|
||||
|
||||
if(listen(sockfd,5) == -1)
|
||||
{
|
||||
qDebug() << "listen error";
|
||||
return;
|
||||
}
|
||||
serverThead = new QThread();
|
||||
moveToThread(serverThead);
|
||||
QObject::connect(serverThead, SIGNAL(started()), this, SLOT(connectListen()));
|
||||
//connect(&m_thread, SIGNAL(finished()), this, SLOT(deleteLater()));
|
||||
// serverThead->start();
|
||||
// qDebug()<<"tftp server on...";
|
||||
}
|
||||
FileTranseceiver::~FileTranseceiver(void)
|
||||
{
|
||||
if(serverThead) {
|
||||
disconnect(serverThead, SIGNAL(started()), this, SLOT(connectListen()));
|
||||
if (serverThead->isRunning()) {
|
||||
serverThead->quit();
|
||||
serverThead->wait();
|
||||
}
|
||||
}
|
||||
if(connfd)
|
||||
close(connfd);
|
||||
if(sockfd)
|
||||
close(sockfd);
|
||||
}
|
||||
void FileTranseceiver::start()
|
||||
{
|
||||
if(serverThead)
|
||||
{
|
||||
serverThead->start();
|
||||
}
|
||||
qDebug()<<"tftp server on...";
|
||||
}
|
||||
|
||||
void FileTranseceiver::connectListen()
|
||||
{
|
||||
|
||||
struct msg info;
|
||||
struct sockaddr_in cliaddr;
|
||||
int len = sizeof(cliaddr);
|
||||
pid_t pid;
|
||||
int n;
|
||||
QString fileName;
|
||||
do
|
||||
{
|
||||
connfd = accept(sockfd,(struct sockaddr *)&cliaddr,(socklen_t *)&len);
|
||||
if(connfd == -1)
|
||||
{
|
||||
qDebug() << "accept";
|
||||
continue;
|
||||
}
|
||||
|
||||
// pid = fork();
|
||||
// if(pid == -1)
|
||||
// {
|
||||
// qDebug() << "fork";
|
||||
// continue;
|
||||
// }
|
||||
// else if(pid == 0)
|
||||
{
|
||||
//close(sockfd);
|
||||
while(1)
|
||||
{
|
||||
n = recv(connfd,&info,sizeof(info),0);
|
||||
if(n == -1)
|
||||
{
|
||||
qDebug() << "recv";
|
||||
//exit(EXIT_FAILURE);
|
||||
break;
|
||||
}
|
||||
if(n == 0)
|
||||
{
|
||||
qDebug() << "客户端退出";
|
||||
//exit(EXIT_SUCCESS);
|
||||
break;
|
||||
}
|
||||
switch(info.type)
|
||||
{
|
||||
// case SEARCH:
|
||||
// search_serverdir(connfd,info);
|
||||
// break;
|
||||
case DOWN:
|
||||
ftp_download(connfd,info);
|
||||
break;
|
||||
case UP:
|
||||
upload_ftp(connfd,info);
|
||||
fileName = QString(info.fname);
|
||||
qDebug()<<"emit fileRecieved(fileName)";
|
||||
emit fileRecieved(fileName);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
close(connfd);
|
||||
}
|
||||
// else
|
||||
// {
|
||||
// close(connfd);
|
||||
// }
|
||||
}
|
||||
while(1);
|
||||
}
|
||||
|
||||
//将某文件中的内容传给客户端
|
||||
int FileTranseceiver::ftp_download(int connfd,struct msg info)
|
||||
{
|
||||
FILE *fp;
|
||||
|
||||
char name[100];
|
||||
memset(name,0,100);
|
||||
sprintf(name, DOC_DIR"/%s",info.fname);
|
||||
// QFile dl_file(QString::fromUtf8(name));
|
||||
// if (!dl_file.exists()) {
|
||||
// return -1;
|
||||
// }
|
||||
//qDebug()<<"receive file name:"<<QString(name);
|
||||
fp = fopen(name,"r");
|
||||
if(fp == NULL)
|
||||
{
|
||||
info.flag = FAIL;
|
||||
send(connfd,&info,sizeof(info),0);
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
info.flag = SUCCESS;
|
||||
send(connfd,&info,sizeof(info),0);
|
||||
}
|
||||
|
||||
unsigned int len;
|
||||
while(1)
|
||||
{
|
||||
len = fread(info.buf,1,sizeof(info.buf),fp);
|
||||
send(connfd,info.buf,len,0);
|
||||
|
||||
if(len < sizeof(info.buf) && feof(fp) != 0)
|
||||
break;
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
//接收客户端上传的文件内容
|
||||
int FileTranseceiver::upload_ftp(int connfd,struct msg info)
|
||||
{
|
||||
FILE *fp;
|
||||
char name[100];
|
||||
//sprintf(name, DOC_DIR"/%s",info.fname);
|
||||
sprintf(name, "/tmp/%s",info.fname);
|
||||
// QFile ul_file(QString::fromUtf8(name));
|
||||
// if (!ul_file.exists()) {
|
||||
// return -1;
|
||||
// }
|
||||
//create and open file
|
||||
fp = fopen(name,"w");
|
||||
if(fp == NULL)
|
||||
{
|
||||
info.flag = FAIL;
|
||||
send(connfd,&info,sizeof(info),0);
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
info.flag = SUCCESS;
|
||||
send(connfd,&info,sizeof(info),0);
|
||||
}
|
||||
//recv file contant
|
||||
unsigned int len;
|
||||
while(1)
|
||||
{
|
||||
len = recv(connfd,info.buf,sizeof(info.buf),0);
|
||||
fwrite(info.buf,1,len,fp);
|
||||
if(len < sizeof(info.buf))
|
||||
break;
|
||||
}
|
||||
fclose(fp);
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
#ifndef FILETRANSECEIVER_H
|
||||
#define FILETRANSECEIVER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QThread>
|
||||
#include<stdio.h>
|
||||
#include<sys/types.h>
|
||||
#include<sys/socket.h>
|
||||
#include<netinet/in.h>
|
||||
#include<arpa/inet.h>
|
||||
#include<string.h>
|
||||
#include<stdlib.h>
|
||||
#include<unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
|
||||
#define DOC_DIR "/home/plg/doc"
|
||||
#define SEARCH 100
|
||||
#define DOWN 200
|
||||
#define END 500
|
||||
#define FAIL 300
|
||||
#define SUCCESS 400
|
||||
#define UP 600
|
||||
|
||||
struct msg
|
||||
{
|
||||
int type;
|
||||
int flag;
|
||||
char buf[64];
|
||||
char fname[100];
|
||||
};
|
||||
class FileTranseceiver : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit FileTranseceiver();
|
||||
~FileTranseceiver(void);
|
||||
|
||||
int ftp_download(int connfd,struct msg info);
|
||||
int upload_ftp(int connfd,struct msg info);
|
||||
void start();
|
||||
protected slots:
|
||||
void connectListen();
|
||||
private:
|
||||
int sockfd;
|
||||
int connfd;;
|
||||
QThread *serverThead;
|
||||
signals:
|
||||
void fileRecieved(QString fileName);
|
||||
};
|
||||
|
||||
#endif // FILETRANSECEIVER_H
|
||||
Reference in New Issue
Block a user