为什么套接字在gdb下失败
Why does socket fail under gdb?
突然socket
在gdb下崩溃(但工作正常)。只有当我初始化winsock时才会发生这种情况。我们非常感谢您的帮助。
#include <stdlib.h>
#include <stdio.h>
#include <locale.h>
#include <winsock2.h>
#include <Iphlpapi.h>
#ifdef _MSC_VER
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "IPHLPAPI.lib")
#endif
#include <windows.h>
unsigned char Network_isInitialized_ = 0;
#define SYSTEMINFORMATION_ISNATIVEWINDOWS 1
unsigned char Network_init_(){
#if SYSTEMINFORMATION_ISNATIVEWINDOWS
if (Network_isInitialized_){
return Network_isInitialized_;
}
WSADATA wsadata;
int error = WSAStartup(0x0202, &wsadata);
Network_isInitialized_ = error == 0 ? 1 : 0;
return Network_isInitialized_;
#else
return 1;
#endif
}
void Network_shutdown_(){
#if SYSTEMINFORMATION_ISNATIVEWINDOWS
if (Network_isInitialized_){
WSACleanup(); //Clean up Winsock
}
Network_isInitialized_ = 0;
#else
//Unix doesnt need to do anything
#endif
}
typedef struct TCP_Connection_s {
char *name;
int port;
unsigned int ip;
struct sockaddr_in *connection;
void *ptr;
unsigned int socket;
} TCP_Connection;
TCP_Connection* TCP_Connection_malloc(){
TCP_Connection *connection = (TCP_Connection*)malloc(sizeof(TCP_Connection));
memset(connection, 0, sizeof(*connection));
connection->connection = (struct sockaddr_in*) malloc (sizeof(struct sockaddr_in));
memset(connection->connection, 0, sizeof(*connection->connection));
return connection;
}
TCP_Connection* myconnect(TCP_Connection *connection){
if (connection == NULL){
return NULL;
}
connection->socket = socket(AF_INET, SOCK_STREAM, 0);
if (connection->socket+1 == 0){
return NULL;
}
int ret = connect(connection->socket, (struct sockaddr *)connection->connection, sizeof(*connection->connection));
if (ret != 0){
return NULL;
}
return connection;
}
TCP_Connection* connectToHost(TCP_Connection *connection, char *address, int port){
if (address == NULL){
return NULL;
}
if (connection == NULL){
connection = TCP_Connection_malloc();
}
struct hostent *he = (struct hostent *) gethostbyname(address);
int retryCount = 5;
while (he == NULL && retryCount > 0){
he = (struct hostent *) gethostbyname(address);
Sleep(2000);
retryCount = retryCount - 1;
}
if (he == NULL){
return NULL;
}
struct sockaddr_in temp;
memcpy(&(temp.sin_addr), he->h_addr, he->h_length);
connection->connection->sin_family = AF_INET;
connection->connection->sin_addr.s_addr = INADDR_ANY;
connection->connection->sin_port = htons(port);
connection->port = port;
memcpy(&(connection->connection->sin_addr), he->h_addr, he->h_length);
return myconnect(connection);
}
int main(int argc, char *argv[]){
//Random_seedCryptographic();
Network_init_();
connectToHost(NULL, "localhost", 1337);
Network_shutdown_();
printf("Press any key to continue...");
fflush(0);
getchar();
return 0;
}
$gdb test.exeGNU gdb(gdb)7.3.50.20111026-cvs(cygwin special)版权所有(C)2011自由软件基金会,股份有限公司。许可证GPLv3+:GNU GPL版本3或更高版本这是免费软件:你可以自由更改和重新分发它。在法律允许的范围内,不存在任何担保。键入"显示复制"以及"显示保修"以了解详细信息。此GDB被配置为"i686 cygwin"。有关错误报告说明,请参阅:。。。正在从/cygdrive/c/test.exe读取符号…已完成。(gdb)运行正在启动程序:/cygdrive/c/test.exe[新线程201280.0x60b68]dll路径太长[新线程201280.0x304b0]警告:无法加载//的共享库符号/globalroot/systemroot/syswow64/mswsock.dll。您需要"设置solib搜索路径"还是"设置sysroot"?程序接收到信号SIGTRAP,跟踪/断点陷阱。0x3567125a英寸??()来自/cygdrive/c/Windows/syswow64/mswsock.dll(gdb)bt#0 0x3567125a英寸??()来自/cygdrive/c/Windows/syswow64/mswsock.dll#来自/cygdrive/c/Windows/syswow64/WS2_32.dll的inet_ntoa()中的1 0x753b8b9d#来自/cygdrive/c/Windows/syswow64/WS2_32.dll的inet_ntoa()中的2 0x753b8972#来自/cygdrive/c/Windows/syswow64/WS2_32.dll的inet_ntoa()中的3 0x753b89da#WSCInstallProvider中的4 0x753b3d70()来自/cygdrive/c/Windows/syswow64/WS2_32.dll#5 0x00000002英寸??()#6 0x00000001英寸??()#7 0x00000000英寸??()(gdb)
更新:我尝试在mingw-portable下的另一个系统上编译同一个文件,结果成功了。然后我尝试在我的系统上安装mingw并卸载cygwin。。。XD 仍然不工作
这是因为Cygwin试图模拟UNIX环境,而不需要在UNIX上调用WSAStartup()
。Cygwin在后台自动完成这项工作,因此您正在复制Winsock的初始化/清理。例如,请参见此。
相关文章:
- 如果没有malloc,链表实现将失败
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- 视图中的参数推导失败:take_while
- 当在带有Eigen的C++中使用GDB时,我如何才能看到更多的大矩阵
- 如何找出GDB的SIGTRAP核心转储的根本原因
- 链接到自行创建的dll失败
- 带有特殊路径部分的"std::filesystem::weakly_canonical"失败
- GetShortPathName在网络驱动器上使用中文文件夹时失败
- gcc和c++17的过载解析失败
- 条件断点失败的 GDB 特征调试
- GDB在断点检查条件时失败
- VS Code 上的简单 GDB C++调试失败,并显示 NullReferenceException
- GDB断言失败
- 为什么套接字在gdb下失败
- 连接到gdb失败
- 调试失败,gdb: "Could not find the frame base" ,但使用 lldb
- 打破gdb中失败断言的正确方法是什么
- 我使用 gdb 跟踪'catch','syscall'失败了,它报告找不到"brk"?
- 我使用gdb调用一个简单的c++函数,但它失败了