Advertisement
Guest User

serwer_lab2.c

a guest
Oct 16th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. #include <arpa/inet.h>
  2. #include <netinet/in.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/socket.h>
  7. #include <unistd.h>
  8.  
  9. int main(int argc, char* argv[]){
  10. socklen_t sl;
  11. int sfd, cfd, on = 1;
  12. struct sockaddr_in saddr, caddr;
  13.  
  14. saddr.sin_family = AF_INET;
  15. saddr.sin_addr.s_addr = INADDR_ANY;
  16. saddr.sin_port = htons(1234);
  17. sfd = socket(PF_INET, SOCK_STREAM, 0);
  18. setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (char*) &on, sizeof(on));
  19. bind(sfd, (struct sockaddr*)&saddr, sizeof(saddr));
  20. listen(sfd, 5);
  21.  
  22. char request[7];
  23. while(1){
  24. sl = sizeof(caddr);
  25. cfd = accept(sfd, (struct sockaddr*) &caddr, &sl);
  26.  
  27. printf("%s -- new connection from %s:%d\n",
  28. argv[0],
  29. inet_ntoa(caddr.sin_addr),
  30. ntohs(caddr.sin_port));
  31.  
  32. read(cfd, request, 7);
  33. if (strcmp(request, "132296") == 0)
  34. write(cfd, "Adrianna Nowicka\n", 17);
  35. else if (strcmp(request, "132237") == 0)
  36. write(cfd, "Wojciech Handkiewicz\n", 21);
  37. else
  38. write (cfd, "Error.\n", sizeof("Error.\n"));
  39. close(cfd);
  40.  
  41. }
  42. close(sfd);
  43.  
  44. return 1;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement