Advertisement
Ancutahij

Server Fork

Oct 18th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.17 KB | None | 0 0
  1.  
  2. #include<sys/types.h>
  3. #include<sys/socket.h>
  4. #include<stdio.h>
  5. #include<netinet/in.h>
  6. #include<netinet/ip.h>
  7. #include<string.h>
  8. #include<fcntl.h>
  9. #include<unistd.h>
  10. #define MAX 512
  11.  
  12.  
  13. void deservire_pb3(int c){
  14. printf("\n Problema 3. \n");
  15. char buffer[MAX];
  16. char buffer2[MAX];
  17. int i;
  18. recv(c,buffer, sizeof(buffer), MSG_WAITALL);
  19. for(int i=0;i<strlen(buffer);i++)
  20. buffer2[i]= buffer[strlen(buffer)-1-i];
  21. buffer2[strlen(buffer)]='\0';
  22. send(c,buffer2, sizeof(buffer2),0);
  23. close(c);
  24.  
  25. }
  26.  
  27. void deservire_pb4(int c){
  28. printf("\n Problema4. \n");
  29. char buffer[MAX];
  30. char buffer2[MAX];
  31. char bufferFinal[MAX];
  32. int i=0;
  33. int j=0;
  34. int k=0;
  35. recv(c, buffer, sizeof(buffer), MSG_WAITALL);
  36. recv(c, buffer2, sizeof(buffer2), MSG_WAITALL);
  37. int length1=strlen(buffer);
  38. int length2=strlen(buffer2);
  39.  
  40. while(i<length1 && j<length2){
  41. if(buffer[i]< buffer2[j]){
  42. bufferFinal[k]=buffer[i];
  43. i=i+1;
  44. k=k+1;
  45. }
  46. else{
  47. bufferFinal[k]=buffer2[j];
  48. j=j+1;
  49. k=k+1;
  50. }
  51. }
  52. //printf("1. %s", bufferFinal);
  53. bufferFinal[k]='\0';
  54. while(i<length1 && j==length2){
  55. bufferFinal[k]=buffer[i];
  56. i=i+1;
  57. k=k+1;
  58. }
  59. //printf("2. %s", bufferFinal);
  60. while(j<length2 && i== length2){
  61. bufferFinal[k] = buffer2[j];
  62. j=j+1;
  63. k=k+1;
  64. }
  65. //printf("3. %s", bufferFinal);
  66.  
  67. bufferFinal[k]='\0';
  68. send(c, bufferFinal, sizeof(bufferFinal), 0);
  69. close(c);
  70. }
  71. // ================ PROBLEMA 5 =====================
  72.  
  73. void deservire_pb5(int c){
  74. printf("\n Problema 5.\n ");
  75. uint16_t buffer[MAX];
  76. uint16_t i=0;
  77. uint16_t numar;
  78. recv(c,&numar, sizeof(numar), MSG_WAITALL);
  79. numar= ntohs(numar);
  80. uint16_t copie = numar;
  81. while(numar>0){
  82. if(copie%numar == 0){
  83. buffer[i]=numar;
  84. i++;
  85. }
  86. numar--;
  87. }
  88.  
  89. i=htons(i);
  90. send(c, &i, sizeof(i),0);
  91. send(c,buffer, sizeof(buffer), 0);
  92. close(c);
  93. }
  94.  
  95.  
  96. // =========================== Problema 6 =========================
  97. uint16_t buffer[MAX];
  98. uint16_t i=0;
  99. uint16_t numar;
  100. recv(c,&numar, sizeof(numar), MSG_WAITALL);
  101. numar= ntohs(numar);
  102. uint16_t copie = numar;
  103. while(numar>0){
  104. if(copie%numar == 0){
  105. buffer[i]=numar;
  106. i++;
  107. }
  108. numar--;
  109. }
  110.  
  111. i=htons(i);
  112. send(c, &i, sizeof(i),0);
  113. send(c,buffer, sizeof(buffer), 0);
  114. close(c);
  115. }
  116.  
  117.  
  118. // =========================== Problema 6 =========================
  119. void deservire_pb6(int c){
  120. printf("\n Problema 6. \n");
  121. char ch[2];
  122. char buffer[MAX];
  123. uint16_t integers[MAX];
  124.  
  125. recv(c, ch, sizeof(ch), MSG_WAITALL);
  126. recv(c,buffer, sizeof(buffer), MSG_WAITALL);
  127.  
  128. uint16_t i=0;
  129. int j=0;
  130. for(j=0;j<strlen(buffer);j++)
  131. if(buffer[j] == ch[0]){
  132. integers[i]=j;
  133. i++;
  134. }
  135. i= htons(i);
  136. send(c, &i, sizeof(i), 0);
  137. send(c, integers, sizeof(integers), 0);
  138. close(0);
  139. }
  140. // ======================== Problema 7 =======================
  141. void deservire_pb7(int c){
  142. uint16_t i,l;
  143. char s[MAX];
  144. char res[MAX];
  145.  
  146.  
  147. recv(c, &i, sizeof(i), MSG_WAITALL);
  148. recv(c, &l, sizeof(l), MSG_WAITALL);
  149. i=ntohs(i);
  150. l=ntohs(l);
  151.  
  152. recv(c, s, sizeof(s), MSG_WAITALL);
  153.  
  154. int j=0;
  155. int k=0;
  156. for(j=i; j<l+i;j++){
  157. res[k]=s[j];
  158. k++;
  159. }
  160. res[k]='\0';
  161. send(c, res, sizeof(res), 0);
  162. close(0);
  163. }
  164. int main(){
  165. int s;
  166. struct sockaddr_in server, client;
  167. int c,l;
  168.  
  169. s = socket(AF_INET, SOCK_STREAM,0);
  170. if(s<=0){
  171. printf("Eroare la crearea socketului server.\n");
  172. return 1;
  173. }
  174.  
  175. memset(&server, 0, sizeof(server));
  176. server.sin_port=htons(1359);
  177. server.sin_family=AF_INET;
  178. server.sin_addr.s_addr=INADDR_ANY;
  179.  
  180. if(bind(s,(struct sockaddr *) &server, sizeof(server))<0){
  181. printf("Eroare la bind.\n");
  182. return 1;
  183. }
  184. listen (s,5);
  185.  
  186. l=sizeof(client);
  187. memset(&client,0,sizeof(client));
  188. while(1){
  189. char buffer[MAX];
  190.  
  191. uint16_t nrProblema;
  192. c= accept(s, (struct sockaddr * ) &client, &l );
  193. printf("S-a conectat un client. \n");
  194. recv(c, &nrProblema, sizeof(nrProblema), MSG_WAITALL);
  195. nrProblema= ntohs(nrProblema);
  196. if(fork() == 0){
  197. if(nrProblema==3){
  198. deservire_pb3(c);
  199. return 0;
  200. }
  201. if(nrProblema==4){
  202. deservire_pb4(c);
  203. return 0;
  204. }
  205. if(nrProblema == 5){
  206.  
  207. deservire_pb5(c);
  208. return 0;
  209. }
  210. if(nrProblema == 6){
  211. deservire_pb6(c);
  212. return 0;
  213. }
  214. if(nrProblema == 7){
  215. deservire_pb7(c);
  216. return 0;
  217. }
  218. }
  219. }
  220. close(s);
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement