Skip to content

Commit

Permalink
persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
rehne committed Jul 10, 2017
1 parent 35111b5 commit 7c06de9
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 34 deletions.
Binary file modified client
Binary file not shown.
68 changes: 46 additions & 22 deletions client.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,65 @@
#include <string.h>
#define BUF 1024

int main(int argc, char **argv) {
int main(int argc, char *argv[]) {
int sock, size;
char *buffer = malloc(BUF);
struct sockaddr_in sin;
char server_reply[BUF];
char buffer[BUF];
struct sockaddr_in server;

if(argc != 2){
printf("<client IP-ADRESSE> erwartet\n");
return 1;
}

server.sin_family = AF_INET;
server.sin_port = htons(4712);
//server.sin_addr.s_addr = inet_addr("127.0.0.1");

// Socket anlegen
if ((sock = socket(AF_INET, SOCK_STREAM, 0) > 0)) {
printf("Socket %i wurde angelegt\n", sock);
printf("Socket %i wurde angelegt\n", sock);
} else {
printf("Socket konnte nicht angelegt werden.\n");
printf("Socket konnte nicht angelegt werden.\n");
}

int sin_len = sizeof(sin);
sin.sin_family = AF_INET;
sin.sin_port = htons(1500);
// Socket nach beenden wieder freigeben
int option = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const void *) &option, sizeof(int));

if(connect(sock, (struct sockaddr *) &sin, sizeof(sin)) == 0){
printf("Verbindung mit dem Server %s hergestellt\n",
inet_ntoa(sin.sin_addr));
// Socket binden
/*
if(bind(sock, (struct sockaddr *)& server, sizeof(server)) == 0) {
printf("Socket %i wurde gebunden!\n", sock);
} else {
printf("Der Port ist nicht frei - belegt!\n");
}
*/

// IP-Adresse in Binärform umwandeln
if(inet_pton(AF_INET, argv[1], &server.sin_addr.s_addr) <= 0) {
printf("Fehler beim konvertieren der IP-Adresse\n");
return 1;
}

// Server verbinden
if(connect(sock, (struct sockaddr *) &server, sizeof(server)) != 0) {
printf("keine Verbindung hergestellt\n");
} else {
printf("Verbindung mit dem Server %s hergestellt\n",
inet_ntoa(server.sin_addr));
}

while(strcmp(buffer, "quit\n") != 0){
size = recv(sock, buffer, BUF-1, 0);
if(size > 0){
buffer[size] = '\0';
}
printf("Nachricht erhalten: %s\n", buffer);
if(strcmp(buffer, "quit\n")){
printf("Nachricht zum Versenden: ");
fgets(buffer, BUF, stdin);
send(sock, buffer, strlen(buffer), 0);
fgets(buffer, sizeof(buffer), stdin);
if(write(sock, buffer, strlen(buffer)) <= 0){
printf("Fehler beim Schreiben\n");
return 1;
} else if(read(sock, server_reply, sizeof(server_reply)) <= 0){
printf("Fehler beim Lesen\n");
return 1;
}
puts(server_reply);
}

close(sock);
return (EXIT_SUCCESS);
}
4 changes: 2 additions & 2 deletions database.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
a 10
c 50
b 90
d 20
b 40
Binary file modified server
Binary file not shown.
20 changes: 10 additions & 10 deletions server.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ int main() {
char buffer[BUF];
char line[BUF];

// Initialisieren vom struct sin mit geeigneten Werten
int sin_len = sizeof(server);
// Initialisieren vom struct server mit geeigneten Werten
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(4711); // 4711 ist die Portangabe
Expand All @@ -63,6 +62,7 @@ int main() {
printf("Socket %i konnte nicht angelegt werden.\n", sock);
}

// Socket nach beenden wieder freigeben
int option = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const void *) &option, sizeof(int));

Expand All @@ -89,7 +89,7 @@ int main() {
exit(1);
}
int db = semget(IPC_PRIVATE, 1, IPC_CREAT|0777);
if(db == -1) {
if (db == -1) {
perror("Die Gruppe db konnte nicht angelegt werden!");
exit(1);
}
Expand Down Expand Up @@ -282,18 +282,18 @@ int put(char* buffer, int counter) {
char **result = malloc(100);
printf("PUT Funktion\n");
int count = strtoken(buffer, " ", result, 3);
for(int i = 0; i < counter; i++)
for(int i = 0; i < counter; i++) {
if(strcmp(KeyValue_Wrapper->keyValues[i].key, result[1]) == 0) {
strcpy(KeyValue_Wrapper->keyValues[i].value, result[2]);
printf("Key gefunden und überschrieben %s\n", KeyValue_Wrapper->keyValues[i].value);
return 0;
}

strcpy(KeyValue_Wrapper->keyValues[KeyValue_Wrapper->counter].key, result[1]);
printf("Key gespeichert: %s\n", KeyValue_Wrapper->keyValues[KeyValue_Wrapper->counter].key);
strcpy(KeyValue_Wrapper->keyValues[KeyValue_Wrapper->counter].value, result[2]);
printf("Value gespeichert: %s", KeyValue_Wrapper->keyValues[KeyValue_Wrapper->counter].value);
return 1;
}
strcpy(KeyValue_Wrapper->keyValues[KeyValue_Wrapper->counter].key, result[1]);
printf("Key gespeichert: %s\n", KeyValue_Wrapper->keyValues[KeyValue_Wrapper->counter].key);
strcpy(KeyValue_Wrapper->keyValues[KeyValue_Wrapper->counter].value, result[2]);
printf("Value gespeichert: %s", KeyValue_Wrapper->keyValues[KeyValue_Wrapper->counter].value);
return 1;
}

// DELETE Funktion
Expand Down

0 comments on commit 7c06de9

Please sign in to comment.