gpt4 book ai didi

c++ - 在没有信道数据包丢失的传输中未收到数据包的原因?

转载 作者:行者123 更新时间:2023-11-28 00:56:34 32 4
gpt4 key购买 nike

我有一个在 UDP 套接字上接收数据包的程序。

这是接收数据包的循环:

clientfd = bind(client_s,(const sockaddr*) &client_addr, sizeof(client_addr));

/*---Forever... ---*/
while (1)
{
addrlen=sizeof(client_addr);
bufferWithPacketData = new char [headerSizeTot+symbol_size];
int n = recvfrom (client_s, bufferWithPacketData, symbol_size + headerSizeTot, 0,(struct sockaddr*)&addrSenderOfVideo, &fromlen);
if (n >= 0 ) //n=-1 => nothing receieved..
{
pthread_create(&threadID , NULL, &ProcessDataOfPacketInThread, (void*) bufferWithPacketData );
}

} //end of while

处理数据包的线程代码:

void* ProcessDataOfPacketInThread (void* ptr) 
{
char* bufferWithPacketData = (char*) ptr;
pthread_t threadID;

//todo remove fprint messages..in the listener thread..
//first byte containing which fec-session,etcetera..

//todo remove print outs here

unsigned int curr_fec_session = extract_value_from_header (bufferWithPacketData,POSITION_OF_FEC_SESSION);
unsigned int fecSessionModuloNr = curr_fec_session % SIZE_OF_MAX_FEC_SESSIONS_AT_SAME_TIME;
assert ( fecSessionModuloNr < SIZE_OF_MAX_FEC_SESSIONS_AT_SAME_TIME );

//todo remove print outs here
//i can do modulo elsewhere also...easiest..
// int fecSessionNrModulo = curr_fec_session % SIZE_OF_MAX_FEC_SESSIONS_AT_SAME_TIME;
//one more symbol for this fec-session...
//todo remove this if we are missing packets..

int nrOfPktReceivedForCurrentSession;
int nrOfPacketLossesForCurrentSession;
float currentLostRate;

unsigned int curr_symbol_nr = extract_value_from_header (bufferWithPacketData,POSITION_OF_SYMBOL_NR);

pthread_mutex_lock (&mutexForAccessSymbolAndEncodingAndNrReceivedVector[fecSessionModuloNr]);
vectorOfNumberOfReceivedSymbolsForFecUnit [fecSessionModuloNr]++;
nrOfPktReceivedForCurrentSession = vectorOfNumberOfReceivedSymbolsForFecUnit [fecSessionModuloNr];
pthread_mutex_unlock (&mutexForAccessSymbolAndEncodingAndNrReceivedVector[fecSessionModuloNr]);
//curr_fec_session == 0 &&
//Do this in new threads all of the time to ensure that we are not blocked receiving new packets...

//Run InitParameters-function, which will store parameters for the current fec-session in an array---necessary to use when decoding.Here we can also measure time...

unsigned int session_sequence_nr = extract_value_from_header (bufferWithPacketData,POSITION_OF_SESSION_SEQUENCE_NR);

fprintf (stderr, "\nProccessData; curr_fec_session %d; nrOfPktReceivedForCurrentSession %d; session_sequence_nr %d; curr_symbol_nr %d",curr_fec_session, nrOfPktReceivedForCurrentSession, session_sequence_nr, curr_symbol_nr );


//size vectorOfVetorWithEncodingSymbols == 0 in the beginning, so is curr_fec_session; check if curr_fec_session <
//todo vectorOfvectorOfEncodingS0ymbolsTab ..decide order.
//vectorOfvectorOfSymbolReceiveOrder contains receiver order..
//i can have the same order for vectorOfvectorOfEncodingSymbolsTab ?
//We need to expand!!! session=2, means we want size==3 to fit it..
// curr_fec_session == 4 means we have expanded it ENOUGH!!



//First packet of NEW SESSION!!!
//only done one because of numberOfHighestFecSessionReceived =curr_fec_session below..
if (curr_fec_session > numberOfHighestFecSessionReceived || ( nrOfPktReceivedForCurrentSession == 1 && curr_fec_session == 0 ) ) //First received packet.
{
pthread_mutex_lock (&mutexForAccessToHighestFecSessionNrReceived);
numberOfHighestFecSessionReceived =curr_fec_session;
pthread_mutex_unlock (&mutexForAccessToHighestFecSessionNrReceived);
fprintf (stderr, "CALLING INIT-PARAM");
InitParametersAndArraysForNewFecSession (bufferWithPacketData );
//Erase old for the fec-session three sessions before current..

if (curr_fec_session >= SIZE_OF_MAX_FEC_SESSIONS_AT_SAME_TIME -1 )
{
EraseVectorsForFecSessionModuloNr ( curr_fec_session);
fprintf (stderr, "Calling EraseVectors");
}

//Received packet of new session-> time to decode old session..Check that all threads performing insertion-elements to vector are finished inside of decoding....
//We are decoding the previous session !!
if ( curr_fec_session != 0 )
{
DecodeFECSession( curr_fec_session -1);
fprintf (stderr, "Calling Decode FEC-session");
}
} //end of if.

//Will always take place afte we have inited arrays..
//Do this for all of the packets received.
InsertElementAtCertainPositionOfEncodingSymbolsVector ( bufferWithPacketData );
fprintf (stderr, "Calling Insert element");


//lastLostRateSentToReceiverFloat

} //end of method--called for each packet received..

