Untitled

 avatar
unknown
plain_text
2 months ago
2.3 kB
4
Indexable
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<arpa/inet.h>
#include<unisd.h>
#include<stdlib.h>
#include<sys/select.h>
#include<pthread.h>
#define timeoutValue 5
#define packetCount10
#define windowSize 3
typedef struct Packet
{
int id;
int sent;
}Packet;
Packet packets[packetCount];
pthread_t thread[packetCount],receive Thread;
int sockfd;
struct sockaddr_in addr;
int windowStart,windowEnd;
int nextToSend;
void*sendPacket(void*sendPacket)
{
Packet*packet=(Packet*)sendToPacket;
char bufer[1024];
while(packet->sent==0)
{
printf("Client:Sending packet%d\n",packet->id);
bzero(buffer,1024);
sprintf(buffer,"%d",packet->id);
sendto(sockfd,buffer,1024,0,(struct sockaddr*)&addr,sizeof(addr));
sleep(timeoutValue);
if(packet->sent==0)
printf("Client: Timeout for  packet %d\n",packet->id);
}
}
void*receivePacket()
char buffer[1024];
while(1)
{
bzero(buffer,1024);
recvfrom(sockfd,buffer,1024,0,(struct sockaddr*)&addr,&addr_size);
char msg[20];
int packet[ID];
scanf(buffer,"%s%d",msg,&packetID);
if(strcmp(msg,"NACK")==0)
{
printf("client: Received negative acknowledgment for packet %d \n"Sending again\n",packetID);
pthread_cancel(thread[packetID] -1);
pthread_cancel(thread[packetID] -1),NULL,sendpacket,(void*)&packets[packetID -1]);
}
else if(strcmp(msg,"ACK")==0)
{
printf("Client: Received ack for packets %d\n",packetID);
packets[packetID -1]sent=1;
if(windowStart==packetID -1)
{
while(packets[windowStart].sent==1)
{
windowStart++;
if(windowEnd<packetCount)
windowEnd++;
}
}
}
else
printf("Client:Invalid message\n");
}
}
void main()
{
for(int i=0;i<packetCount;i++)
{
packet[i].id=i+1;
packets[i].sent=0;
}
char*ip="127.0.0.100";
int port=5570;
char buffer[1024];
socklen_t addr_size;
sockfd=socket(AF_INET,SOCK_DGRAM,0);
if(sockfd<0)
{
perror("[-]Socket error");
exit(1);
}
memset(&addr,'\0',sizeof(addr));
	addr.sin_family=AF_INET;
	addr.sin_port=port;
	addr.sin_addr.s_addr=inet_addr(ip);
	pthread_create(&recievedThread,NULL);
	windowStart=0;
	windowEnd=windowStart+windowSize -1);
	for(int i=windowStart;i<=windowEnd;i++);
	nextToSend=windowEnd+1;
	while(windowStart!=windowEnd && nextToSend<packetCount)
	{
	pthread_create(&thread[nextToSend],NULL,sendPacket,(void*)&packets[nextToSend]);
	nextToSend++;
	}
	}
	close(sockfd);
	}
Editor is loading...
Leave a Comment