-
Notifications
You must be signed in to change notification settings - Fork 0
/
mytcpsever.cpp
98 lines (89 loc) · 3.63 KB
/
mytcpsever.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "mytcpsever.h"
MyTcpSever::MyTcpSever() {
tcpServer = new QTcpServer( this );
ui->edtIP->setText(
QNetworkInterface().allAddresses().at( 1 ).toString() ); //获取本地IP
ui->btnConnect->setEnabled( true );
ui->btnSend->setEnabled( false );
connect( tcpServer, SIGNAL( newConnection() ), this,
SLOT( NewConnectionSlot() ) );
bool ok =
tcpServer->listen( QHostAddress::Any, ui->edtPort->text().toInt() );
if ( ok ) {
ui->btnConnect->setText( "断开" );
ui->btnSend->setEnabled( true );
}
}
void MyTcpServer::NewConnectionSlot() {
currentClient = tcpServer->nextPendingConnection();
tcpClient.append( currentClient );
ui->cbxConnection->addItem(
tr( "%1:%2" )
.arg(
currentClient->peerAddress().toString().split( "::ffff:" )[1] )
.arg( currentClient->peerPort() ) );
connect( currentClient, SIGNAL( readyRead() ), this, SLOT( ReadData() ) );
connect( currentClient, SIGNAL( disconnected() ), this,
SLOT( disconnectedSlot() ) );
}
void MyTcpServer::disconnectedSlot() {
for ( int i = 0; i < tcpClient.length(); i++ ) {
for ( int i = 0; i < tcpClient.length(); i++ ) //断开所有连接
{
tcpClient[i]->disconnectFromHost();
bool ok = tcpClient[i]->waitForDisconnected( 1000 );
if ( !ok ) {
// 处理异常
}
tcpClient.removeAt( i ); //从保存的客户端列表中取去除
}
tcpServer->close(); //不再监听端口
if ( tcpClient[i]->state() == QAbstractSocket::UnconnectedState ) {
// 删除存储在combox中的客户端信息
ui->cbxConnection->removeItem( ui->cbxConnection->findText(
tr( "%1:%2" )
.arg( tcpClient[i]->peerAddress().toString().split(
"::ffff:" )[1] )
.arg( tcpClient[i]->peerPort() ) ) );
// 删除存储在tcpClient列表中的客户端信息
tcpClient[i]->destroyed();
tcpClient.removeAt( i );
}
}
}
void MyTcpServer::ReadData() {
// 由于readyRead信号并未提供SocketDecriptor,所以需要遍历所有客户端
for ( int i = 0; i < tcpClient.length(); i++ ) {
QByteArray buffer = tcpClient[i]->readAll();
if ( buffer.isEmpty() )
continue;
static QString IP_Port, IP_Port_Pre;
IP_Port = tr( "[%1:%2]:" )
.arg( tcpClient[i]->peerAddress().toString().split(
"::ffff:" )[1] )
.arg( tcpClient[i]->peerPort() );
// 若此次消息的地址与上次不同,则需显示此次消息的客户端地址
if ( IP_Port != IP_Port_Pre )
ui->edtRecv->append( IP_Port );
ui->edtRecv->append( buffer );
//更新ip_port
IP_Port_Pre = IP_Port;
}
}
void MyTcpServer::on_btnSend_clicked() {
if ( ui->cbxConnection->currentIndex() == 0 ) {
for ( int i = 0; i < tcpClient.length(); i++ )
tcpClient[i]->write( data.toLatin1() ); // qt5除去了.toAscii()
}
//指定连接
QString clientIP = ui->cbxConnection->currentText().split( ":" )[0];
int clientPort = ui->cbxConnection->currentText().split( ":" )[1].toInt();
for ( int i = 0; i < tcpClient.length(); i++ ) {
if ( tcpClient[i]->peerAddress().toString().split( "::ffff:" )[1]
== clientIP
&& tcpClient[i]->peerPort() == clientPort ) {
tcpClient[i]->write( data.toLatin1() );
return; // ip:port唯一,无需继续检索
}
}
}