summaryrefslogtreecommitdiff
path: root/shared
diff options
context:
space:
mode:
Diffstat (limited to 'shared')
-rw-r--r--shared/hook.c128
-rw-r--r--shared/hook.h32
2 files changed, 160 insertions, 0 deletions
diff --git a/shared/hook.c b/shared/hook.c
new file mode 100644
index 0000000..fd6ce2b
--- /dev/null
+++ b/shared/hook.c
@@ -0,0 +1,128 @@
+/*
+ * som_net / simple library example (c) by jak1
+ *
+ * som_net / simple library example is licensed under a
+ * Creative Commons Attribution-ShareAlike 4.0 International License.
+ *
+ * You should have received a copy of the license along with this
+ * work. If not, see <http://creativecommons.org/licenses/by-sa/4.0/>.
+ * Footer
+*/
+
+#include "hook.h"
+
+void chat(int is_client, int sockfd)
+{
+ char buff[MAX_BUFFER_SIZE];
+ int n;
+ if (is_client){
+ for (;;) {
+ bzero(buff, sizeof(buff));
+ printf("Enter the string : ");
+ n = 0;
+ while ((buff[n++] = getchar()) != '\n')
+ ;
+ write(sockfd, buff, sizeof(buff));
+ bzero(buff, sizeof(buff));
+ read(sockfd, buff, sizeof(buff));
+ printf("From Server : %s", buff);
+ if ((strncmp(buff, "exit", 4)) == 0) {
+ printf("Client Exit...\n");
+ break;
+ }
+ }
+ }
+ else
+ {
+ for (;;) {
+ bzero(buff, MAX_BUFFER_SIZE);
+ read(sockfd, buff, sizeof(buff));
+ printf("From client: %s\t To client : ", buff);
+ bzero(buff, MAX_BUFFER_SIZE);
+ n = 0;
+ while ((buff[n++] = getchar()) != '\n')
+ ;
+ write(sockfd, buff, sizeof(buff));
+ if (strncmp("exit", buff, 4) == 0) {
+ printf("Server Exit...\n");
+ break;
+ }
+ }
+ }
+}
+
+int start_chat( int is_client )
+{
+ int sockfd, connfd;
+ socklen_t len;
+ struct sockaddr_in servaddr, cli;
+ sockfd = socket(AF_INET, SOCK_STREAM, 0);
+ if (is_client)
+ {
+ if (sockfd == -1) {
+ printf("socket creation failed...\n");
+ exit(0);
+ }
+ else
+ printf("Socket successfully created..\n");
+ bzero(&servaddr, sizeof(servaddr));
+
+ servaddr.sin_family = AF_INET;
+ servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
+ servaddr.sin_port = htons(PORT);
+
+ if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr))
+ != 0) {
+ printf("connection with the server failed...\n");
+ exit(0);
+ }
+ else
+ printf("connected to the server..\n");
+
+ chat(is_client, sockfd);
+
+ close(sockfd);
+ }
+ else
+ {
+ if (sockfd == -1) {
+ printf("socket creation failed...\n");
+ exit(0);
+ }
+ else
+ printf("Socket successfully created..\n");
+ bzero(&servaddr, sizeof(servaddr));
+
+ servaddr.sin_family = AF_INET;
+ servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
+ servaddr.sin_port = htons(PORT);
+
+ if ((bind(sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0) {
+ printf("socket bind failed...\n");
+ exit(0);
+ }
+ else
+ printf("Socket successfully binded..\n");
+
+ if ((listen(sockfd, 5)) != 0) {
+ printf("Listen failed...\n");
+ exit(0);
+ }
+ else
+ printf("Server listening..\n");
+ len = sizeof(cli);
+
+ connfd = accept(sockfd, (SA*)&cli, &len);
+ if (connfd < 0) {
+ printf("server accept failed...\n");
+ exit(0);
+ }
+ else
+ printf("server accept the client...\n");
+
+ chat(is_client, connfd);
+
+ close(sockfd);
+ }
+ return 0;
+}
diff --git a/shared/hook.h b/shared/hook.h
new file mode 100644
index 0000000..8f20565
--- /dev/null
+++ b/shared/hook.h
@@ -0,0 +1,32 @@
+/*
+ * som_net / simple library example (c) by jak1
+ *
+ * som_net / simple library example is licensed under a
+ * Creative Commons Attribution-ShareAlike 4.0 International License.
+ *
+ * You should have received a copy of the license along with this
+ * work. If not, see <http://creativecommons.org/licenses/by-sa/4.0/>.
+ * Footer
+*/
+
+#ifndef SHARED_HOOK_H
+#define SHARED_HOOK_H
+ #include <arpa/inet.h> // inet_addr()
+ #include <strings.h>
+ #include <stdio.h>
+ #include <stdbool.h>
+ #include <netdb.h>
+ #include <netinet/in.h>
+ #include <stdlib.h>
+ #include <string.h>
+ #include <sys/socket.h>
+ #include <sys/types.h>
+ #include <unistd.h> // read(), write(), close()
+
+ #define MAX_BUFFER_SIZE 80
+ #define PORT 50501
+ #define SA struct sockaddr
+
+ int start_chat(int is_client);
+
+#endif // SHARED_HOOK_H