Unix socket programming (once again)

Status
Not open for further replies.
I have posted another thread a few weeks ago asking for help on a programming project I was working on. The advice I received worked great. I now ask for your help once more.

the following are code segments outlineing a sender/receiver pair that transfer a file over UDP. The program is to ensure reliable data transfer in the way of packet acknowledgements(ACKs), timeouts and retransmissions.

Currently, the sender can send packets (I use a fix-width, sliding window), the receiver can receive the packets. I have tested the program with files that are smaller than one window worth of data, the files were transfered intact.

The problem is that, the ACKs can't reach the sender. As you can see below, I obtain sender's information using recvfrom(). I have printed the "OtherAddr" structure out, and the IP / port number matches the sender's IP / port used.

Here is the receiver code:
Code:
	if ( -1 == (SocketFD = socket (AF_INET, SOCK_DGRAM, 0)) )
	{
		printf("Error, cannot obtain socket(). Program terminates\r\n");
		goto cleanup_file;
	}
	/* MyAddr is a "struct sockaddr_in */
	MyAddr.sin_family = AF_INET;
	MyAddr.sin_port = htons(RECEIVER_PORT);
	MyAddr.sin_addr.s_addr = htonl(INADDR_ANY);
	memset( &(MyAddr.sin_zero), '\0', 8);

	if (-1 == (bind ( SocketFD, (struct sockaddr*) &MyAddr, sizeof(struct sockaddr_in) ) ) )
	{
		printf("Error, cannot bind(). Program terminates\r\n");
		goto cleanup_socket;
	}

/* Initializations - ENDS*/
/*******************************************************************/
/* Receive Loop */
	while (NOT_EOF)
	{
/* receives a packet... socket blocks until it receives packet. The OttherAddr is another "struct sockaddr_in that would contain sender's IP / port on return */
		dummy = sizeof(struct sockaddr_in);
		if ( -1 == recvfrom (SocketFD, (void*)&CurrentPacket, PacketSize, 0 , (struct sockaddr *)&OtherAddr, &dummy) )
		{	printf("Error, recvfrom(). Program terminates\r\n"); break; }

		/* Out-of-order pscket processing ... chop chop ... */
		/****************************************************/
		/* Preparing the ACK                            ... */

			CurrentACK.SeqNum = (UINT_TYPE_USED) BaseSeqNum;
			CurrentACK.CRC = GenerateCRC(&CurrentACK, ACKSize - UINT_TYPE_SIZE);
			do
			{	ByteSent = sendto(SocketFD, (void*) &CurrentACK, ACKSize, 0, (struct sockaddr *)&OtherAddr, sizeof(struct sockaddr_in) );
			} while (ByteSent != ACKSize );
			printf("ACK sent for sequence number %u\n", BaseSeqNum);
		} /* if (CRC_CHECK) ends; finished processing incoming packet */
	}
/* Receive Loop - END; EOF condition is reached */
/*******************************************************************/

Below is the sender. My overall algorithm is to first look into the sending window, if there are spaces available, send packets (fill the space). Then, I would call select() and wait for something to come in, or timeout, and handle them accordingly. For the past 2 days, I have tried different method of calling select(), printing out the return value, etc. Not once did select return with something on the socket. It was always timeout.

Here is the sender's outline:
Code:
	if ( -1 == (SocketFD = socket (AF_INET, SOCK_DGRAM, 0)) )
	{
		printf("Error, cannot obtain socket(). Program terminates\r\n");
		goto cleanup_file;
	}
/* MyAddr is once again a "struct sockaddr_in" */
	MyAddr.sin_family = AF_INET;
	MyAddr.sin_port = htons(SENDER_PORT);
	MyAddr.sin_addr.s_addr = htonl(INADDR_ANY);
	memset( &(MyAddr.sin_zero), '\0', 8);
	
	if (-1 == (bind ( SocketFD, (struct sockaddr*) &MyAddr, sizeof(struct sockaddr_in) ) ) )
	{
		printf("Error, cannot bind(). Program terminates\r\n");
		goto cleanup_socket;
	}
