Advertisement
Guest User

Untitled

a guest
Jun 30th, 2015
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.91 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<winsock2.h>
  4. #include<windows.h>
  5. #include<string.h>
  6.  
  7. struct codeLine
  8. {
  9. int line_num;
  10. char* code;
  11. struct codeLine* next;
  12. };
  13. typedef struct codeLine line;
  14.  
  15. void closeConnect(int s, int cResult);
  16. int openConnect();
  17. char* Request(int socket, char* request, int requestSize, int answerSize);
  18. int casting(char* str);
  19. //list func:
  20. void bubbleSort(line* head);
  21. void insertAtEnd(line** firstNode, line* newNode);
  22. line* createLine(char* code,int line_num);
  23. void freeList(line** node);
  24. int listLength(line* head);
  25.  
  26. #define PORT 6714
  27. #define SERVER_IP "54.152.161.133"
  28. #define OPEN_CONNECT "100"
  29. #define SUCCESSFUL_OPEN_CONNECT "101"
  30. #define GET_SUM_LINE "400"
  31. #define SUCCESSFUL_GET_SUM_LINE "401"
  32. #define GET_CODE "500"
  33. #define SUCCESSFUL_GET_CODE "501"
  34. #define CLOSE_CONNECT "900"
  35.  
  36. int main(void){
  37.  
  38. /*Your code goes here*/
  39. int socket = 0;
  40. char* answer = NULL;
  41. char* temp = NULL;
  42. char count_code_line[5] = { '0' };
  43. int num_of_lines = 0;
  44. int i = 0;
  45. int line_num = 0;
  46. line* head = NULL;
  47. line* temp_line = NULL;
  48. FILE* file;
  49. temp = (char*)malloc(sizeof(char) * 1024);
  50. answer = (char*)malloc(sizeof(char) * 1024);
  51. socket = openConnect();
  52. answer = Request(socket, OPEN_CONNECT, 4, 4);
  53. if (answer == 0 || strcmp(answer, SUCCESSFUL_OPEN_CONNECT))
  54. {
  55. printf("connect failed!\n");
  56. closeConnect(socket, 0);
  57. system("PAUSE");
  58. return 0;
  59. }
  60. answer = Request(socket, GET_SUM_LINE, sizeof(GET_SUM_LINE)+1, 1024);
  61. strcpy(temp, answer);
  62. *(temp + 3) = '\0';
  63. if (answer == 0 || strcmp(temp, SUCCESSFUL_GET_SUM_LINE))
  64. {
  65. printf("request (num of lines) failed!\n");
  66. closeConnect(socket, 0);
  67. system("PAUSE");
  68. return 0;
  69. }
  70. strcpy(temp, (answer + 3));
  71. num_of_lines = casting(temp);
  72. count_code_line[5] = 0;
  73. for ( i = 0; i < num_of_lines; i++)//הלולאה שמורידה קבצים מהשרת וממיינת
  74. {
  75. strcpy(temp, GET_CODE);
  76. strcat(temp, count_code_line);
  77. answer = Request(socket, GET_CODE, 8, 1024);
  78. temp = answer;
  79. *(temp + 3) = 0;
  80. if (answer == 0 || strcmp(temp, SUCCESSFUL_GET_CODE))
  81. {
  82. printf("request (get code) failed!\n");
  83. i--;
  84. }
  85. else
  86. {
  87. strcpy(temp, (answer + 3));
  88. *(temp + 7) = 0;
  89. line_num = casting(temp);
  90. strcpy(temp, (answer + 7));
  91. temp_line = createLine(temp, line_num);
  92. if (i == 0)
  93. {
  94. head = temp_line;
  95. }
  96. else
  97. {
  98. insertAtEnd(&head, temp_line);
  99. }
  100. if (i % 10 == 0)
  101. {
  102. count_code_line[1]++;
  103. count_code_line[0] = '0';
  104. }
  105. else
  106. {
  107. count_code_line[0]++;
  108. }
  109. }
  110. }
  111. bubbleSort(head);
  112. Request(socket, CLOSE_CONNECT, 4, 4);
  113. closeConnect(socket, 0);
  114. file = fopen("file.c", "w");
  115. if (file == NULL)
  116. {
  117. printf("fopen file filed");
  118. return 0;
  119. }
  120. temp_line = head;
  121. for ( i = 0; i < num_of_lines; i++)
  122. {
  123. fprintf(file, "%s\n",temp_line->code);
  124. temp = temp_line->next;
  125. }
  126. fclose(file);
  127. free(answer);
  128. free(temp);
  129. freeList(&head);
  130. system("PAUSE");
  131. return (0);
  132. }
  133.  
  134. void closeConnect(int s, int cResult)
  135. {
  136. cResult = closesocket(s);
  137. if (cResult == SOCKET_ERROR)
  138. printf("close func failed with error: %1d\n", WSAGetLastError());
  139. WSACleanup();
  140. }
  141.  
  142. int openConnect()
  143. {
  144. WSADATA info;
  145. SOCKADDR_IN clientService;
  146. int err;
  147. int s;
  148. int if_error;
  149. int cResult;
  150. err = WSAStartup(MAKEWORD(2, 2), &info);
  151. if (err != 0)
  152. {
  153. printf("failed, error: %d/n", err);
  154. exit(1);
  155. }
  156. s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  157. if (s == INVALID_SOCKET)
  158. {
  159. printf("error =%d\n", WSAGetLastError());
  160. }
  161. else
  162. {
  163. printf("succeeded\n");
  164. }
  165. clientService.sin_family = AF_INET;
  166. clientService.sin_addr.s_addr = inet_addr(SERVER_IP);
  167. clientService.sin_port = htons(PORT);
  168. cResult = connect(s, (struct sockaddr *) &clientService, sizeof(clientService));
  169. if (cResult == SOCKET_ERROR)
  170. {
  171. printf("failed with error: %1d\n", WSAGetLastError());
  172. closeConnect(s, cResult);
  173. return 1;
  174. }
  175. return s;
  176. }
  177.  
  178. char* Request(int socket, char* request, int requestSize, int answerSize)
  179. {
  180. int if_error;
  181. char answer[1024];
  182. if_error = send(socket, request, requestSize, 0);
  183. if (if_error == SOCKET_ERROR)
  184. {
  185. closeConnect(socket, if_error);
  186. return 0;
  187. }
  188. if_error = recv(socket, answer, answerSize, 0);
  189. if (if_error == SOCKET_ERROR)
  190. {
  191. printf("recv func failed\n");
  192. return 0;
  193. }
  194. return (answer);
  195. }
  196.  
  197. int casting(char* str)
  198. {
  199. int num = 0;
  200. int i;
  201. for (i = 0; i < 4; i++)
  202. {
  203. num += *(str + i) - '0';
  204. num *= 10;
  205. }
  206. return(num / 10);
  207. }
  208.  
  209. void bubbleSort(line* head)
  210. {
  211. int i, j;
  212. int listLen = listLength(head);
  213. int value;
  214. line* temp = head->next;
  215. line* temp2 = head;
  216. for (i = 0; i < listLen - 1; ++i)
  217. {
  218. for (j = 0; j < listLen - i - 1; ++j)
  219. {
  220. if (temp2->line_num < temp->line_num)
  221. {
  222. value = temp2->line_num;
  223. temp2->line_num = temp->line_num;
  224. temp->line_num = value;
  225. }
  226. temp = temp->next;
  227. temp2 = temp2->next;
  228. }
  229. temp = head->next;
  230. temp2 = head;
  231. }
  232. }
  233.  
  234. void insertAtEnd(line** firstNode, line* newNode)
  235. {
  236. line* currNode = *firstNode;
  237.  
  238. // if the linked list is empty
  239. // should put the new node as the first
  240. if (!currNode)
  241. {
  242. *firstNode = newNode;
  243. newNode->next = NULL;
  244. }
  245. else
  246. {
  247. while (currNode->next)
  248. {
  249. currNode = currNode->next;
  250. }
  251.  
  252. currNode->next = newNode;
  253. newNode->next = NULL;
  254. }
  255. }
  256.  
  257. line* createLine(char* code, int line_num)
  258. {
  259. line* newLine = (line*)malloc(sizeof(line));
  260. if (newLine)
  261. {
  262. newLine->code = (char*)malloc(sizeof(char) * 1024);
  263. strcpy(newLine->code ,code);
  264. newLine->line_num = line_num;
  265. newLine->next = NULL;
  266. }
  267. return newLine;
  268. }
  269.  
  270. void freeList(line** node)
  271. {
  272. line* temp;
  273. while (*node)
  274. {
  275. temp = *node;
  276. *node = (*node)->next;
  277. free(temp);
  278. }
  279. }
  280.  
  281. int listLength(line* head)
  282. {
  283. line* temp = head;
  284. int counter = 0;
  285. if (head)
  286. {
  287. while (temp)
  288. {
  289. temp = temp->next;
  290. counter++;
  291. }
  292. }
  293. return counter;
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement