#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
sendNum = 0;
recvNum = 0;
recentRecvNum = 0;
recentSendNum = 0;
stop = false;
time = new QTimer();
time->setInterval(1000);
connect(time,SIGNAL(timeout()),this,SLOT(sendSpeed()));
time1 = new QTimer();
time1->setInterval(1000);
connect(time1,SIGNAL(timeout()),this,SLOT(recvSpeed()));
initData();
initSock();
}
MainWindow::~MainWindow()
{
delete ui;
stop = true;
}
void MainWindow::initSock()
{
WSADATA wsaData;
WORD version=MAKEWORD(2,0);
int ret=WSAStartup(version,&wsaData);
if(ret!=0)
printf("初始化失败");
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(sock == INVALID_SOCKET)
{
printf("winsocket error !");
return;
}
socksend = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(socksend == INVALID_SOCKET)
{
printf("winsocket error !");
return;
}
struct timeval t;
t.tv_sec = 5;
t.tv_usec = 0;
setsockopt(sock,SOL_SOCKET,SO_RCVTIMEO ,(const char *)&t,sizeof (t));
setsockopt(socksend,SOL_SOCKET,SO_SNDTIMEO ,(const char *)&t,sizeof (t));
}
void MainWindow::sendMsg()
{
qDebug()<<"开始发送数据!";
while(1){
if(stop){
break;
}
int nRetSent = sendto(socksend, sendData, 65507 , 0, (sockaddr *)&addr, addr_len);
if(nRetSent == 0){
WSACleanup();
closesocket(sock);
char errBuf[16];
qDebug()<< itoa(WSAGetLastError(), errBuf, sizeof(errBuf));
}
if(nRetSent>0){
qDebug()<<"send success ,ip:"<<inet_ntoa(addr.sin_addr)<<" port:"<<ntohs(addr.sin_port)<<" nRetSent"<<nRetSent;
sendMutex.lock();
sendNum++;
sendMutex.unlock();
}
}
}
void MainWindow::recvMsg()
{
qDebug()<<"开始接收数据!";
int length = 64*1024;
char buffer[length];
struct sockaddr_in cliaddr;
int len = sizeof(cliaddr);
while(1){
if(stop){
break;
}
int count = recvfrom(sock,buffer,length,0,(LPSOCKADDR)&cliaddr,&len);
if(count > 0){
recvMutex.lock();
recvNum++;
recvMutex.unlock();
qDebug()<<count<< "recv success ,ip:"<<inet_ntoa(cliaddr.sin_addr)<<" port:"<<ntohs(cliaddr.sin_port);
}else{
}
}
}
void MainWindow::initData()
{
QVector<u32> vec;
QByteArray msg;
for(u32 i = 0 ;i <16384 ; i++){
vec<<i;
msg.append((const char *)&vec, sizeof(vec));
}
sendData = msg.data();
qDebug()<<sizeof(vec[0])*vec.size()<<" "<<msg.size();
}
void MainWindow::CloseSocket()
{
if(WSACleanup()!=0)
printf("clean失败");
closesocket(sock);
}
void MainWindow::on_pushButton_clicked()
{
stop = false;
sendNum = 0;
recvNum = 0;
recentRecvNum = 0;
recentSendNum = 0;
QtConcurrent::run(this,&MainWindow::sendMsg);
QtConcurrent::run(this,&MainWindow::recvMsg);
time->start();
time1->start();
}
void MainWindow::on_pushButton_stop_clicked()
{
stop = true;
time->stop();
time1->stop();
qDebug()<<"结束!";
}
void MainWindow::sendSpeed()
{
double speed,currentSendNum;
sendMutex.lock();
currentSendNum = sendNum;
sendMutex.unlock();
speed = (currentSendNum-recentSendNum)*60/1024;
recentSendNum = currentSendNum;
ui->lineEdit_send->setText(QString::number(speed));
}
void MainWindow::recvSpeed()
{
double speed,currentRecvNum;
recvMutex.lock();
currentRecvNum = recvNum;
recvMutex.unlock();
speed = (currentRecvNum-recentRecvNum)*60/1024;
recentRecvNum = currentRecvNum;
ui->lineEdit_recv->setText(QString::number(speed));
}
void MainWindow::on_pushButton_connect_clicked()
{
src_addr.sin_family = AF_INET;
src_addr.sin_port = htons(10041);
src_addr.sin_addr.S_un.S_addr = inet_addr(ui->lineEdit_ip_local->text().toLatin1());
src_addr_len = sizeof(src_addr);
if(bind(sock, (struct sockaddr *)&src_addr, src_addr_len) == SOCKET_ERROR)
{
qDebug()<<"bind error!";
return;
}
addr.sin_family = AF_INET;
addr.sin_port = htons(10041);
addr.sin_addr.S_un.S_addr = inet_addr(ui->lineEdit_ip->text().toLatin1());
addr_len = sizeof(addr);
}
|