Source code of CRC
#ifndef CRCH
#define CRCH
unsigned short crc_16(unsigned char[100], unsigned short);
unsigned short crc_16(unsigned char input[100], unsigned short length)
{
unsigned short crc = 0xFFFF;
unsigned short temp, i, j;
unsigned char msg[100];
memcpy(msg, input, length);
for (j = 0; j < length; j++)
{
for (i = 0; i <= 7; i++)
{
temp = (crc >> 15) ^ (msg[j] >> 7);
crc <<= 1;
if (temp)
{
crc ^= 0x1021;
}
msg[j] <<= 1;
}
}
return crc;
}
#endif
Source code of UDP Go Back n ARQ Server
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include "crc.h"
int main()
{
short int n = 0, m = 0, i = 1, correct_recv = 0;
int length = 100;
int address_length, text_length;
unsigned short seq_no, nack_seq_no = 15;
unsigned short server_port = 25000;
short socket_file_descriptor;
unsigned short crc, recv_crc;
char server_ip[] = "127.0.0.1";
char message[1000] = "ghgsdhsvgdghasdadafh";
char text_message[100];
char ack_text[] = "ACK";
char nak_text[] = "NAK";
unsigned char text_message[100];
char *client;
struct sockaddr_in server_address, client_address;
client = (char *) malloc(sizeof(server_address));
socket_file_descriptor = socket(AF_INET, SOCK_DGRAM, 0);
if (socket_file_descriptor < 0)
{
printf("failed");
exit(0);
}
printf("\n socket created.....\n");
bzero(&server_address, sizeof(server_address));
server_address.sin_family = AF_INET;
inet_aton(server_ip, &server_address.sin_addr);
server_address.sin_port = htons(server_port);
bind(socket_file_descriptor, (struct sockaddr *) &server_address, sizeof(server_address));
while (1)
{
n = recvfrom(socket_file_descriptor, message, length, 0, (struct sockaddr *) &client_address, &address_length);
memcpy(&seq_no, message, sizeof(unsigned short));
memcpy(text_message, message, sizeof(unsigned short));
message[n] = '\0';
printf("\n %s \n", message);
crc = crc_16(message, n);
printf("\n crc=%x \n", crc);
//sleep(5);
if (nack_seq_no >= seq_no)
{
if (crc == 0)
{
memcpy(&text_message[2], ack_text, strlen(ack_text));
correct_recv++;
nack_seq_no = 15;
printf("\n ack for %d\n", seq_no);
}
else
{
memcpy(&text_message[2], nak_text, strlen(nak_text));
nack_seq_no = seq_no;
printf("\n nack for %d\n", seq_no);
//i=correct_recv;
}
text_message[2 + strlen(ack_text)] = '\0';
text_length = sizeof(seq_no) + strlen(ack_text);
m = sendto(socket_file_descriptor, text_message, 5, 0, (struct sockaddr *) &client_address, sizeof(client_address));
if (m!=-1)
printf("\n send success.....\n");
else
printf("\n send failure.....\n");
}
if (correct_recv == 3)
break;
}
close(socket_file_descriptor);
return 0;
}
Source code of UDP Go Back n ARQ Client
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include "crc.h"
static void sig_io(int);
int socket_file_descriptor, returnvalue, length = 100, n, i = 1, j = 1;
unsigned short seq_no = 1, seq_no_ret, crc, shift_crc, correct_recv = 0;
unsigned int address_length;
unsigned short server_port = 25000;
char server_ip[] = "127.0.0.1";
char *text[3] = { "abcde", "fghij", "klmno" };
unsigned char text_message[100];
unsigned char text_message_copy[100];
char rtext[100];
int text_length;
struct sockaddr_in server_address;
int main()
{
int val;
address_length = sizeof(server_address);
bzero(&server_address, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_port = htons(server_port);
inet_aton(server_ip, &server_address.sin_addr);
socket_file_descriptor = socket(PF_INET, SOCK_DGRAM, 0);
val = fcntl(socket_file_descriptor, F_GETFL, 0);
fcntl(socket_file_descriptor, F_SETFL, val | O_NONBLOCK | O_ASYNC);
fcntl(socket_file_descriptor, F_SETOWN, getpid());
signal(SIGIO, sig_io);
while (1)
{
if (seq_no <= 3)
{
printf(" seq_no=%d\n", seq_no);
memcpy(text_message, &seq_no, sizeof(seq_no));
memcpy(&text_message[2], text[seq_no - 1], 5);
memcpy(text_message_copy, text_message, 7);
crc = crc_16(text_message_copy, sizeof(unsigned short) + strlen(text[seq_no - 1]));
shift_crc = crc;
shift_crc = (shift_crc << 8) ^ (0x0000);
crc = (crc >> 8) ^ (0x0000);
crc = (shift_crc) ^ (crc);
if (seq_no == 2 && j == 1)
{
crc = 0x5555;
j++;
}
memcpy(&text_message[7], &crc, sizeof(unsigned short));
text_message[9] = '\0';
text_length = sizeof(seq_no) + strlen(text[seq_no - 1]) + sizeof(crc);
returnvalue = sendto(socket_file_descriptor, text_message, text_length, 0, (struct sockaddr *) &server_address, sizeof(server_address));
seq_no++;
if(returnvalue!=-1)
printf("\n\t message %d sent successfully.....\n",i);
else
printf("\n\t message %d sent failure.....\n",i);
}
else
{
if (correct_recv == 3)
break;
else
continue;
}
}
close(socket_file_descriptor);
return 0;
}
static void sig_io(int signo)
{
n = recvfrom(socket_file_descriptor, rtext, length, 0, (struct sockaddr *) &server_address, &address_length);
if (n != -1)
{
rtext[n] = '\0';
memcpy(&seq_no_ret, rtext, sizeof(unsigned short));
if (memcmp(&rtext[2], "ACK", 3) == 0)
{
printf("\n ACK for %d\n", seq_no_ret);
correct_recv++;
sleep(3);
}
else
{
seq_no = seq_no_ret;
printf("\n NAK for %d\n", seq_no);
sleep(3);
}
}
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.