hashim
unknown
plain_text
2 months ago
1.5 kB
3
Indexable
#include<stdio.h> #include<string.h> #include<time.h> #include<sys/socket.h> #include<sys/types.h> #include<arpa/inet.h> #include<stdlib.h> #include<unistd.h> #define packetcount 10 typedef struct Packet { int id; int recieved; }Packet; Packet packets[packetcount]; void main() { for(int i=0;i<packetcount;++i) { packets[i].id=i+1; packets[i].recieved=0; } char *ip="127.0.0.100"; int port=5555; srand(time(0)); int sockfd; struct sockaddr_in server_addr,client_addr; char buffer[1024]; socklen_t addr_size; int n; sockfd=socket(AF_INET,SOCK_DGRAM,0); if(sockfd<0) { perror("[-] Socket error"); exit(1); } memset(&server_addr,'\0',sizeof(server_addr)); server_addr.sin_family=AF_INET; server_addr.sin_port=htons(port); server_addr.sin_addr.s_addr=inet_addr(ip); n=bind(sockfd,(struct sockaddr*)&server_addr,sizeof(server_addr)); if(n<0) { perror("[-] bind error"); exit(1); } while(1) { bzero(buffer,1024); addr_size=sizeof(client_addr); recvfrom(sockfd,buffer,1024,0,(struct sockaddr*)&client_addr,&addr_size); sleep(1); int paclet=atoi(buffer); if(packets[packet-1].recieved==1||rand()%5==0) continue; else if(rand()%4==0) { printf("server:recieved corrupted packeted %s.sending negatrive ackn\n",buffer); sprintf(buffer,"NACK %d",packet); sendto(sockfd,buffer,1024,0,(struct sockaddr*)&client_addr,sizeof(client_addr)); } else { printf("server:recieved packet .sending ackn\n",buffer); sprintf(buffer,"ACK %d",packet); sendto(sockfd,buffer,1024,0,(struct sockaddr*)&client_addr,sizeof(client_addr)); packets[packet-1].recieved=1; } } }
Editor is loading...
Leave a Comment