吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 896|回复: 3
收起左侧

[求助] QT 服务器和客户端 连接不上

[复制链接]
djdgf4 发表于 2024-8-25 22:22
本帖最后由 djdgf4 于 2024-8-27 21:05 编辑

初学QT,写了个服务/客户端,但是怎么都连接不上
在connectToServer判断的时候没有输出,直接跳出去了,然后就是断连。在程序启动的时候就是表现为,服务端初始化完成,服务启动,然后客户端启动,但是连接不上。
我尝试使用telnet用两个终端各自连接服务器:
[Shell] 纯文本查看 复制代码
(base) Popen 127.0.0.1 1234sssssss> telnet 
 telnet> open 127.0.0.1 1234 
Trying 127.0.0.1... 
Connected to 127.0.0.1. 
Escape character is '^]'

. (WechatMSG) PS D:\GitClone\WeChatMsg> telnet 
telnet> open 127.0.0.1 1234 
Trying 127.0.0.1... 
Connected to 127.0.0.1. 
Escape character is '^]'. 
1 


并且发送消息,但是没有其他反应,也无法显示客户端id,显然client没有来连接上
[Shell] 纯文本查看 复制代码
 (base) PS C:\Users\Theonesssssssss> telnet 
telnet> open 127.0.0.1 12345 
Trying 127.0.0.1... 
Connected to 127.0.0.1. 
Escape character is '^]'. 

 

服务端存在Server: started.字样,但是没有Client connected: 输出使用client.exe程序的时候,点击connect没有反应,点击disconnect会在client界面出现Disconnected from server. Attempting to reconnect...
我客户端的出错位置的逻辑是
[C++] 纯文本查看 复制代码
void TCPClient::connectToServer()
{
    qDebug() << "Attempting to connect to server...";
    socket->connectToHost("127.0.0.1", 1234);  // 连接到本地主机的1234端口
        if (socket->waitForConnected(3000)) {
        displayArea->append("Connected to server.");
    } else {
                displayArea->append("Failed to connect to server. Error: " + socket->errorString());
    }
}

void TCPClient::onConnectClicked()
{
    qDebug() << "Connect button clicked.";
    connectToServer();  // 连接到服务器
}


我是直接使用qmake编译的,因为不太会建立QT工程
[Asm] 纯文本查看 复制代码
make clean && qmake client.pro && make -k
make clean && qmake server.pro && make -k


(base) PS C:\Users\Theonesssssssss> Get-NetTCPConnection -LocalPort 1234

LocalAddress                        LocalPort RemoteAddress                       RemotePort State       AppliedSetting OwningProcess
------------                        --------- -------------                       ---------- -----       -------------- -------------
::                                  1234      ::                                  0          Listen                     12708
127.0.0.1                           1234      127.0.0.1                           59759      Established Internet       11812
127.0.0.1                           1234      0.0.0.0                             0          Listen                     11812


附上代码
client.rar || https://前缀1drv.ms/u/s!AuA35fjcaGBNiHmBQGwfyNF-EFSR?e=uOYMRx

server.rar   ||  1drv.ms/u/s!AuA35fjcaGBNiHpiRDVRG0z4pP0U?e=dDwn9P

这里是我的调试信息,
[Shell] 纯文本查看 复制代码
(Theonesssssssss@Stein-Gate)[2854] ~/TCP/tcp1 - Copy (2)/client % gdb debug/./client.exe
GNU gdb (GDB) 15.1
Copyright (C) 2024 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-w64-mingw32".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from debug/./client.exe...
(gdb) b connectToServer
Breakpoint 1 at 0x140001d3c: file client.cpp, line 50.
(gdb) r
Starting program: D:\msys64\home\Theonesssssssss\TCP\tcp1 - Copy (2)\client\debug\client.exe
[New Thread 184.0x6a1c]
[New Thread 184.0x7214]
[New Thread 184.0x463c]
[New Thread 184.0x68d4]
[New Thread 184.0x7e74]
[New Thread 184.0x2680]
[New Thread 184.0x2008]
[Thread 184.0x2008 exited with code 0]
[New Thread 184.0x4b08]
[Thread 184.0x4b08 exited with code 0]
[New Thread 184.0x7c78]
[New Thread 184.0x373c]
[New Thread 184.0x6190]
warning: Connect button clicked.

Thread 1 hit Breakpoint 1, TCPClient::connectToServer (this=0x5ffd80) at client.cpp:50
warning: Source file is more recent than executable.
50          qDebug() << "Attempting to connect to server...";
(gdb) l 47,64
47
48      void TCPClient::connectToServer()
49      {
50          qDebug() << "Attempting to connect to server...";
51          socket->connectToHost("127.0.0.1", 1234);  // 杩炴帴鍒版湰鍦颁富鏈虹殑1234 绔彛
52              if (socket->waitForConnected(3000)) {
53              displayArea->append("Connected to server.");
54          } else {
55                      displayArea->append("Failed to connect to server. Error: " + socket->errorString());
56          }
57      }
58
59      void TCPClient::onConnectClicked()
60      {
61          qDebug() << "Connect button clicked.";
62          connectToServer();  // 杩炴帴鍒版湇鍔″櫒
63      }
64
(gdb) n
warning: Attempting to connect to server...
51          socket->connectToHost("127.0.0.1", 1234);  // 杩炴帴鍒版湰鍦颁富鏈虹殑1234 绔彛
(gdb) bt
#0  TCPClient::connectToServer (this=0x5ffd80) at client.cpp:51
#1  0x00007ff779101eb8 in TCPClient::onConnectClicked (this=0x5ffd80) at client.cpp:57
#2  0x00007ff779104899 in QtPrivate::FunctorCall<QtPrivate::IndexesList<>, QtPrivate::List<>, void, void (TCPClient::*)()>::call(void (TCPClient::*)(), TCPClient*, void**) (
    f=(void (TCPClient::*)(TCPClient * const)) 0x7ff779101e46 <TCPClient::onConnectClicked()>, o=0x5ffd80, arg=0x5fbf20)
    at D:/msys64/mingw64/include/QtCore/qobjectdefs_impl.h:152
#3  0x00007ff779104a8c in QtPrivate::FunctionPointer<void (TCPClient::*)()>::call<QtPrivate::List<>, void>(void (TCPClient::*)(), TCPClient*, void**) (
    f=(void (TCPClient::*)(TCPClient * const)) 0x7ff779101e46 <TCPClient::onConnectClicked()>, o=0x5ffd80, arg=0x5fbf20)
    at D:/msys64/mingw64/include/QtCore/qobjectdefs_impl.h:185
#4  0x00007ff77910494a in QtPrivate::QSlotObject<void (TCPClient::*)(), QtPrivate::List<>, void>::impl(int, QtPrivate::QSlotObjectBase*, QObject*, void**, bool*) (which=1,
    this_=0xe2dd00, r=0x5ffd80, a=0x5fbf20, ret=0x0)
    at D:/msys64/mingw64/include/QtCore/qobjectdefs_impl.h:418
#5  0x00007ffa0d39257c in ?? () from D:\msys64\mingw64\bin\Qt5Core.dll
#6  0x00007ffa06b336c2 in ?? () from D:\msys64\mingw64\bin\Qt5Widgets.dll
#7  0x00007ffa06b352c6 in ?? () from D:\msys64\mingw64\bin\Qt5Widgets.dll
#8  0x00007ffa06b354bf in ?? () from D:\msys64\mingw64\bin\Qt5Widgets.dll
#9  0x00007ffa06a88690 in ?? () from D:\msys64\mingw64\bin\Qt5Widgets.dll
#10 0x00007ffa06a4853e in ?? () from D:\msys64\mingw64\bin\Qt5Widgets.dll
#11 0x00007ffa06a5058b in ?? () from D:\msys64\mingw64\bin\Qt5Widgets.dll
#12 0x00007ffa0d2a4298 in ?? () from D:\msys64\mingw64\bin\Qt5Core.dll
#13 0x00007ffa06a4e3ff in ?? () from D:\msys64\mingw64\bin\Qt5Widgets.dll
#14 0x00007ffa06a9f61b in ?? () from D:\msys64\mingw64\bin\Qt5Widgets.dll
#15 0x00007ffa06aa2a07 in ?? () from D:\msys64\mingw64\bin\Qt5Widgets.dll
#16 0x00007ffa06a4853e in ?? () from D:\msys64\mingw64\bin\Qt5Widgets.dll
#17 0x00007ffa0d2a4298 in ?? () from D:\msys64\mingw64\bin\Qt5Core.dll
#18 0x00007ffa04a37bd4 in ?? () from D:\msys64\mingw64\bin\Qt5Gui.dll
#19 0x00007ffa04a0eecc in ?? () from D:\msys64\mingw64\bin\Qt5Gui.dll
#20 0x00007ffa0d300b02 in ?? () from D:\msys64\mingw64\bin\Qt5Core.dll
#21 0x00007ffa0bd44185 in ?? ()
   from D:\msys64\mingw64\share\qt5\plugins\platforms\qwindows.dll
#22 0x00007ffa0d2a25a3 in ?? () from D:\msys64\mingw64\bin\Qt5Core.dll
#23 0x00007ffa0d2ab3a5 in ?? () from D:\msys64\mingw64\bin\Qt5Core.dll
#24 0x00007ff7791014a3 in qMain (argc=1, argv=0xf03f80) at main.cpp:9
#25 0x00007ff779103a0f in WinMain ()
#26 0x00007ff7791012e9 in __tmainCRTStartup ()
    at C:/M/B/src/mingw-w64/mingw-w64-crt/crt/crtexe.c:258
#27 0x00007ff7791013d6 in WinMainCRTStartup ()
    at C:/M/B/src/mingw-w64/mingw-w64-crt/crt/crtexe.c:148
