Untitled

 avatar
unknown
plain_text
19 days ago
1.4 kB
5
Indexable
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<sys/select.h>
#include<sys/types.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<stdlib.h>
#define timeoutValue 5
void main()
{
char *ip="127.0.0.1";
int port=5555;
pthread_t timeoutThread;
int sockfd;
struct sockaddr_in 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(&addr,'\0',sizeof(addr));
addr.sin_family=AF_INET;
addr.sin_port=htons(port);
addr.sin_addr.s_addr=inet_addr(ip);
int packetCounter=1;
while(packetCounter<=7)
{
bzero(buffer,1024);
sprintf(buffer,"%d",packetCounter);
printf("client :sending packet%s\n",buffer);
sendto(sockfd,buffer,1024,0,(struct sockaddr*)&addr,sizeof(addr));
bzero(buffer,1024);
addr_size=sizeof(addr);
struct timeval timeout;
timeout.tv_sec=timeoutValue;
timeout.tv_usec=0;
if(setsockopt(sockfd,SOL_SOCKET,SO_RCVTIMEO,&timeout,sizeof(timeout))<0)
printf("error\n");
else
bzero(buffer,1024);
addr_size=sizeof(addr);
int rec=recvfrom(sockfd,buffer,1024,0,(struct sockaddr*)&addr,&addr_size);
if(recv<0)
printf("Client:TIMEOUT error!SEnding packet agaiain\n");
else
{
printf("Client:Received acknowledgement for packet %s\n",buffer);
recvfrom(sockfd,buffer,1024,0,(struct sockaddr*)&addr,&addr_size);
printf("client:received acknowledgement from packet%s\n",buffer);
sleep(1);
packetCounter++;
}
}
sleep(1);
}

Leave a Comment