📄 socket.cpp
字号:
#include "socket.h"Dialog::Dialog( QWidget* parent, const char* name, bool modal, WFlags fl ) : QDialog( parent, name, modal, fl ){ if ( !name ) setName( "Dialog" ); ipAddressLineEdit = new QLineEdit( this, "ipAddressLineEdit" ); ipAddressLineEdit->setGeometry( QRect( 100, 10, 160, 41 ) ); connectButton = new QPushButton( this, "connectButton" ); connectButton->setGeometry( QRect( 410, 10, 120, 51 ) ); connectButton->setFocusPolicy( QPushButton::NoFocus ); exitButton = new QPushButton( this, "exitButton" ); exitButton->setGeometry( QRect( 400, 360, 131, 61 ) ); exitButton->setFocusPolicy( QPushButton::NoFocus ); textEdit= new QTextEdit( this, "textEdit" ); textEdit->setGeometry( QRect( 40, 170, 321, 221 ) ); disconnectButton = new QPushButton( this, "disconnectButton" ); disconnectButton->setGeometry( QRect( 540, 10, 121, 50 ) ); disconnectButton->setFocusPolicy( QPushButton::NoFocus ); portLineEdit = new QLineEdit( this, "portLineEdit" ); portLineEdit->setGeometry( QRect( 310, 10, 91, 41 ) ); portLabel = new QLabel( this, "portLabel" ); portLabel->setGeometry( QRect( 270, 10, 29, 41 ) ); ipAddressLabel = new QLabel( this, "ipAddressLabel" ); ipAddressLabel->setGeometry( QRect( 20, 10, 70, 41 ) ); writeLabel = new QLabel( this, "writeLabel" ); writeLabel->setGeometry( QRect( 20, 90, 91, 41 ) ); writeLineEdit = new QLineEdit( this, "writeLineEdit" ); writeLineEdit->setGeometry( QRect( 121, 90, 260, 41 ) ); goButton = new QPushButton( this, "goButton" ); goButton->setGeometry( QRect( 400, 90, 101, 41 ) ); languageChange(); resize( QSize(640, 444).expandedTo(minimumSizeHint()) ); socket=new QSocket(this,"socket"); connect(socket,SIGNAL(readyRead()),this,SLOT(socketReadData())); connect(socket,SIGNAL(connected()),this,SLOT(socketConnectedOK())); connect(socket,SIGNAL(connectionClosed()),this,SLOT(socketClosed())); connect( connectButton, SIGNAL( clicked() ), this, SLOT( connectSlot() ) ); connect( disconnectButton, SIGNAL( clicked() ), this, SLOT( disconnectSlot() ) ); connect( exitButton, SIGNAL( clicked() ), this, SLOT( exitSlot() ) ); connect( goButton, SIGNAL( clicked() ), this, SLOT( sendData() ) );}Dialog::~Dialog(){ socket->close();}void Dialog::languageChange(){ setCaption( tr( "Form1" ) ); connectButton->setText( tr( "Connect to Host" ) ); exitButton->setText( tr( "Exit Program" ) ); disconnectButton->setText( tr( "Disconnect" ) ); portLabel->setText( tr( "port:" ) ); ipAddressLabel->setText( tr( "IP address:" ) ); writeLabel->setText( tr( "Write To Socket:" ) ); writeLineEdit->setText( tr( "O,yes~~~" ) ); goButton->setText( tr( "GO" ) );}void Dialog::connectSlot(){ if(ipAddressLineEdit->text().isEmpty())return; if(portLineEdit->text().isEmpty())return; QString ipAddress=ipAddressLineEdit->text().stripWhiteSpace(); QString portString=portLineEdit->text().stripWhiteSpace(); QHostAddress hostAddress; if(!hostAddress.setAddress(ipAddress)) return; bool b; int port=portString.toInt(&b); if(!b)return; socket->connectToHost(ipAddress,port);}void Dialog::disconnectSlot(){ textEdit->append(QString("socket's state is:")+QString::number(socket->state())); socket->close();}void Dialog::exitSlot(){ socket->close(); emit quit();}void Dialog::sendData(){
int i=0;
int k=0; if( socket->state()!=QSocket::Connection)
return;
QString newfile = QFileDialog::getOpenFileName();
QFile f(newfile);
f.open(IO_ReadWrite);
int p=f.size();
QDataStream s(&f);
QString str;
s>>str;
while(i<p)
{
if(k=socket->writeBlock(str,500)>=0)
i+=k;
}}void Dialog::socketReadData(){ char *netin="in.jp2";
do
{
socket->readBlock(netin,socket->bytesAvailable() );
}while(socket->atEnd()==TRUE)
}void Dialog::socketConnectedOK(){ textEdit->append("socket connected OK!\n");}void Dialog::socketClosed(){ textEdit->append("socket connection closed!\n");}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -