Advertisement
Guest User

Untitled

a guest
Jul 25th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sys/socket.h>
  4. #include <sys/types.h>
  5. #include <netinet/in.h>
  6. #include <arpa/inet.h>
  7.  
  8. #define porta_locale 8889
  9.  
  10. int main(){
  11. int sockfd;
  12. struct sockaddr_in indirizzo_remoto, indirizzo_locale;
  13. size_t l;
  14. char testo[512];
  15. char resp[] = "ACK";
  16. if((sockfd = socket(AF_INET, SOCK_DGRAM, 0))<0){
  17. printf("\nERRORE APERTURA SOCKET\n");
  18. return -1;
  19. }
  20. memset((char *) &indirizzo_locale, 0, sizeof(indirizzo_locale));
  21. indirizzo_locale.sin_family = AF_INET;
  22. indirizzo_locale.sin_port = htons(porta_locale);
  23. indirizzo_locale.sin_addr.s_addr = htonl(INADDR_ANY);
  24.  
  25. if(bind(sockfd, (struct sockaddr *) &indirizzo_locale, sizeof(indirizzo_locale))<0){
  26. printf("\nERRORE BINDING\n");
  27. return -1;
  28. }
  29.  
  30. for(;;){
  31. l = sizeof(indirizzo_remoto);
  32. recvfrom(sockfd, testo, 511, 0, (struct sockaddr*) &indirizzo_remoto, (socklen_t *) &l);
  33. printf("%s\n", testo);
  34. sendto(sockfd, resp, strlen(resp)+1, 0, (struct sockaddr*) &indirizzo_remoto, sizeof(indirizzo_remoto));
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement