gpt4 book ai didi

c++ - 为什么socket在gdb下会失败?

转载 作者:可可西里 更新时间:2023-11-01 14:36:52 26 4
gpt4 key购买 nike

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)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) runStarting 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)

更新:我尝试在 mingw portable 下的不同系统上编译相同的文件并且成功了。然后我尝试在我的系统上安装 mingw 并卸载 cygwin ...仍然不起作用 XD

最佳答案

那是因为 Cygwin 试图模拟 UNIX 环境,您不需要在 UNIX 上调用 WSAStartup()。 Cygwin 在后台自动执行此操作,因此您正在重复 Winsock 的初始化/清理。参见 this ,例如。

关于c++ - 为什么socket在gdb下会失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11497465/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com