Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.17 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 <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10.  
  11. struct student{
  12.     char indeks[6];
  13.     char nazwisko[20];
  14. };
  15.  
  16. typedef struct student student;
  17.  
  18. int main(int argc, char** argv) {
  19.    
  20.     student tab[2];
  21.     strcpy(tab[0].indeks, "132244");
  22.     strcpy(tab[0].nazwisko, "Tomasz Jurek      \n");
  23.     strcpy(tab[1].indeks, "136731");
  24.     strcpy(tab[1].nazwisko, "Hubert Kamieniarz\n");
  25.    
  26.     struct sockaddr_in saddr, caddr;
  27.     int sfd, cfd, on = 1, i = 0, cmpres;
  28.     socklen_t len;
  29.  
  30.     sfd = socket(PF_INET, SOCK_STREAM, 0);
  31.     setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on));
  32.  
  33.     saddr.sin_family = PF_INET;
  34.     saddr.sin_port = htons(3210);
  35.     saddr.sin_addr.s_addr = INADDR_ANY;
  36.     bind(sfd, (struct sockaddr*)&saddr, sizeof(saddr));
  37.     listen(sfd, 10);
  38.     char buf[32];
  39.  
  40.     while(1) {
  41.         len = sizeof(caddr);
  42.         cfd = accept(sfd, (struct sockaddr*)&caddr, &len);
  43.         printf("New connection from: %s:%d\n", inet_ntoa(caddr.sin_addr), caddr.sin_port);
  44.         read(cfd, buf, sizeof(buf));
  45.         for(i=0; i<2; i++)
  46.         {
  47.             cmpres = strncmp(buf, tab[i].indeks, 6);
  48.             if(cmpres==0) {
  49.                 write(cfd, tab[i].nazwisko, sizeof(tab[i].nazwisko));
  50.                 break;
  51.             }
  52.         }
  53.         if(cmpres!=0) write(cfd, "ERROR!\n", 6);
  54.         close(cfd);
  55.     }
  56.  
  57.     close(sfd);
  58.  
  59.     return 0;
  60. }
  61.  
  62.  
  63.  
  64. ===========================================================
  65.  
  66.  
  67. #include <sys/types.h>
  68. #include <sys/socket.h>
  69. #include <netinet/in.h>
  70. #include <arpa/inet.h>
  71. #include <netdb.h>
  72. #include <unistd.h>
  73. #include <stdio.h>
  74. #include <string.h>
  75. #include <stdlib.h>
  76.  
  77. int main(int argc, char** argv) {
  78.  
  79. int fd = socket(PF_INET, SOCK_STREAM, 0);
  80.  
  81. struct sockaddr_in addr;
  82. struct hostent *host;
  83.  
  84. addr.sin_family = PF_INET;
  85. addr.sin_port = htons(atoi(argv[2]));
  86.  
  87. host = gethostbyname(argv[1]);
  88.  
  89. memcpy(&addr.sin_addr.s_addr, host->h_addr, host->h_length);
  90.  
  91. connect(fd, (struct sockaddr*)&addr, sizeof(addr));
  92.  
  93. char buf[256];
  94. int rc = read(fd, buf, sizeof(buf));
  95. write(1, buf, rc);
  96.  
  97. close(fd);
  98.  
  99. return 0;
  100. }
  101.  
  102.  
  103.  
  104. ================================================
  105.  
  106.  
  107. #include <sys/types.h>
  108. #include <sys/socket.h>
  109. #include <netinet/in.h>
  110. #include <arpa/inet.h>
  111. #include <netdb.h>
  112. #include <unistd.h>
  113. #include <stdio.h>
  114. #include <string.h>
  115. #include <stdlib.h>
  116.  
  117. int main(int argc, char** argv) {
  118.  
  119. struct sockaddr_in saddr, caddr;
  120. int sfd, cfd, on = 1;
  121. socklen_t len;
  122.  
  123. sfd = socket(PF_INET, SOCK_STREAM, 0);
  124. setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on));
  125.  
  126. saddr.sin_family = PF_INET;
  127. saddr.sin_port = htons(3210);
  128. saddr.sin_addr.s_addr = INADDR_ANY;
  129. bind(sfd, (struct sockaddr*)&saddr, sizeof(saddr));
  130. listen(sfd, 10);
  131.  
  132. while(1) {
  133.     len = sizeof(caddr);
  134.     cfd = accept(sfd, (struct sockaddr*)&caddr, &len);
  135.     printf("New connection from: %s:%d\n", inet_ntoa(caddr.sin_addr), caddr.sin_port);
  136.     write(cfd, "Hello there!\n", 14);
  137.     close(cfd);
  138. }
  139.  
  140. close(sfd);
  141.  
  142. return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement