Last update: 10/2/2017
The solution posted for the HW1 program is https://people.cs.clemson.edu/~jmarty/courses/Fall-2017/CPSC360/homework/UDPEchoHW1Sol/
The client supports both modes of operation (RTT_MODE and CBR_MODE). The server is pretty close....you need to complete the server to support mode 1. The following items are worth noting;
Pay attention to how the session manager routines are used, .the use of the RxModePtr, the use of htons(), and how count of dropped and out of order packets is done.
unsigned int RxSeqNumber = 0;
int *RxSeqNumberPtr = (int *)echoBuffer; //$A2
unsigned short RxMode = 0;
unsigned short *RxModePtr = (unsigned short *)&echoBuffer[4];
session *mySession = NULL;
/* Block until receive message from a client */
recvMsgSize = recvfrom(sock, echoBuffer, ECHOMAX, 0, (struct sockaddr *) &echoClntAddr, &cliAddrLen);
if (recvMsgSize < 0)
{
errorCount++;
continue;
}
//Check if Session already exists m
mySession = findSession(echoClntAddr.sin_addr, echoClntAddr.sin_port);
if(mySession == NULL) // If not added already, create a session
{
mySession = createSession(echoClntAddr.sin_addr, echoClntAddr.sin_port);
}
//Tracks total number received from any client
receivedCount++;
RxSeqNumber = (unsigned int)ntohl( (unsigned int)(*RxSeqNumberPtr) );
//two cases, arrival is a 'new' segment
if (mySession->largestSeqNumRxed < RxSeqNumber)
mySession->largestSeqNumRxed = RxSeqNumber; //the arrival is an 'old' segment
if (mySession->largestSeqNumRxed >= RxSeqNumber)
mySession->outOfOrderArrivals++;
mySession->lastSeqNumRxed = RxSeqNumber;
RxMode = (unsigned short )ntohs( (unsigned short )(*RxModePtr));
mySession->mode = RxMode;
The client, pay attention to:
//Warning: the following are longs and not doubles.... usec2 = (theTime2->tv_sec) * 1000000 + (theTime2->tv_usec); usec1 = (theTime1->tv_sec) * 1000000 + (theTime1->tv_usec); curPing = (usec2 - usec1); totalPing += curPing; curRTT = ((double)curPing) / 1000000; smoothedRTT = 0.5 * smoothedRTT + 0.5*curRTT; sumOfCurRTT += curRTT; numberRTTSamples++;
//For both modes, we can just do this once.
//Place the iterationDelay in units of seconds
if ( mode == RTT_MODE) {
iterationDelay = ((double)delay)/1000000;/* Iteration delay in seconds */
} else if ( mode == CBR_MODE) {
if (targetSendRate > 0) {
iterationDelay = (((double)messageSize)*8) / (double)targetSendRate;
}
int myDelay(double delayTime) {
int rc = SUCCESS;
int64_t ns = (int64_t) (delayTime * 1000000000);
// printf("myDelay: delayTime:%f %ld \n",delayTime, ns);
rc = delay(ns);
return rc;
}
int delay(int64_t ns) {
struct timespec req, rem;
req.tv_sec = 0;
while (ns >= 1000000000L) {
ns -= 1000000000L;
req.tv_sec += 1;
}
req.tv_nsec = ns;
while (nanosleep(&req, &rem) == -1)
if (EINTR == errno)
memcpy(&req, &rem, sizeof(rem));
else
return -1;
return 0;
}