Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <sys/wait.h>
  10. #include <errno.h>
  11. #include <signal.h>
  12. #include <string.h>
  13. #include <sys/socket.h>
  14. #include <netdb.h>
  15. #include <netinet/in.h>
  16. #include <arpa/inet.h>
  17.  
  18. //server tcp dzialajacy interacyjnie pod adres lokalhost port > 1024
  19. //klient program rc serwer wysyla w formie tekstowej znaczniki czasu takie jakie istnieja
  20.  
  21. #define PORT 8080
  22. #define SA const struct sockaddr
  23.  
  24. void errorExit(const char* str)
  25. {
  26. perror(str);
  27. exit(EXIT_FAILURE);
  28. }
  29.  
  30. int sockfd, len, n;
  31. struct sockaddr_in servaddr, cliaddr;
  32.  
  33. void func()
  34. {
  35. int SIZE = 256;
  36. char buff[SIZE];
  37.  
  38. len = sizeof(cliaddr);
  39.  
  40. while(1)
  41. {
  42. memset(&buff,'\0',sizeof(buff));
  43. n = recvfrom(sockfd, (char*)buff, SIZE, MSG_WAITALL, (struct sockaddr*) &cliaddr, (socklen_t*)&len);
  44.  
  45. buff[n] = '\0';
  46. printf("%s\n", buff);
  47.  
  48. sendto(sockfd, "siema", 5, MSG_CONFIRM, (const struct sockaddr*)&cliaddr, len);
  49. }
  50. }
  51.  
  52. int main( int argc, char* argv[] )
  53. {
  54. if((sockfd=socket(AF_INET, SOCK_DGRAM, 0))==-1)
  55. errorExit("socket");
  56.  
  57. memset(&servaddr,'\0', sizeof(servaddr));
  58. memset(&cliaddr,'\0', sizeof(cliaddr));
  59.  
  60. servaddr.sin_family = AF_INET;
  61. servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  62. servaddr.sin_port=htons(PORT);
  63.  
  64. if(bind(sockfd, (SA*)&servaddr, sizeof(servaddr))!=0)
  65. errorExit("bind");
  66.  
  67. func();
  68.  
  69. if(close(sockfd)==-1)
  70. errorExit("close");
  71.  
  72. exit(EXIT_SUCCESS);
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement