c - UDP recvfrom data not displaying properly -


html file has following content

helloworld.html

hello world testing echo server 

output server

enter image description here

client code

//pack contents udp packet , send while(1) {     //check validity of file     readsize = fread(buffer, 1, buffersize, currentfile);     if (readsize <= 0) {         if (ferror(currentfile) != 0) {             fprintf(stderr, "unable read file %s\n", inputfile);             free(buffer);             fclose(currentfile);             close(sdescriptor);         }         break;     }     //send data     if (send(sdescriptor, buffer, readsize, 0) < 0) {         fprintf(stderr,"send failed\n");         free(buffer);         fclose(currentfile);         close(sdescriptor);         exit(1);     } } 

server code

/* receive data & print */ unsigned int len = sizeof(cad); int size = sizeof(buffer); while (1) {     charactersread = recvfrom(sd, buffer, size, 0, (struct sockaddr *)&cad, &len);     /* print address of sender */     printf("got datagram %s port %d\n", inet_ntoa(cad.sin_addr), ntohs(cad.sin_port));     printf("%s\n", buffer);     if (charactersread < 0 ) {       perror("error receiving data");     } else {       printf("got %d bytes\n", charactersread);       /* got something, send */       //sendto(sd, buffer, charactersread, 0,(struct sockaddr *)&cad, &length);     }   } 

any more information needed, willing post. client send contents of file udp packet , server receives , prints out. however, can corrupted. have no idea why doing this, if switch parameters of third argument in recvfrom different outputs. example, if make size larger print out whole thing, corrupted chars @ end. correct way of determining size? that's if that's problem. here's happens when size argument made larger. enter image description here

this closer expected, still corrupt bits there.

printf("%s\n", buffer); 

that should be

printf("%.*s\n", charactersread, buffer); 

you're ignoring count.

and if buffer pointer, sizeof buffer going give size of pointer, not size of points to.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -