Skip to content

Commit a517d15

Browse files
committed
Fixing seqnum issues
1 parent 06dd483 commit a517d15

File tree

4 files changed

+12
-5
lines changed

4 files changed

+12
-5
lines changed

go-back-n/gbn.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ ssize_t gbn_send(int sockfd, const void *buf, size_t len, int flags){
4545
bool reset_alrm=true;
4646
state.seq_curr=1;
4747

48+
4849
while(state.seq_curr<=packet_num){
4950
gbnhdr *data_packet=malloc(sizeof(*data_packet));
5051
data_packet->type=DATA;
@@ -84,13 +85,14 @@ ssize_t gbn_send(int sockfd, const void *buf, size_t len, int flags){
8485

8586

8687
}
87-
return(-1);
88+
return(0);
8889
}
8990

9091
ssize_t gbn_recv(int sockfd, void *buf, size_t len, int flags){
9192

9293
int retryCount = 1;
9394
int status = 0;
95+
bool is_seq = false;
9496

9597
gbnhdr *packet = malloc(sizeof(*packet));
9698
gbnhdr *ack_packet = malloc(sizeof(*ack_packet));
@@ -144,23 +146,26 @@ ssize_t gbn_recv(int sockfd, void *buf, size_t len, int flags){
144146
/* Sender has sent a DATA packet */
145147
printf("INFO: DATA received successfully.\n");
146148
/* Receiver responds with DATAACK */
149+
printf("Packet seqnum : %d State seqnum: %d\n",packet->seqnum,state.seqnum);
147150
if(state.seqnum == packet->seqnum){
148151
printf("INFO: Received DATA packet is in sequence.\n");
149152
state.seqnum = packet->seqnum + 1;
150153
ack_packet->seqnum = state.seqnum;
151-
ack_packet->checksum = 0;
154+
ack_packet->checksum = 0;
155+
is_seq = true;
152156
}else {
153157
printf("INFO: DATA packet has the incorrect sequence number.\n");
154158
ack_packet->seqnum = state.seqnum;
155-
ack_packet->checksum = 0;
159+
ack_packet->checksum = 0;
160+
is_seq = false;
156161
}
157162
/* Sending ACK / duplicate ACK */
158163
if (sendto(sockfd, ack_packet, sizeof(*ack_packet), 0, state.address, state.socklen) == -1) {
159164
printf("ERROR: ACK sending failed.\n");
160165
state.state = CLOSED;
161166
break;
162167
} else {
163-
if(state.seqnum == packet->seqnum)
168+
if(is_seq)
164169
printf("INFO: ACK sent successfully.\n");
165170
else
166171
printf("INFO: Duplicate ACK has been sent.\n");
@@ -309,6 +314,7 @@ int gbn_connect(int sockfd, const struct sockaddr *server, socklen_t socklen){
309314
free(ack_packet);
310315
state.address=server;
311316
state.socklen=socklen;
317+
state.seqnum = 1;
312318
return SUCCESS;
313319
}else{
314320
printf("ERROR: ACK send failed.Retrying ...\n");
@@ -402,7 +408,8 @@ int gbn_accept(int sockfd, struct sockaddr *client, socklen_t *socklen){
402408
printf("INFO: ACK received successfully.\n");
403409
printf("INFO: Connection established successfully.\n");
404410
state.address = client;
405-
state.socklen = *socklen;
411+
state.socklen = *socklen;
412+
state.seqnum = 1;
406413
free(ack_packet);
407414
return sockfd;
408415
}

go-back-n/gbn.o

968 Bytes
Binary file not shown.

go-back-n/receiver

144 Bytes
Binary file not shown.

go-back-n/sender

152 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)