上述方法中调用的一个方法:

void InsertElementAtCertainPositionOfEncodingSymbolsVector (char* bufferWithDataAndHeader ) 
{
vector<char*>::iterator it;
unsigned int curr_fec_session = extract_value_from_header (bufferWithDataAndHeader,POSITION_OF_FEC_SESSION);
unsigned int fecSessionModuloNr = curr_fec_session % SIZE_OF_MAX_FEC_SESSIONS_AT_SAME_TIME;

unsigned int session_sequence_nr = extract_value_from_header (bufferWithDataAndHeader,POSITION_OF_SESSION_SEQUENCE_NR);
unsigned int curr_symbol_nr = extract_value_from_header (bufferWithDataAndHeader,POSITION_OF_SYMBOL_NR);

unsigned int nrOfSrcSymbolsSent = extract_value_from_header (bufferWithDataAndHeader,POSITION_OF_NR_SRC_SYMBOLS);

unsigned int nrOfRepairSymbolsSent = extract_value_from_header (bufferWithDataAndHeader,POSITION_OF_NR_REPAIR_SYMBOLS);

unsigned int totNrOfSymbols = nrOfSrcSymbolsSent+nrOfRepairSymbolsSent;
fprintf (stderr, "\nINSERT:curr_fec_session %d; session_sequence_nr %d",curr_fec_session, session_sequence_nr);
//change the address of the buffer...
bufferWithDataAndHeader += headerSizeTot;
fprintf (stderr, "\nINSERT:first symbol of buffer data %d; second %d:", bufferWithDataAndHeader[0], bufferWithDataAndHeader[1] ) ;
//strcpy (destination, source)
strcpy (charArrayofCharArrayOfEncodingSymbolsTab [fecSessionModuloNr][curr_symbol_nr ],
bufferWithDataAndHeader );
fprintf (stderr, "after doing strcpy..........");
//just commented out to do some testing how the code works without it...
// pthread_mutex_lock (&mutexForAccessSymbolAndEncodingAndNrReceivedVector[fecSessionModuloNr]);

// fprintf (stderr, "inside of mutex: adding symbol to SymbolReceiveOrder");

// vectorOfvectorOfSymbolReceiveOrder[fecSessionModuloNr].push_back (curr_symbol_nr);
// fprintf (stderr, "releasing lock for encoding sectioN");
// pthread_mutex_unlock (&mutexForAccessSymbolAndEncodingAndNrReceivedVector[fecSessionModuloNr]);
// fprintf (stderr, "INSERT:after releasing lock!!");
}

测试程序的运行之一:发送方发送超过 512 个数据包。接收方收到大约前 100 个数据包。之后有大约 200 个数据包丢失的间隙。收到另外 30 个序列号彼此接近的数据包;然后程序终止。

修改代码以测试没有互斥量的行为,表明仍然有很多丢失的数据包。

程序接收所有数据包,代码如下:

while (1)
{
addrlen=sizeof(client_addr);
bufferWithPacketData = new char [headerSizeTot+symbol_size];
int n = recvfrom (client_s, bufferWithPacketData, symbol_size + headerSizeTot, 0,(struct sockaddr*)&addrSenderOfVideo, &fromlen);
if (n >= 0 ) //n=-1 => nothing receieved..
{
nrReceived++;
unsigned int session_sequence_nr = extract_value_from_header (bufferWithPacketData,POSITION_OF_SESSION_SEQUENCE_NR);
fprintf (stderr, "\nsession seq %d; nrReceived %d", session_sequence_nr, nrReceived);
}

丢失数据包的原因是什么,我该如何解决这个问题?我的猜测是程序没有在每个实例中使用 recvfrom 命令运行线程,套接字接收到新数据包,从而导致丢失数据包。

一些说明:我正在使用环回发送数据包,并且 channel 中没有数据包丢失。当我处理数据包的代码较少时,所有数据包都会收到。

最佳答案

UDP 没有保证。如果您需要保证发送的数据包将被接收并且将按照发送的顺序接收,您应该使用其他一些协议(protocol),例如 TCP。

关于c++ - 在没有信道数据包丢失的传输中未收到数据包的原因?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10920885/

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