Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<winsock2.h>
- #include<windows.h>
- #include<string.h>
- struct codeLine
- {
- int line_num;
- char* code;
- struct codeLine* next;
- };
- typedef struct codeLine line;
- void closeConnect(int s, int cResult);
- int openConnect();
- char* Request(int socket, char* request, int requestSize, int answerSize);
- int casting(char* str);
- //list func:
- void bubbleSort(line* head);
- void insertAtEnd(line** firstNode, line* newNode);
- line* createLine(char* code,int line_num);
- void freeList(line** node);
- int listLength(line* head);
- #define PORT 6714
- #define SERVER_IP "54.152.161.133"
- #define OPEN_CONNECT "100"
- #define SUCCESSFUL_OPEN_CONNECT "101"
- #define GET_SUM_LINE "400"
- #define SUCCESSFUL_GET_SUM_LINE "401"
- #define GET_CODE "500"
- #define SUCCESSFUL_GET_CODE "501"
- #define CLOSE_CONNECT "900"
- int main(void){
- /*Your code goes here*/
- int socket = 0;
- char* answer = NULL;
- char* temp = NULL;
- char count_code_line[5] = { '0' };
- int num_of_lines = 0;
- int i = 0;
- int line_num = 0;
- line* head = NULL;
- line* temp_line = NULL;
- FILE* file;
- temp = (char*)malloc(sizeof(char) * 1024);
- answer = (char*)malloc(sizeof(char) * 1024);
- socket = openConnect();
- answer = Request(socket, OPEN_CONNECT, 4, 4);
- if (answer == 0 || strcmp(answer, SUCCESSFUL_OPEN_CONNECT))
- {
- printf("connect failed!\n");
- closeConnect(socket, 0);
- system("PAUSE");
- return 0;
- }
- answer = Request(socket, GET_SUM_LINE, sizeof(GET_SUM_LINE)+1, 1024);
- strcpy(temp, answer);
- *(temp + 3) = '\0';
- if (answer == 0 || strcmp(temp, SUCCESSFUL_GET_SUM_LINE))
- {
- printf("request (num of lines) failed!\n");
- closeConnect(socket, 0);
- system("PAUSE");
- return 0;
- }
- strcpy(temp, (answer + 3));
- num_of_lines = casting(temp);
- count_code_line[5] = 0;
- for ( i = 0; i < num_of_lines; i++)//הלולאה שמורידה קבצים מהשרת וממיינת
- {
- strcpy(temp, GET_CODE);
- strcat(temp, count_code_line);
- answer = Request(socket, GET_CODE, 8, 1024);
- temp = answer;
- *(temp + 3) = 0;
- if (answer == 0 || strcmp(temp, SUCCESSFUL_GET_CODE))
- {
- printf("request (get code) failed!\n");
- i--;
- }
- else
- {
- strcpy(temp, (answer + 3));
- *(temp + 7) = 0;
- line_num = casting(temp);
- strcpy(temp, (answer + 7));
- temp_line = createLine(temp, line_num);
- if (i == 0)
- {
- head = temp_line;
- }
- else
- {
- insertAtEnd(&head, temp_line);
- }
- if (i % 10 == 0)
- {
- count_code_line[1]++;
- count_code_line[0] = '0';
- }
- else
- {
- count_code_line[0]++;
- }
- }
- }
- bubbleSort(head);
- Request(socket, CLOSE_CONNECT, 4, 4);
- closeConnect(socket, 0);
- file = fopen("file.c", "w");
- if (file == NULL)
- {
- printf("fopen file filed");
- return 0;
- }
- temp_line = head;
- for ( i = 0; i < num_of_lines; i++)
- {
- fprintf(file, "%s\n",temp_line->code);
- temp = temp_line->next;
- }
- fclose(file);
- free(answer);
- free(temp);
- freeList(&head);
- system("PAUSE");
- return (0);
- }
- void closeConnect(int s, int cResult)
- {
- cResult = closesocket(s);
- if (cResult == SOCKET_ERROR)
- printf("close func failed with error: %1d\n", WSAGetLastError());
- WSACleanup();
- }
- int openConnect()
- {
- WSADATA info;
- SOCKADDR_IN clientService;
- int err;
- int s;
- int if_error;
- int cResult;
- err = WSAStartup(MAKEWORD(2, 2), &info);
- if (err != 0)
- {
- printf("failed, error: %d/n", err);
- exit(1);
- }
- s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (s == INVALID_SOCKET)
- {
- printf("error =%d\n", WSAGetLastError());
- }
- else
- {
- printf("succeeded\n");
- }
- clientService.sin_family = AF_INET;
- clientService.sin_addr.s_addr = inet_addr(SERVER_IP);
- clientService.sin_port = htons(PORT);
- cResult = connect(s, (struct sockaddr *) &clientService, sizeof(clientService));
- if (cResult == SOCKET_ERROR)
- {
- printf("failed with error: %1d\n", WSAGetLastError());
- closeConnect(s, cResult);
- return 1;
- }
- return s;
- }
- char* Request(int socket, char* request, int requestSize, int answerSize)
- {
- int if_error;
- char answer[1024];
- if_error = send(socket, request, requestSize, 0);
- if (if_error == SOCKET_ERROR)
- {
- closeConnect(socket, if_error);
- return 0;
- }
- if_error = recv(socket, answer, answerSize, 0);
- if (if_error == SOCKET_ERROR)
- {
- printf("recv func failed\n");
- return 0;
- }
- return (answer);
- }
- int casting(char* str)
- {
- int num = 0;
- int i;
- for (i = 0; i < 4; i++)
- {
- num += *(str + i) - '0';
- num *= 10;
- }
- return(num / 10);
- }
- void bubbleSort(line* head)
- {
- int i, j;
- int listLen = listLength(head);
- int value;
- line* temp = head->next;
- line* temp2 = head;
- for (i = 0; i < listLen - 1; ++i)
- {
- for (j = 0; j < listLen - i - 1; ++j)
- {
- if (temp2->line_num < temp->line_num)
- {
- value = temp2->line_num;
- temp2->line_num = temp->line_num;
- temp->line_num = value;
- }
- temp = temp->next;
- temp2 = temp2->next;
- }
- temp = head->next;
- temp2 = head;
- }
- }
- void insertAtEnd(line** firstNode, line* newNode)
- {
- line* currNode = *firstNode;
- // if the linked list is empty
- // should put the new node as the first
- if (!currNode)
- {
- *firstNode = newNode;
- newNode->next = NULL;
- }
- else
- {
- while (currNode->next)
- {
- currNode = currNode->next;
- }
- currNode->next = newNode;
- newNode->next = NULL;
- }
- }
- line* createLine(char* code, int line_num)
- {
- line* newLine = (line*)malloc(sizeof(line));
- if (newLine)
- {
- newLine->code = (char*)malloc(sizeof(char) * 1024);
- strcpy(newLine->code ,code);
- newLine->line_num = line_num;
- newLine->next = NULL;
- }
- return newLine;
- }
- void freeList(line** node)
- {
- line* temp;
- while (*node)
- {
- temp = *node;
- *node = (*node)->next;
- free(temp);
- }
- }
- int listLength(line* head)
- {
- line* temp = head;
- int counter = 0;
- if (head)
- {
- while (temp)
- {
- temp = temp->next;
- counter++;
- }
- }
- return counter;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement