Skip to content

Commit 63fa45c

Browse files
authored
Merge pull request #173 from Lj-dog/master
增加TCP客户端连接后,客户端IP与端口提示
2 parents 9b94811 + 1d681e2 commit 63fa45c

6 files changed

Lines changed: 912 additions & 817 deletions

File tree

COMTool/conn/conn_tcp_udp.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class TCP_UDP(COMM):
4040
id = "tcp_udp"
4141
name = "TCP UDP"
4242
showSwitchSignal = pyqtSignal(ConnectionStatus)
43+
showLocalIPandPortSignal = pyqtSignal(str)
4344
updateTargetSignal = pyqtSignal(str)
4445
updateClientsSignal = pyqtSignal(bool, tuple)
4546
def onInit(self, config):
@@ -108,6 +109,8 @@ def onWidget(self):
108109
self.modeLabel = QLabel(_("Mode"))
109110
self.targetLabel = QLabel(_("Target"))
110111
self.targetCombobox = ComboBox()
112+
self.selfIPandPortLabel = QLabel(_("Local"))
113+
self.selfIPandPort = QLineEdit()
111114
self.targetCombobox.setEditable(True)
112115
self.portLabel = QLabel(_("Port"))
113116
self.portLabel.hide()
@@ -141,14 +144,16 @@ def onWidget(self):
141144
self.serialSettingsLayout.addWidget(modeWidget, 1, 1, 1, 2)
142145
self.serialSettingsLayout.addWidget(self.targetLabel, 2, 0)
143146
self.serialSettingsLayout.addWidget(self.targetCombobox, 2, 1, 1, 2)
144-
self.serialSettingsLayout.addWidget(self.portLabel, 3, 0)
145-
self.serialSettingsLayout.addWidget(self.porttEdit, 3, 1, 1, 2)
146-
self.serialSettingsLayout.addWidget(self.clientsCombobox, 4, 0, 1, 2)
147-
self.serialSettingsLayout.addWidget(self.disconnetClientBtn, 4, 2, 1, 1)
148-
self.serialSettingsLayout.addWidget(self.autoReconnetLable, 5, 0, 1, 1)
149-
self.serialSettingsLayout.addWidget(self.autoReconnect, 5, 1, 1, 1)
150-
self.serialSettingsLayout.addWidget(self.autoReconnectIntervalEdit, 5, 2, 1, 1)
151-
self.serialSettingsLayout.addWidget(self.serialOpenCloseButton, 6, 0, 1, 3)
147+
self.serialSettingsLayout.addWidget(self.selfIPandPortLabel, 3, 0)
148+
self.serialSettingsLayout.addWidget(self.selfIPandPort,3,1,1,2)
149+
self.serialSettingsLayout.addWidget(self.portLabel, 4, 0)
150+
self.serialSettingsLayout.addWidget(self.porttEdit, 4, 1, 1, 2)
151+
self.serialSettingsLayout.addWidget(self.clientsCombobox, 5, 0, 1, 2)
152+
self.serialSettingsLayout.addWidget(self.disconnetClientBtn, 5, 2, 1, 1)
153+
self.serialSettingsLayout.addWidget(self.autoReconnetLable, 6, 0, 1, 1)
154+
self.serialSettingsLayout.addWidget(self.autoReconnect, 6, 1, 1, 1)
155+
self.serialSettingsLayout.addWidget(self.autoReconnectIntervalEdit, 6, 2, 1, 1)
156+
self.serialSettingsLayout.addWidget(self.serialOpenCloseButton, 7, 0, 1, 3)
152157
serialSetting.setLayout(self.serialSettingsLayout)
153158
self.widgetConfMap["protocol"] = [self.protoclTcpRadioBtn, self.protoclUdpRadioBtn]
154159
self.widgetConfMap["mode"] = self.modeClientRadioBtn
@@ -164,6 +169,7 @@ def initEvet(self):
164169
self.serialOpenCloseButton.clicked.connect(self.openCloseSerial)
165170
self.porttEdit.textChanged.connect(self.onPortChanged)
166171
self.showSwitchSignal.connect(self.showSwitch)
172+
self.showLocalIPandPortSignal.connect(self.selfIPandPort.setText)
167173
self.updateTargetSignal.connect(self.updateTarget)
168174
self.updateClientsSignal.connect(self.updateClients)
169175
self.protoclTcpRadioBtn.clicked.connect(lambda: self.changeProtocol("tcp"))
@@ -188,6 +194,8 @@ def changeProtocol(self, protocol, init=False):
188194
else:
189195
self.targetCombobox.show()
190196
self.targetLabel.show()
197+
self.selfIPandPortLabel.show()
198+
self.selfIPandPort.show()
191199
self.porttEdit.show()
192200
self.portLabel.show()
193201
self.clientsCombobox.hide()
@@ -208,6 +216,8 @@ def changeMode(self, mode, init=False):
208216
if mode == "server":
209217
self.targetCombobox.hide()
210218
self.targetLabel.hide()
219+
self.selfIPandPortLabel.hide()
220+
self.selfIPandPort.hide()
211221
self.porttEdit.show()
212222
self.portLabel.show()
213223
self.clientsCombobox.show()
@@ -218,6 +228,8 @@ def changeMode(self, mode, init=False):
218228
else:
219229
self.targetCombobox.show()
220230
self.targetLabel.show()
231+
self.selfIPandPortLabel.show()
232+
self.selfIPandPort.show()
221233
self.porttEdit.hide()
222234
self.portLabel.hide()
223235
self.clientsCombobox.hide()
@@ -339,6 +351,7 @@ def openCloseSerial(self):
339351
def openCloseSerialProcess(self):
340352
if self.isOpened:
341353
print("-- disconnect")
354+
# self.selfIPandPort.setText("")
342355
try:
343356
# set status first to prevent auto reconnect
344357
self.status = ConnectionStatus.CLOSED
@@ -360,6 +373,7 @@ def openCloseSerialProcess(self):
360373
except Exception as e:
361374
print("openCloseSerialProcess", e)
362375
pass
376+
self.showLocalIPandPortSignal.emit("")
363377
self.onConnectionStatus.emit(self.status, "")
364378
self.showSwitchSignal.emit(self.status)
365379
else:
@@ -376,8 +390,14 @@ def openCloseSerialProcess(self):
376390
self.conn.connect(target)
377391
self.status = ConnectionStatus.CONNECTED
378392
print("-- connect success")
393+
# 获取客户端的 IP 地址和端口号
394+
client_ip, client_port = self.conn.getsockname()
395+
# self.selfIPandPort.setText(f"{client_ip}:{client_port}")
396+
self.showLocalIPandPortSignal.emit(f"{client_ip}:{client_port}")
397+
print(f"Client IP: {client_ip}, Client Port: {client_port}")
379398
self.receiveProcess = threading.Thread(target=self.receiveDataProcess, args=(self.conn, ))
380399
self.receiveProcess.setDaemon(True)
400+
381401
self.receiveProcess.start()
382402
else:
383403
print("-- server mode, wait client connect")

0 commit comments

Comments
 (0)