commit d1c0a6b0f13bbf7c0cda014245bad3537f500370 Author: Hizenberg469 Date: Wed Nov 15 00:01:36 2023 +0530 Simple Hello Server/Client program diff --git a/Server-Client Program/Client/Client.cbp b/Server-Client Program/Client/Client.cbp new file mode 100644 index 0000000..7e6edbc --- /dev/null +++ b/Server-Client Program/Client/Client.cbp @@ -0,0 +1,38 @@ + + + + + + diff --git a/Server-Client Program/Client/Client.cpp b/Server-Client Program/Client/Client.cpp new file mode 100644 index 0000000..2baa6f0 --- /dev/null +++ b/Server-Client Program/Client/Client.cpp @@ -0,0 +1,52 @@ +//Client side program in C/C++ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +using namespace std; +static void msg( const char* msg){ + fprintf(stderr, "%s\n", msg); +} + +static void die(const char* msg){ + int err = errno; + fprintf(stderr, "[%d] %s\n", err,msg); + abort(); +} + +int main(){ + + int fd = socket(AF_INET, SOCK_STREAM, 0); + if( fd < 0 ){ + die("socket()"); + } + + struct sockaddr_in addr = {}; + addr.sin_family = AF_INET; + addr.sin_port = ntohs(1234); + addr.sin_addr.s_addr = ntohl(INADDR_LOOPBACK);// 127.0.0.1 + int rv = connect(fd, (const struct sockaddr*)&addr, sizeof(addr)); + if( rv ){ + die("connect"); + } + + char msg[] = "hello"; + write(fd, msg, strlen(msg)); + + char rbuf[64] = {}; + ssize_t n = read(fd, rbuf , sizeof(rbuf) - 1); + if( n < 0 ){ + die("read"); + } + printf("server says: %s\n", rbuf); + close(fd); + return 0; +} diff --git a/Server-Client Program/Client/Client.depend b/Server-Client Program/Client/Client.depend new file mode 100644 index 0000000..d3f1abd --- /dev/null +++ b/Server-Client Program/Client/Client.depend @@ -0,0 +1,12 @@ +# depslib dependency file v1.0 +1699986008 source:/home/hizenberg/Desktop/My Own Redis/Server-Client Program/Client/Client.cpp + + + + + + + + + + diff --git a/Server-Client Program/Client/bin/Debug/Client b/Server-Client Program/Client/bin/Debug/Client new file mode 100755 index 0000000..2c16833 Binary files /dev/null and b/Server-Client Program/Client/bin/Debug/Client differ diff --git a/Server-Client Program/Client/client b/Server-Client Program/Client/client new file mode 100755 index 0000000..a78c056 Binary files /dev/null and b/Server-Client Program/Client/client differ diff --git a/Server-Client Program/Client/obj/Debug/Client.o b/Server-Client Program/Client/obj/Debug/Client.o new file mode 100644 index 0000000..951fbde Binary files /dev/null and b/Server-Client Program/Client/obj/Debug/Client.o differ diff --git a/Server-Client Program/Server/Server.cbp b/Server-Client Program/Server/Server.cbp new file mode 100644 index 0000000..e98b5ca --- /dev/null +++ b/Server-Client Program/Server/Server.cbp @@ -0,0 +1,38 @@ + + + + + + diff --git a/Server-Client Program/Server/Server.cpp b/Server-Client Program/Server/Server.cpp new file mode 100644 index 0000000..022506a --- /dev/null +++ b/Server-Client Program/Server/Server.cpp @@ -0,0 +1,86 @@ +//Server side Program in C/C++ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +static void msg( const char* msg){ + fprintf(stderr, "%s\n", msg); +} + +static void die(const char* msg){ + int err = errno; + fprintf(stderr, "[%d] %s\n", err,msg); + abort(); +} + +static void do_something(int connfd){ + char rbuf[64] = {}; + + ssize_t n = read(connfd, rbuf, sizeof(rbuf) - 1); + + if( n < 0 ){ + msg("read() error"); + return; + } + + printf("Client says: %s\n",rbuf); + + char wbuf[] = "world"; + write(connfd , wbuf, strlen(wbuf)); +} + + +int main(){ + //syscall to return integer for socket communication from kernel + int fd = socket(AF_INET, SOCK_STREAM , 0); + if( fd < 0 ){ + die("socket()"); + } + + //Set the option in various layer which will establish communication. + int val = 1; + setsockopt(fd, SOL_SOCKET , SO_REUSEADDR, &val , sizeof(val)); + + //bind, dealing wih IPv4 addresses + struct sockaddr_in addr = {}; + addr.sin_family = AF_INET; + addr.sin_port = ntohs(1234); + addr.sin_addr.s_addr = ntohl(0);// wildcard address 0.0.0.0 + int rv = bind(fd, (const sockaddr*)&addr, sizeof(addr)); + + if(rv){ + die("bind()"); + } + + //listen + rv = listen(fd , SOMAXCONN); + + if( rv ){ + die("listen()"); + } + + //Looping to connect to each connection. + while(true){ + //accept + struct sockaddr_in client_addr = {}; + socklen_t socklen = sizeof( client_addr ); + int connfd = accept(fd , (struct sockaddr*)& client_addr, &socklen); + if( connfd < 0 ){ + //error + printf("Error encountered while connecting with client."); + } + + do_something(connfd); + close(connfd); + } + + return 0; +} diff --git a/Server-Client Program/Server/bin/Debug/Server b/Server-Client Program/Server/bin/Debug/Server new file mode 100755 index 0000000..24f95df Binary files /dev/null and b/Server-Client Program/Server/bin/Debug/Server differ diff --git a/Server-Client Program/Server/obj/Debug/Server.o b/Server-Client Program/Server/obj/Debug/Server.o new file mode 100644 index 0000000..5b79cc6 Binary files /dev/null and b/Server-Client Program/Server/obj/Debug/Server.o differ diff --git a/Server-Client Program/Server/server b/Server-Client Program/Server/server new file mode 100755 index 0000000..e513af1 Binary files /dev/null and b/Server-Client Program/Server/server differ diff --git a/Server-Client Program/Tut-Sheet-1.txt b/Server-Client Program/Tut-Sheet-1.txt new file mode 100644 index 0000000..6fe83e7 --- /dev/null +++ b/Server-Client Program/Tut-Sheet-1.txt @@ -0,0 +1,36 @@ +socket() -> this is a syscall which returns an fd. + + fd - It is like a integer which is like an ID used by kernel. It is basically related + to kernel functionality. + +bind() -> It is used to bind the address to the fd. + -> The address is which we want to connect with. + +listen() -> listen helps to establish connection with that address. + +accept() -> when connection is made my client, it returns an fd which represents the established connection. + +WORKFLOW OF SERVER (PSEUDO CODE): + + fd = socket(); + bind(fd,address) + listen(fd) + while(true) + conn_fd = accept(fd) + /**************/ + do something with conn_fd + doing something in this connection + /**************/ + close(conn_fd) + + +connect() -> take the fd from client side and make the connection from the address that + -> the address is of the server side. +WORKFLOW OF CLIENT (PSEUDO CODE) + + fd = socket() + connect(fd,address) + /*********/ + doing something in the connection/ + /*********/ + close(fd) diff --git a/Server-Client Program/Tut-Sheet-2.txt b/Server-Client Program/Tut-Sheet-2.txt new file mode 100644 index 0000000..34a2eea --- /dev/null +++ b/Server-Client Program/Tut-Sheet-2.txt @@ -0,0 +1,33 @@ +Starting of client and server. + +1. First we need fd using socket() + + int fd = socket(AF_INET,SOCK_STREAM,0); + + ->AF_INET = It is used to provide IPv4 + NOTE: AF_INET6 = It is used to provide IPv6 or dual-stack socket. + ->SOCK_STREAM = It is used to set the TCP protocol. + + +2. NEW SYSTEM CALL + + setsocketopt() + -> There are various layer of network communication and each layer have various option, from which we pick one to + configure the network communication. + -> This syscall is to set those option + + IMPORTANT: SO_REUSEADDR-> It is an option to reuse the same address when the process(communication) is restarted. + -> Risk of using it. + # If the previous connection using the same address didn't took all the packets and the process got closed + before taking it, the same address might be used by the same program with new instance of process or by another + program with new instance of process. This rouge packets is intented to send it to the address which is in reuse. + This cause problem. This problem also make it hackable. + + + NEW TERM: WILDCARD ADDRESS -> when we use sockets, the communication is design to between two process only. + Not allowing multicast or multiple communication. To establish this multiple communication we use wildcard address. + This is used on server-side program. + + +3. read() and write() + It returns number of read and written bytes. diff --git a/Server-Client Program/default.workspace b/Server-Client Program/default.workspace new file mode 100644 index 0000000..3bf095a --- /dev/null +++ b/Server-Client Program/default.workspace @@ -0,0 +1,7 @@ + + + + + + + diff --git a/Server-Client Program/default.workspace.layout b/Server-Client Program/default.workspace.layout new file mode 100644 index 0000000..914b302 --- /dev/null +++ b/Server-Client Program/default.workspace.layout @@ -0,0 +1,6 @@ + + + + + +