Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <arpa/inet.h>
  5. #include <netdb.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <sys/wait.h>
  11. #include <sys/types.h>
  12. #include <unistd.h>
  13. #include <signal.h>
  14.  
  15. struct Student {
  16. char nazwa[50];
  17. char indeks[7];
  18. };
  19.  
  20. void childend(int signo){
  21. wait(NULL);
  22. }
  23.  
  24. int main (int argc, char *argv[])
  25. {
  26. struct Student PCH;
  27. strcpy( PCH.nazwa, "PCH");
  28. strcpy( PCH.indeks, "116658");
  29.  
  30. struct Student ja2;
  31. strcpy( ja2.nazwa, "hubert");
  32. strcpy( ja2.indeks, "132285");
  33.  
  34. signal(SIGCHLD, childend);
  35. int sfd,cfd,on=1;
  36. char indeks_in[7];
  37. struct sockaddr_in saddr,caddr;
  38. socklen_t sl;
  39. saddr.sin_family = AF_INET;
  40. //zapisanie adresu ip, konwenterowanego do bitow ta funkcja
  41. saddr.sin_addr.s_addr = INADDR_ANY;
  42. saddr.sin_port = htons(1234);
  43. sfd = socket(PF_INET, SOCK_STREAM, 0);
  44. setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (char*) &on, sizeof(on));
  45. bind(sfd, (struct sockaddr*) &saddr, sizeof(saddr));
  46. listen(sfd,5);
  47. int i = 1;
  48. while(i>0){
  49. sl = sizeof(caddr);
  50. cfd = accept(sfd, (struct sockaddr*) &caddr, &sl);
  51. i = fork();
  52. if(i==0){
  53. close(sfd);
  54. printf("new connection %s: %d\n",inet_ntoa(caddr.sin_addr), ntohs(caddr.sin_port));
  55. read(cfd, &indeks_in, 7);
  56. if( strncmp(indeks_in, PCH.indeks, 6) == 0)
  57. {
  58. write(cfd,PCH.nazwa,3);
  59. }else if ( strncmp(indeks_in, ja2.indeks, 6) == 0)
  60. {
  61. write(cfd,ja2.nazwa,6);
  62. }else{
  63. write(cfd,"nieznany",8);
  64. }
  65. close(cfd);
  66. exit(EXIT_SUCCESS);
  67. }
  68. close(cfd);
  69. }
  70. close(sfd);
  71. return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement