|
| 1 | +/* |
| 2 | + UDP_Server. This Program will will create the Server side for UDP_Socket Programming. |
| 3 | + It will receive the data from the client and then send the same data back to client. |
| 4 | +*/ |
| 5 | + |
| 6 | +#include <stdio.h> |
| 7 | +#include <stdlib.h> |
| 8 | +#include <string.h> |
| 9 | +#include <sys/socket.h> //socket |
| 10 | +#include <arpa/inet.h> //inet_addr |
| 11 | + |
| 12 | +int main(void) |
| 13 | +{ |
| 14 | + int socket_number = 5006; |
| 15 | + |
| 16 | + int socket_desc; |
| 17 | + struct sockaddr_in server_addr, client_addr; |
| 18 | + char server_message[2000], client_message[2000]; |
| 19 | + |
| 20 | + int client_struct_length = sizeof(client_addr); |
| 21 | + |
| 22 | + //Cleaning the Buffers |
| 23 | + |
| 24 | + memset(server_message,'\0',sizeof(server_message)); |
| 25 | + memset(client_message,'\0',sizeof(client_message)); |
| 26 | + |
| 27 | + //Creating Socket |
| 28 | + |
| 29 | + socket_desc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 30 | + |
| 31 | + if(socket_desc < 0) |
| 32 | + { |
| 33 | + printf("Could Not Create Socket. Error!!!!!\n"); |
| 34 | + return -1; |
| 35 | + } |
| 36 | + |
| 37 | + printf("Socket Created\n"); |
| 38 | + |
| 39 | + //Binding IP and Port to socket |
| 40 | + |
| 41 | + server_addr.sin_family = AF_INET; |
| 42 | + server_addr.sin_port = htons(socket_number); |
| 43 | + server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); |
| 44 | + |
| 45 | + if(bind(socket_desc, (struct sockaddr*)&server_addr, sizeof(server_addr))<0) |
| 46 | + { |
| 47 | + printf("Bind Failed. Error!!!!!\n"); |
| 48 | + return -1; |
| 49 | + } |
| 50 | + |
| 51 | + printf("Bind Done\n"); |
| 52 | + |
| 53 | + //----------------------------------------------------------// |
| 54 | + //read from file and initialize the array |
| 55 | + |
| 56 | + FILE* fptr = fopen("attendance.txt", "r"); |
| 57 | + |
| 58 | + int current_size = 0; |
| 59 | + |
| 60 | + fscanf(fptr, "%d", ¤t_size); |
| 61 | + |
| 62 | + //--read empty space if any--// |
| 63 | + //char buff[2]; |
| 64 | + //fscanf(fptr, "%s", buff); |
| 65 | + |
| 66 | + //using pointers to efficiently manage memory |
| 67 | + char *strs[20]; |
| 68 | + char temp_roll[15]; |
| 69 | + |
| 70 | + int l = 0; |
| 71 | + |
| 72 | + for(; l<current_size; l++) |
| 73 | + { |
| 74 | + fscanf(fptr, "%s", temp_roll); |
| 75 | + strs[l] = malloc(strlen(temp_roll+1)); |
| 76 | + strcpy(strs[l], temp_roll); |
| 77 | + } |
| 78 | + |
| 79 | + fclose(fptr); |
| 80 | + //----------------------------------------------------------// |
| 81 | + |
| 82 | + while (1) |
| 83 | + { |
| 84 | + int write_to_file = 0; |
| 85 | + |
| 86 | + printf("Listening for Messages...\n\n"); |
| 87 | + |
| 88 | + //Receive the message from the client |
| 89 | + |
| 90 | + if (recvfrom(socket_desc, client_message, sizeof(client_message), 0, (struct sockaddr*) &client_addr,&client_struct_length) < 0) |
| 91 | + { |
| 92 | + printf("Receive Failed. Error!!!!!\n"); |
| 93 | + return -1; |
| 94 | + } |
| 95 | + |
| 96 | + printf("Received Message from IP: %s and Port No: %i\n",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port)); |
| 97 | + |
| 98 | + // get roll number |
| 99 | + char rollnum[15]; |
| 100 | + strcpy(rollnum, client_message); |
| 101 | + rollnum[7] = '\0'; |
| 102 | + |
| 103 | + int i = 0; |
| 104 | + int found = 0; |
| 105 | + int f_index = 0; |
| 106 | + |
| 107 | + for (i = 0; i < current_size; i++) |
| 108 | + { |
| 109 | + if(strcmp(strs[i], rollnum) == 0) |
| 110 | + { |
| 111 | + found = 1; |
| 112 | + f_index = i; |
| 113 | + break; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // Check In Scenario |
| 118 | + if (client_message[8] == 'C' && client_message[9] == 'I') |
| 119 | + { |
| 120 | + // if it doesnt exist then allocate memory to it |
| 121 | + if (found == 0) |
| 122 | + { |
| 123 | + strs[current_size] = malloc(strlen(rollnum) + 1); |
| 124 | + strcpy(strs[current_size], rollnum); |
| 125 | + current_size++; |
| 126 | + |
| 127 | + strcpy(server_message, "Welcome Student "); |
| 128 | + strcat(server_message, rollnum); |
| 129 | + strcat(server_message, " !\n"); |
| 130 | + write_to_file = 1; |
| 131 | + } |
| 132 | + else |
| 133 | + { |
| 134 | + strcpy(server_message, "You are already here.\n"); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + // Check Out Scenario |
| 139 | + else if (client_message[8] == 'C' && client_message[9] == 'O') |
| 140 | + { |
| 141 | + // remove that student from array and move all students one place backwards |
| 142 | + if (found == 1) |
| 143 | + { |
| 144 | + int j = f_index; |
| 145 | + |
| 146 | + //moving all students one place back |
| 147 | + for(; j < current_size - 1;j++) |
| 148 | + { |
| 149 | + strcpy(strs[j], strs[j+1]); |
| 150 | + } |
| 151 | + |
| 152 | + // mark one place free now |
| 153 | + free (strs[current_size - 1]); |
| 154 | + |
| 155 | + current_size--; |
| 156 | + |
| 157 | + |
| 158 | + strcpy(server_message, "Goodbye Student "); |
| 159 | + strcat(server_message, rollnum); |
| 160 | + strcat(server_message, " ! Have a nice day!\n"); |
| 161 | + write_to_file = 1; |
| 162 | + } |
| 163 | + else |
| 164 | + { |
| 165 | + strcpy(server_message, "You didn't check in today. Contact System Administrator.\n"); |
| 166 | + } |
| 167 | + |
| 168 | + } |
| 169 | + |
| 170 | + else |
| 171 | + printf("Wrong Input Message\n"); |
| 172 | + |
| 173 | + printf("Current Size is %d . ", current_size); |
| 174 | + |
| 175 | + if (current_size > 0) |
| 176 | + { |
| 177 | + //print all members in the array |
| 178 | + printf("Following students are present:\n"); |
| 179 | + |
| 180 | + int c = 0; |
| 181 | + for(; c < current_size; c++) |
| 182 | + { |
| 183 | + printf(strs[c]); |
| 184 | + printf("\n"); |
| 185 | + } |
| 186 | + } |
| 187 | + else |
| 188 | + printf("No student is present.\n"); |
| 189 | + |
| 190 | + //Send the message back to client |
| 191 | + |
| 192 | + if (sendto(socket_desc, server_message, strlen(server_message), 0, (struct sockaddr*)&client_addr,client_struct_length)<0) |
| 193 | + { |
| 194 | + printf("Send Failed. Error!!!!!\n"); |
| 195 | + return -1; |
| 196 | + } |
| 197 | + |
| 198 | + memset(server_message,'\0',sizeof(server_message)); |
| 199 | + memset(client_message,'\0',sizeof(client_message)); |
| 200 | + |
| 201 | + // write to file only when the array is modified |
| 202 | + if (write_to_file == 1) |
| 203 | + { |
| 204 | + //in case the server shuts down for some reason, write the array back to file for persistence of database |
| 205 | + |
| 206 | + fptr = fopen("attendance.txt", "w"); |
| 207 | + |
| 208 | + // write current size of array to file |
| 209 | + char a_size[5]; |
| 210 | + snprintf (a_size, sizeof(a_size), "%d",current_size); |
| 211 | + fputs (a_size, fptr); |
| 212 | + fputs (" ", fptr); |
| 213 | + |
| 214 | + int k = 0; |
| 215 | + |
| 216 | + for(; k < current_size; k++) |
| 217 | + { |
| 218 | + fputs(strs[k], fptr); |
| 219 | + fputs (" ", fptr); |
| 220 | + } |
| 221 | + |
| 222 | + fclose(fptr); |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + |
| 227 | + //Closing the Socket |
| 228 | + close(socket_desc); |
| 229 | + return 0; |
| 230 | +} |
0 commit comments