diff options
-rw-r--r-- | client/main.c | 4 | ||||
-rw-r--r-- | server/main.c | 4 | ||||
-rw-r--r-- | shared/hook.c | 34 | ||||
-rw-r--r-- | shared/hook.h | 9 |
4 files changed, 42 insertions, 9 deletions
diff --git a/client/main.c b/client/main.c index c58191c..cad3781 100644 --- a/client/main.c +++ b/client/main.c @@ -13,6 +13,8 @@ int main(void) { - start_chat(1); + set_client(); + printf("%d\n", is_client()); + start_chat(); return 0; } diff --git a/server/main.c b/server/main.c index 35ff02d..e1320e3 100644 --- a/server/main.c +++ b/server/main.c @@ -13,6 +13,8 @@ int main(void) { - start_chat(0); + set_server(); + printf("%d\n", is_client()); + start_chat(); return 0; } diff --git a/shared/hook.c b/shared/hook.c index fd6ce2b..eda76db 100644 --- a/shared/hook.c +++ b/shared/hook.c @@ -11,11 +11,13 @@ #include "hook.h" -void chat(int is_client, int sockfd) +static bool client; + +void chat(int sockfd) { char buff[MAX_BUFFER_SIZE]; int n; - if (is_client){ + if (client){ for (;;) { bzero(buff, sizeof(buff)); printf("Enter the string : "); @@ -51,13 +53,13 @@ void chat(int is_client, int sockfd) } } -int start_chat( int is_client ) +int start_chat() { int sockfd, connfd; socklen_t len; struct sockaddr_in servaddr, cli; sockfd = socket(AF_INET, SOCK_STREAM, 0); - if (is_client) + if (client) { if (sockfd == -1) { printf("socket creation failed...\n"); @@ -79,7 +81,7 @@ int start_chat( int is_client ) else printf("connected to the server..\n"); - chat(is_client, sockfd); + chat(sockfd); close(sockfd); } @@ -120,9 +122,29 @@ int start_chat( int is_client ) else printf("server accept the client...\n"); - chat(is_client, connfd); + chat(connfd); close(sockfd); } return 0; } + +void set_client() +{ + client = true; +} + +bool is_client() +{ + return client; +} + +void set_server() +{ + client = false; +} + +bool is_server() +{ + return !client; +}
\ No newline at end of file diff --git a/shared/hook.h b/shared/hook.h index 8f20565..15d39c2 100644 --- a/shared/hook.h +++ b/shared/hook.h @@ -27,6 +27,13 @@ #define PORT 50501 #define SA struct sockaddr - int start_chat(int is_client); + + int start_chat(); + + void set_client(); + bool is_client(); + + void set_server(); + bool is_server(); #endif // SHARED_HOOK_H |