Thank you for your prompt reply, can you please explain in more detail the rest of the code that has //comment on it, as they r vital for me to understand
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <time.h>
// The channel's port -- do not change
#define CHANNELPORT "4950"
// Our receiving buffer length
#define MAXBUFLEN 100
// Size of our frame
#define MAXMESGLEN 1
int main(int argc, char *argv[])
{
int sockfd; // COMMENT
struct addrinfo hints, *servinfo, *p; // COMMENT
int rv; // COMMENT
int numbytes; // COMMENT
socklen_t addr_len; // COMMENT
struct sockaddr_storage their_addr; // COMMENT
char buf[MAXBUFLEN]; // COMMENT
char myframe[MAXMESGLEN]; // Do not change. Size of frame to channel
char *mesg = "A"; // Message to be sent
// used for select
fd_set fds;
int rc;
struct timeval timeout;
// COMMENT
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // COMMENT
hints.ai_socktype = SOCK_DGRAM; // COMMENT
// COMMENT
if ((rv = getaddrinfo("127.0.0.1", CHANNELPORT, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
// COMMENT
for(p = servinfo; p != NULL; p = p->ai_next)
{
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) {
perror("client: socket");
continue;
}
break;
}
// If we can't get a socket ...
if (p == NULL) {
fprintf(stderr, "client: failed to bind socket\n");
return 2;
}
// In this assignment, we only send 1 byte at a time.
// Let's copy a byte into our frame
myframe[0] = mesg[0];
// COMMENT
if ((numbytes = sendto(sockfd, myframe, MAXMESGLEN, 0,
p->ai_addr, p->ai_addrlen)) == -1)
{
perror("talker: sendto");
exit(1);
}
printf("Done Sending: %d\n", numbytes);
// ---------------------------------------------------------------------------
// Free everything and call it a day ...
freeaddrinfo(servinfo);
close(sockfd);
return 0;
}
and server1.c
Code:
int main(void)
{
int sockfd;
struct addrinfo hints, *servinfo, *p;
int rv;
int numbytes;
struct sockaddr_storage their_addr;
char buf[MAXBUFLEN];
socklen_t addr_len;
/*
* COMMENT
*/
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE;
// COMMENT -- What does the following function do?
// COMMENT -- Why is the first parameter to getaddrinfo NULL?
if ((rv = getaddrinfo(NULL, MYPORT, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
/*
* COMMENT -- what does the following code do?
*/
for(p = servinfo; p != NULL; p = p->ai_next)
{
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) {
perror("listener: socket");
continue;
}
if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
perror("listener: bind");
continue;
}
break;
} // END FOR
if (p == NULL) {
fprintf(stderr, "listener: failed to bind socket\n");
return 2;
}
freeaddrinfo(servinfo);
printf("server: waiting to recvfrom...\n");
/* -----------------------------------------------------------------------
* MAIN LOOP
* Hint: hit ctrl-c to kill the server
* -----------------------------------------------------------------------
*/
while(1)
{
/*
* COMMENT
*/
addr_len = sizeof their_addr;
if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0,
(struct sockaddr *)&their_addr, &addr_len)) == -1)
{
perror("recvfrom");
exit(1);
}
// Let's print our received data.
buf[numbytes] = '\0';
printf("server: received... \"%s\"\n", buf);
/*
* Now that we've received a character (1-byte), do something ...
* Hint: checkout sendto
*/
} // END WHILE -- the main loop
// -----------------------------------------------------------------------
// Close the socket to the channel.
close(sockfd);
return 0;
}
I know im supposed to use packet acknowledgements, so Im not sure if im supposed to use go-back-n arq or simply modify code to acknowledge, and use go-back-n differently.
Could you please provide me an example of what you would change in the codes to make use of go-back-n,
and also how could it then be used to help the client recover from packet loss, which must include a senders and recievers window, the timeout, I will have to work out by myself using the avergae RTT as a starting point, which fits in with my channels im using