Advertisement
Guest User

Untitled

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