/* IP of the receiver side is obtained from command line */
	/* prepare sockaddr for the receiver side */
	Host = gethostbyname(argv[2]);
	ReceiverAddr.sin_addr.s_addr = *((uint32_t*)(Host->h_addr));
	ReceiverAddr.sin_family = AF_INET;
	ReceiverAddr.sin_port = htons( (unsigned short int)strtoul(argv[3], NULL, 10) );
	memset( &(ReceiverAddr.sin_zero), '\0', 8);

	/* get file size */
	fstat(FileFD, &FileStat);
	RemainingFileSize = FileStat.st_size;

/* TA recommended setting the socket non-block. but I don't see what it will accomplish */
	fcntl(SocketFD, F_SETFL, O_NONBLOCK);
	NOT_ALL_ACKed = TRUE;

/* Initializations - ENDS*/
/*******************************************************************/
/* Send Loop */
	while (NOT_ALL_ACKed)
	{
/* filling up the send-window */
		for (Num=0; Num < MAX_WND_SIZE && NOT_EOF; ++Num )
		{
			/* packet construction, skipped */
			/* send packet to receiver */
				do{
					ByteSent = sendto(SocketFD, (void*) (PacketsInWindow + WndIndex), PacketSize, 0, (struct sockaddr*)&ReceiverAddr, sizeof(struct sockaddr_in));
				} while (ByteSent != PacketSize);
			/* associate a timestamp with packet */
				gettimeofday( PacketTimeOut + WndIndex , NULL); /* get send time for this packet */
				PacketTimeOut[WndIndex].tv_sec += TIMEOUT_SECOND; /* mark timeout */
				NOT_EOF = PacketsInWindow[WndIndex].EOF_Flag ? FALSE : TRUE;
				RemainingFileSize -= PacketsInWindow[WndIndex].DataLen;
printf("SENT sequence number %u\n", PacketsInWindow[WndIndex].SeqNum);
			}
		} /* sending window should now be filled */
	/* preparation for select() */
		FD_ZERO (&SocketFD_Set);
		FD_SET(SocketFD, &SocketFD_Set);
		TimeOut.tv_sec = TIMEOUT_SECOND;
		TimeOut.tv_usec = 0;

		switch ( select (1, &SocketFD_Set, NULL, NULL, &TimeOut) )
		{
			case 0:
			/* select return 0 for time out; retransmission...which works perfectly,
 as that is all my this program can do right now, after the initial window is filled */
						break;
			case -1: /* error...oops */
				printf ("Select error !!\nError Message = %s\n", strerror(errno));
				break;
			default: /* there should be something coming in... */
				if (! FD_ISSET(SocketFD, &SocketFD_Set) )
					break;
				else 
				/* process ACKs, slide window forward */				
				break;
		} /* switch-case statment ends; timeout processing completes */
	}
/* Send Loop - END; ALL packets have been ACKed */
/*******************************************************************/

Any help, advice, probable cause, or even random thoughts about this stupid bug would be appreciated.

PS. Sorry for the long post, both vertically and horizontally :p
PPS. If anyone wish to see the entire source (about 200 lines for receiver, and 300 lines for sender) I'll be more than willing to send them via email.

[This message was edited by LoneGumMan on March 14, 2002 at 01:19.]
 
There is a number of things:

- The first parameter sent to the select call isn't the number of fds + 1 but the highest value of any fd in the set + 1.

So say for instance you opened a descriptor with the value of 42, the first parameter would be 43. It is a common misstake to make but I think that is what is wrong with your client code...

- fcntl(), the fcntl() call will actually REPLACE all the flags on a descriptor not add to them. So you need to do this:

Code:
int flags = fcntl(fd, F_GETFL);

if(flags == -1) {
  // error
}

flags |= O_NONBLOCK;

if(fcntl(fd, F_SETFL, flags) != -1) {
  // error
}


I hope this help you n00b!!!

// tiny
 
hmm....The first parameter for select()...
I would have never known it from reading the GNU C library documentation. Thank you _tiny_. I think you have just finished my program :cool:

I'll go test this tomorrow. Wish me luck :D

As for non-block mode. I don't really know what does that accomplish. Select() would block anyway, and upon return, the socket either has things in it (which mean i won't block anyway) or I am gonna branch to my timeout handler. Either case, non-block makes no sense. Anyany, thanks for the tip.
 
Status
Not open for further replies.