gpt4 book ai didi

c - 函数没有返回它应该返回的地方

转载 作者:行者123 更新时间:2023-11-30 15:35:27 25 4
gpt4 key购买 nike

好的,我有一个客户端正在尝试连接到服务器。我调用下面的函数。在函数完成其工作并即将返回后,客户端程序崩溃。我使用了调试器,发现了一件非常不寻常的事情。在 return 语句控件跳转到未知函数之后(据我了解)。 IDE (Code::Blocks) 在客户端崩溃之前打印此内容:

At C:...\client.c:55

In ?? () ()

Failure finding "Stack level " Failure matching reg_output

Program received signal SIGSEGV, Segmentation fault.

这是代码:

SOCKET InitializeClient( void )
{
SOCKET ClientsockDesc;
char ServerHostName [256] = {0};
unsigned short int ServerPortNumber;
struct sockaddr_in ServerAddress;
struct hostent *ptrServerHostEntry = NULL;
char Temp [10] = {0};
const char yes = '1';
char* Packet;

while( 0 == strcmp(ServerHostName, "") )
{
printf("Input the host name: ");
fgets(ServerHostName, sizeof(ServerHostName), stdin);
}
ServerHostName[strlen(ServerHostName) - 1] = '\0';

while( 0 == strcmp(Temp, "") )
{
printf("Input the server port number: ");
fgets(Temp, sizeof(Temp), stdin);
}
Temp[strlen(Temp) - 1] = '\0';
ServerPortNumber = (unsigned short int)atoi(Temp);

if( NULL ==(ptrServerHostEntry = gethostbyname(ServerHostName)) )
return INVALID_SOCKET;

ServerAddress.sin_family = AF_INET;
ServerAddress.sin_port = htons( ServerPortNumber );
ServerAddress.sin_addr = *(struct in_addr *)ptrServerHostEntry->h_addr;
memset(&(ServerAddress.sin_zero), 0, 8);
if( INVALID_SOCKET ==(ClientsockDesc = socket(AF_INET, SOCK_STREAM, 0)) )
return INVALID_SOCKET;
if( SOCKET_ERROR == setsockopt(ClientsockDesc,
SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) )
return INVALID_SOCKET;
if( SOCKET_ERROR == connect(ClientsockDesc, (struct sockaddr *)&ServerAddress, sizeof(struct sockaddr)) )
{
closesocket(ClientsockDesc);
return INVALID_SOCKET;
}
printf("Successfully connected to host \'%s\' -(%s).\n",
ServerHostName, inet_ntoa(*(struct in_addr *)ptrServerHostEntry->h_addr) );
if( SOCKET_ERROR == ReceivePacket(&ClientsockDesc, Packet) )
{
closesocket(ClientsockDesc);
return INVALID_SOCKET;
}
printf("%s", Packet);
return ClientsockDesc;
}

如有任何帮助,我们将不胜感激。我很乐意提供任何其他信息。

最佳答案

您正在将未初始化的变量传递给 ReceivePacket:

char* Packet;

<skipped lines>

if( SOCKET_ERROR == ReceivePacket(&ClientsockDesc, Packet) )
{

C 按值传递,这意味着调用后,Packet 仍未初始化。

关于c - 函数没有返回它应该返回的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22912059/

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