尝试发送包含最多 30 个字符以及其他识别信息的数据包。这是构造函数。
packet::packet(int t, int s, int l, char * d){
type = t;
seqnum = s;
length = l;
data = d;
}
对于我的课程,我们需要通过以下方式进行序列化和反序列化:
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,则数据包传输得非常好。一旦我尝试增加数据包的长度并给它一个字符串,服务器就会遇到分段错误。
我在这里想念什么?我觉得我的缓冲区太大了,或者我错误地指向或引用了某些东西。