Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么在gdb下套接字会失败?_C++_Windows_Gdb_Winsock - Fatal编程技术网

C++ 为什么在gdb下套接字会失败?

C++ 为什么在gdb下套接字会失败?,c++,windows,gdb,winsock,C++,Windows,Gdb,Winsock,在gdb下,socket突然崩溃(但正常工作)。只有在初始化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") #

在gdb下,
socket
突然崩溃(但正常工作)。只有在初始化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;
}
#包括
#包括
#包括
#包括
#包括
#ifdef硕士学位
#pragma注释(lib,“ws2_32.lib”)
#pragma注释(lib,“iphlapi.lib”)
#恩迪夫
#包括
无符号字符网络被初始化为0;
#在本机Windows 1中定义系统信息
无符号字符网络{
#如果系统信息是本机Windows
if(网络初始化){
返回网络初始化;
}
WSADATA WSADATA;
int error=WSAStartup(0x0202,&wsadata);
网络初始化错误=0?1:0;
返回网络初始化;
#否则
返回1;
#恩迪夫
}
无效网络关闭{
#如果系统信息是本机Windows
if(网络初始化){
WSACleanup();//清理Winsock
}
网络初始值=0;
#否则
//Unix不需要做任何事情
#恩迪夫
}
类型定义结构TCP_连接{
字符*名称;
国际港口;
无符号整数ip;
*连接中的结构sockaddr_;
无效*ptr;
无符号整数套接字;
}TCP_连接;
TCP_连接*TCP_连接_malloc(){
TCP_连接*连接=(TCP_连接*)malloc(sizeof(TCP_连接));
memset(连接,0,sizeof(*连接));
连接->连接=(struct sockaddr_in*)malloc(sizeof(struct sockaddr_in));
memset(connection->connection,0,sizeof(*connection->connection));
回路连接;
}
TCP_连接*myconnect(TCP_连接*连接){
if(连接==NULL){
返回NULL;
}
连接->套接字=套接字(AF\u INET,SOCK\u STREAM,0);
如果(连接->套接字+1==0){
返回NULL;
}
int ret=connect(connection->socket,(struct sockaddr*)connection->connection,sizeof(*connection->connection));
如果(ret!=0){
返回NULL;
}
回路连接;
}
TCP_连接*连接到主机(TCP_连接*连接,字符*地址,int端口){
如果(地址==NULL){
返回NULL;
}
if(连接==NULL){
连接=TCP_连接_malloc();
}
struct hostent*he=(struct hostent*)gethostbyname(地址);
int-retryCount=5;
while(he==NULL&&retryCount>0){
he=(结构主机*)gethostbyname(地址);
睡眠(2000年);
retryCount=retryCount-1;
}
if(he==NULL){
返回NULL;
}
温度中的结构sockaddr_;
memcpy(&(临时sin_addr),he->h_addr,he->h_length);
连接->连接->单家庭=AF\u INET;
连接->连接->sin\u addr.s\u addr=INADDR\u ANY;
连接->连接->单端口=htons(端口);
连接->端口=端口;
memcpy(&(connection->connection->sin_addr),he->h_addr,he->h_length);
返回myconnect(连接);
}
int main(int argc,char*argv[]){
//随机_-seedCryptographic();
网络初始化();
connectToHost(NULL,“localhost”,1337);
网络关闭();
printf(“按任意键继续…”);
fflush(0);
getchar();
返回0;
}
$gdb test.exe GNU gdb(gdb)7.3.50.20111026-cvs(cygwin特别版) 版权所有(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搜索路径”还是“设置系统根”? 程序接收信号信号陷阱,跟踪/断点陷阱。 0x3567125a英寸??()来自/cygdrive/c/Windows/syswow64/mswsock.dll (gdb)英国电信 #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的初始化/清理。例如,请参见。

您是否尝试过gdb告诉您的内容:
您需要“设置solib搜索路径”还是“设置系统根”?
@NikolaiNFetissov我尝试过,但它只消除了一条错误消息,程序仍然在那里崩溃。没有消失的错误是“dll路径太长”。您是否使用了winsock库?请注意,
TCP\u connection\s
结构的
connection
成员实际上不需要作为指针。当您需要指向它的指针时,您可以使用(
&
)运算符的地址。如果您从该点开始
继续
,会发生什么情况? $ gdb test.exe GNU gdb (GDB) 7.3.50.20111026-cvs (cygwin-special) Copyright (C) 2011 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later 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 "i686-cygwin". For bug reporting instructions, please see: ... Reading symbols from /cygdrive/c/test.exe...done. (gdb) run Starting program: /cygdrive/c/test.exe [New Thread 201280.0x60b68] dll path too long [New Thread 201280.0x304b0] warning: Could not load shared library symbols for //./globalroot/systemroot/syswow64/mswsock.dll. Do you need "set solib-search-path" or "set sysroot"? Program received signal SIGTRAP, Trace/breakpoint trap. 0x3567125a in ?? () from /cygdrive/c/Windows/syswow64/mswsock.dll (gdb) bt #0 0x3567125a in ?? () from /cygdrive/c/Windows/syswow64/mswsock.dll #1 0x753b8b9d in inet_ntoa () from /cygdrive/c/Windows/syswow64/WS2_32.dll #2 0x753b8972 in inet_ntoa () from /cygdrive/c/Windows/syswow64/WS2_32.dll #3 0x753b89da in inet_ntoa () from /cygdrive/c/Windows/syswow64/WS2_32.dll #4 0x753b3d70 in WSCInstallProvider () from /cygdrive/c/Windows/syswow64/WS2_32.dll #5 0x00000002 in ?? () #6 0x00000001 in ?? () #7 0x00000000 in ?? () (gdb)