gpt4 book ai didi

c++ - 尝试发送包含字符数组的数据包时出现段错误(核心已转储)

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:10:14 24 4
gpt4 key购买 nike

尝试发送最多包含 30 个字符以及其他识别信息的数据包。这是构造函数。

packet::packet(int t, int s, int l, char * d){
type = t;
seqnum = s;
length = l;
data = d;
}

对于我的类(class),我们需要通过以下方式序列化和反序列化:

void packet::serialize(char * spacket){
sprintf (spacket, "%d %d %d %s", type, seqnum, length, data);
}

void packet::deserialize(char * spacket){
char * itr;
itr = strtok(spacket," ");
char * null_end;

this->type = strtol(itr, &null_end, 10);

itr = strtok(NULL, " ");
this->seqnum = strtol (itr, &null_end, 10);

itr = strtok(NULL, " ");
this->length = strtol (itr, &null_end, 10);

if(this->length == 0){
data = NULL;
}
else{
itr = strtok(NULL, " ");
for(int i=0; i < this->length; i++){ // copy data into char array
this->data[i] = itr[i];
}
}
}

此客户端代码导致我的 server.exe 出现段错误:

char sendPayload[512];
char receivePayload[512];
char sendBuffer[30] = {'1', '2', '3', '\0'};
int receivedPacketType = 0;
int sendPacket;
int receivePacket;

bzero(receivePayload, 512);
bzero(sendPayload, 512);

for (int iterator = 0; iterator < 8; iterator++)
{
//Setting up sendPayload for sending.
packet sendpckt(1, iterator, strlen(sendBuffer), sendBuffer);
sendpckt.serialize((char*)sendPayload);

//Datagram send to emulator.
sendPacket = sendto(sendSocket, &sendPayload, sizeof(sendPayload), 0, (struct sockaddr *)&sendSocketStruct, sendSocketLen);

//Datagram wait on receive from emulator.
receivePacket = recvfrom(receiveSocket, &receivePayload, sizeof(sendPayload), 0, (struct sockaddr *)&receiveSocketStruct, &receiveSocketLen);

//Setting up packet for receivePayload.
packet recvpckt(0,0,0,NULL);
recvpckt.deserialize((char*)receivePayload);
recvpckt.printContents();

bzero(receivePayload, 512);
bzero(receivePayload, 512);

}

服务器端接收代码如下:

char receivePayload[512];
char sendPayload[512];
int expectedSeqNum = 0;
int receivedPacketType = 0;
int receivedPacketSeqNum = 0;
int receivedPacketLength = 0;
char receivedPacketData[512];
int receivePacket;
int sendPacket;
std:string finalText;

bzero(receivePayload, 512);
bzero(sendPayload, 512);

do
{
receivePacket = recvfrom(receiveSocket, &receivePayload, sizeof(receivePayload), 0, (struct sockaddr *)&receiveSocketStruct, &receiveSocketLen);
packet recvpckt(0, 0, 0, NULL);
recvpckt.deserialize((char*)receivePayload);
receivedPacketType = recvpckt.getType();
receivedPacketSeqNum = recvpckt.getSeqNum();
receivedPacketLength = recvpckt.getLength();
bzero(receivePayload, 512);
recvpckt.printContents();

if (receivedPacketType == 3)
{
break;
}

/*
if (receivedPacketType == 1)
{
*receivedPacketData = *recvpckt->getData();
printf("%s\n", receivedPacketData);
}
*/

if (receivedPacketSeqNum == expectedSeqNum)
{
packet sendpckt(0, expectedSeqNum, 0, NULL);
sendpckt.serialize((char*)sendPayload);
sendPacket = sendto(sendSocket, &sendPayload, sizeof(sendPayload), 0, (struct sockaddr *)&sendSocketStruct, sendSocketLen);
bzero(sendPayload, 512);
expectedSeqNum = expectedSeqNum + 1;
if (expectedSeqNum > 7)
{
expectedSeqNum = 0;
}

}
else
{
packet sendpckt(0, expectedSeqNum, 0, NULL);
sendpckt.serialize((char*)sendPayload);
sendPacket = sendto(sendSocket, &sendPayload, sizeof(sendPayload), 0, (struct sockaddr *)&sendSocketStruct, sendSocketLen);
bzero(sendPayload, 512);
}

} while (1);

如果我将数据包中的数据内容清零并将长度设置为 0,则数据包可以完美传输。一旦我尝试增加数据包的长度并给它一个字符串,服务器就会遇到段错误。

我在这里错过了什么?我觉得我的缓冲区太大了,或者我指向或引用的内容不正确。

最佳答案

你像这样初始化你的接收数据包(recvpckt):

packet recvpckt(0, 0, 0, NULL);

这使得 data 成员为 NULL。

然后您调用recvpckt.deserialize,它写入未初始化的data 数组。您需要在某处分配内存(例如在 recvpckt.deserialize 中),或者将数组地址传递给 recvpckt 构造函数以将数据写入。

这类问题最好使用调试器来解决。

关于c++ - 尝试发送包含字符数组的数据包时出现段错误(核心已转储),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42937279/

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