Skip to content

Commit

Permalink
server client connection
Browse files Browse the repository at this point in the history
  • Loading branch information
rehne committed Jul 17, 2017
1 parent 7c06de9 commit 0461a4d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 33 deletions.
Binary file modified client
Binary file not shown.
41 changes: 14 additions & 27 deletions client.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,23 @@ int main(int argc, char *argv[]) {
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);
} else {
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("Socket konnte nicht angelegt werden.\n");
} else {
printf("Socket %i wurde angelegt\n", sock);
}

// Socket nach beenden wieder freigeben
int option = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const void *) &option, sizeof(int));
server.sin_family = AF_INET;
server.sin_port = htons(4711);
//server.sin_addr.s_addr = inet_addr("127.0.0.1");

// 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");
}
*/
// Socket nach beenden wieder freigeben
//int option = 1;
//setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const void *) &option, sizeof(int));

// IP-Adresse in Binärform umwandeln
if(inet_pton(AF_INET, argv[1], &server.sin_addr.s_addr) <= 0) {
if(inet_pton(AF_INET, argv[1], &server.sin_addr) <= 0) {
printf("Fehler beim konvertieren der IP-Adresse\n");
return 1;
}
Expand All @@ -57,16 +48,12 @@ int main(int argc, char *argv[]) {
inet_ntoa(server.sin_addr));
}

while(strcmp(buffer, "quit\n") != 0){
while(1){
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;
}
read(sock, server_reply, sizeof(server_reply));
write(sock, buffer, strlen(buffer));
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
d 20
b 40
d 40
c 30
Binary file modified server
Binary file not shown.
7 changes: 3 additions & 4 deletions server.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
#include <sys/sem.h>
#include <sys/ipc.h>
#include <sys/wait.h>
#define BUF 128
#define SEGSIZE sizeof(keyValues)
#define BUF 1024

int counter = 0;

Expand Down Expand Up @@ -122,7 +121,7 @@ int main() {
while(fgets(line, sizeof(line), file)) {
int count = strtoken(line, " ", result, 2);

printf("Key: %s\n", result[0]);
printf("Key: %s\n", result[0]);
printf("Value: %s", result[1]);
printf("Counter: %d\n\n", KeyValue_Wrapper->counter);

Expand Down Expand Up @@ -222,10 +221,10 @@ int main() {
} else if (strncmp(buffer, stringDel, 3) == 0) {
semop(db, &enter, 1); // Eintritt in kritischen Bereich
int delValue = delete(buffer, KeyValue_Wrapper->counter);
KeyValue_Wrapper->counter--;
if(delValue == 0) {
write(new_sock, "Value nicht gefunden!\n", 22);
} else {
KeyValue_Wrapper->counter--;
file = fopen("database.txt", "w+");
if(file == NULL){
printf("Error opening file!\n");
Expand Down

0 comments on commit 0461a4d

Please sign in to comment.