Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netdb.h>
- #include <string.h>
- #include <unistd.h>
- #include <time.h>
- char* createLine(int size) {
- unsigned char* line=malloc(sizeof(char)*size);
- return line;
- }
- char* createPackage() {
- unsigned char** package=malloc(sizeof(char*)*(12));
- // Header line
- unsigned char* header = createLine(4);
- header[0]=35;//VN=4, Mode=3
- package[0]= header;
- unsigned char*tmp=0;
- //Temporary set to null
- // package[1]= createLine(4);
- // package[2]= createLine(4);
- // package[3]= createLine(4);
- //
- //
- // package[4]= createLine(8);
- // package[5]= createLine(8);
- // package[6]= createLine(8);
- // package[7]= createLine(8);
- package[1]= tmp;
- package[2]= tmp;
- package[3]= tmp;
- package[4]= tmp;
- package[5]= tmp;
- package[6]= tmp;
- package[7]= tmp;
- return package;
- }
- int main(int argc,char** argv)
- {
- // Build the package
- char* myPackage = createPackage();
- char* port = "1203";//argv[2];
- char* address ="127.0.0.1";// argv[1];
- //initialize attributes of getaddrinfo function in order to get the ip adress of server
- struct addrinfo hints, *servinfo, *p;
- int client_socket;
- memset(&hints, 0, sizeof(hints));
- hints.ai_family = AF_INET;
- hints.ai_socktype = SOCK_DGRAM;
- int rv;
- if ((rv = getaddrinfo(address, port, &hints, &servinfo)) != 0) {
- fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
- exit(1);
- }
- //creates a socket and connects it to a server
- struct timespec start, end;
- clock_gettime(CLOCK_MONOTONIC,&start);
- for(p = servinfo;p!=NULL;p=p->ai_next)
- {
- if((client_socket = socket(p->ai_family,p->ai_socktype,p->ai_protocol)) == -1)
- {
- perror("socket");
- continue;
- }
- // Connected succesfully!
- break;
- }
- if(p == NULL)
- {
- fprintf(stderr, "Not connected succesfully");
- }
- char buffer[1024];
- //while(1){
- socklen_t addrsize = sizeof servinfo;
- sendto(client_socket,myPackage,10,0,p->ai_addr,p->ai_addrlen);
- recvfrom(client_socket,buffer,1024,0,NULL,NULL);
- printf("\n %s",buffer);
- clock_gettime(CLOCK_MONOTONIC,&end);
- float diff = end.tv_nsec - start.tv_nsec;
- printf("\n%f\n",diff/1000000);
- //}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement