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 message[100];
memcpy(message, input, length);
for (j = 0; j < length; j++)
{
for (i = 0; i <= 7; i++)
{
temp = (crc >> 15) ^ (message[j] >> 7);
crc <<= 1;
if (temp)
crc ^= 0x1021;
message[j] <<= 1;
}
}
//printf("%x",crc);
return crc;
}
#endif
Source code of UDP Stop and Wait 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, correct_recv = 0;
int length = 100;
int address_length, text_length;
unsigned short sequnce_no;
unsigned short server_port = 25000;
short socket_file_descriptor;
unsigned short crc, recv_crc;
char server_ip[] = "127.0.0.1";
char message[1000];
char text_message[100];
char ack_text[] = "ACK";
char nak_text[] = "NAK";
unsigned char Tx_maeesage[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\t 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);
if (n != -1)
printf("\n\t receive success.....\n");
else
printf("\n\t receive failure.....\n");
message[n] = '\0';
printf(" message=%s \n", message);
crc = crc_16(message, n);
printf(" crc=%x ", crc);
memcpy(&sequnce_no, message, sizeof(unsigned short));
memcpy(text_message, message, sizeof(unsigned short));
if (crc == 0)
{
memcpy(&text_message[2], ack_text, strlen(ack_text));
printf(" %s \n", message);
correct_recv++;
}
else
{
memcpy(&text_message[2], nak_text, strlen(nak_text));
i = correct_recv;
}
text_message[2 + strlength(ack_text)] = '\0';
text_length = sizeof(sequnce_no) + strlen(ack_text);
m = sendto(socket_file_descriptor, text_message, text_length, 0, (struct sockaddr *) &client_address, sizeof(client_address));
if (m != -1)
printf("\n\t send success.....\n");
else
printf("\n\t send failure.....\n");
}
close(socket_file_descriptor);
return 0;
}
Source code of UDP Stop and Wait ARQ Client
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include "crc.h"
int main()
{
int socket_file_descriptor, returnvalue, length = 100, n, i;
unsigned short sequnce_no = 1, crc, shift_crc;
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];
char rtext[100];
int text_length;
struct sockaddr_in server_address;
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);
for (i = 1; i < 4; i++)
{
memcpy(text_message, &sequnce_no, sizeof(sequnce_no));
memcpy(&text_message[2], text[i - 1], 5);
crc = crc_16(text_message, sizeof(unsigned short) + strlen(text[i - 1]));
shift_crc = crc;
shift_crc = (shift_crc << 8) ^ (0x0000);
crc = (crc >> 8) ^ (0x0000);
crc = (shift_crc) ^ (crc);
memcpy(&text_message[7], &crc, sizeof(unsigned short));
text_message[9] = '\0';
text_length = sizeof(sequnce_no) + strlen(text[i - 1]) + sizeof(crc);
returnvalue = sendto(socket_file_descriptor, text_message, text_length, 0, (struct sockaddr *) &server_address, sizeof(server_address));
if (returnvalue != -1)
printf("\n\t message %d sent successfully.....\n", i);
else
printf("\n\t message %d sent failure.....\n", i);
n = recvfrom(socket_file_descriptor, rtext, length, 0, (struct sockaddr *) &server_address, &address_length);
if (n != -1)
{
rtext[n] = '\0';
if (memcmp(&rtext[2], "ACK", 3) == 0)
{
printf("\n\t ACK.....\n");
sequnce_no++;
sleep(3);
}
else
{
i = sequnce_no;
printf(" NAK ");
sleep(3);
}
}
}
close(socket_file_descriptor);
return 0;
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.