-
Notifications
You must be signed in to change notification settings - Fork 0
/
mytcpclient.cpp
54 lines (49 loc) · 1.66 KB
/
mytcpclient.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "mytcpclient.h"
#include <string>
MyTcpClient::MyTcpClient( QWidget *parent = 0 ) {
tcpClient = new QTcpSocket( this ); //实例化tcpClient
tcpClient->abort(); //取消原有连接
connect( tcpClient, SIGNAL( readyRead() ), this, SLOT( ReadData() ) );
connect( tcpClient, SIGNAL( error( QAbstractSocket::SocketError ) ), this,
SLOT( ReadError( QAbstractSocket::SocketError ) ) );
}
void MyTcpClient::connect() {
tcpClient->connectToHost( 192.168.1.101,
81 ); // 服务端
if ( !tcpClient->waitForConnected( 1000 ) ) // 连接成功则进入if{}
{
throw string( "connected error" );
}
}
void MyTcpClient::disconnect() {
tcpClient->disconnectFromHost();
if ( !( tcpClient->state() == QAbstractSocket::UnconnectedState
|| tcpClient->waitForDisconnected(
1000 ) ) ) //已断开连接则进入if{}
{
throw string( "disconnect error" );
}
}
void MyTcpClient::ReadData() {
QByteArray buffer = tcpClient->readAll();
if ( !buffer.isEmpty() ) {
pass;
}
}
void MyTcpClient::ReadError( QAbstractSocket::SocketError ) {
tcpClient->disconnectFromHost();
QMessageBox msgBox;
msgBox.setText( tr( "failed to connect server because %1" )
.arg( tcpClient->errorString() ) );
xec();
}
void MyTcpClient::SendData() {
QString data = //待填入
if ( data != "" ) {
tcpClient->write( data.toLatin1() ); //发送
}
}
MyTcpClient::~MyTcpClient() {
disconnect();
//可能要delete指针
}