(gdb) n
152         virtual void connectToHost(const QString &hostName, quint16 port, OpenMode mode = ReadWrite, NetworkLayerProtocol protocol = AnyIPProtocol);
(gdb) n
51          socket->connectToHost("127.0.0.1", 1234);  // 杩炴帴鍒版湰鍦颁富鏈虹殑1234 绔彛
(gdb) n
warning: onecore\net\netprofiles\service\src\nsp\dll\namespaceserviceprovider.cpp(613)\nlansp_c.dll!00007FFAB782F6BD: (caller: 00007FFAE0CEACF6) LogHr(1) tid(968) 8007277C No such service is known. The service cannot be found in the specified name space.
[New Thread 184.0x7cb8]
52              if (socket->waitForConnected(3000)) {
(gdb) n
TCPClient::onConnectClicked (this=0x5ffd80) at client.cpp:58
58
(gdb) n
QtPrivate::FunctorCall<QtPrivate::IndexesList<>, QtPrivate::List<>, void, void (TCPClient::*)()>::call(void (TCPClient::*)(), TCPClient*, void**) (
    f=(void (TCPClient::*)(TCPClient * const)) 0x7ff779101e46 <TCPClient::onConnectClicked()>, o=0x5ffd80, arg=0x5fbf20)
    at D:/msys64/mingw64/include/QtCore/qobjectdefs_impl.h:153
153             }
(gdb) n
QtPrivate::FunctionPointer<void (TCPClient::*)()>::call<QtPrivate::List<>, void>(void (TCPClient::*)(), TCPClient*, void**) (
    f=(void (TCPClient::*)(TCPClient * const)) 0x7ff779101e46 <TCPClient::onConnectClicked()>, o=0x5ffd80, arg=0x5fbf20)
    at D:/msys64/mingw64/include/QtCore/qobjectdefs_impl.h:186
186             }
(gdb) n
QtPrivate::QSlotObject<void (TCPClient::*)(), QtPrivate::List<>, void>::impl(int, QtPrivate::QSlotObjectBase*, QObject*, void**, bool*) (which=1, this_=0xe2dd00,
    r=0x5ffd80, a=0x5fbf20, ret=0x0)
    at D:/msys64/mingw64/include/QtCore/qobjectdefs_impl.h:419
419                     break;
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) n
Program not restarted.
(gdb) n
425             }
(gdb) l
420                 case Compare:
421                     *ret = *reinterpret_cast<Func *>(a) == static_cast<QSlotObject*>(this_)->function;
422                     break;
423                 case NumOperations: ;
424                 }
425             }
426         public:
427             explicit QSlotObject(Func f) : QSlotObjectBase(&impl), function(f) {}
428         };
429         // implementation of QSlotObjectBase for which the slot is a functor (or lambda)

这里可以看到直接跳出来了

Run till exit from #0  0x00007ffa0bd44185 in ?? ()
   from D:\msys64\mingw64\share\qt5\plugins\platforms\qwindows.dll
0x00007ffa0d2a25a3 in ?? () from D:\msys64\mingw64\bin\Qt5Core.dll
(gdb) finish
Run till exit from #0  0x00007ffa0d2a25a3 in ?? ()
   from D:\msys64\mingw64\bin\Qt5Core.dll
warning: Disconnect button clicked.
warning: Disconnected from server. Attempting to reconnect...
warning: Disconnect button clicked.
[Thread 184.0x2680 exited with code 0]
[Thread 184.0x463c exited with code 0]
[Thread 184.0x6a1c exited with code 0]
[Thread 184.0x7214 exited with code 0]




image.png

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

ZoRoLOVE 发表于 2024-8-26 06:24
下一个调试工具,用调试工具分别当做服务器、客户端和你的客户端、服务器连接,看看是哪的问题。服务器开启端口监听了没有?Qt Creator 里有tcp通讯的实例,你可以看看官方例子。
liuenzhuo 发表于 2024-8-26 08:17
 楼主| djdgf4 发表于 2024-8-26 23:16
ZoRoLOVE 发表于 2024-8-26 06:24
下一个调试工具,用调试工具分别当做服务器、客户端和你的客户端、服务器连接,看看是哪的问题。服务器开启 ...

我用了Ai写了个脚本开启防火墙和端口监听,但是,结果是相同的。
然后我尝试用Wireshark捕获,问了ai是以

> 运行你的服务器和客户端:

在捕获进行时,启动你的服务器和客户端。可以使用你编写的程序,或者使用 netcat 来创建一个简单的服务器和客户端连接。例如:
    在一个终端中作为服务器监听:

    bash

nc -l 127.0.0.1 1234

在另一个终端中作为客户端连接:

bash

nc 127.0.0.1 1234

发送和接收一些消息来生成流量。


但是发现这样获得的好像是两个终端之间的流量,我开不开服务器都不影响。不太懂网络这块,不知道怎么排查
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-24 12:36

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表