Simple Hello Server/Client program

This commit is contained in:
2023-11-15 00:01:36 +05:30
commit d1c0a6b0f1
15 changed files with 308 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="Client" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/Client" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/Client" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
<Add option="-fexceptions" />
</Compiler>
<Unit filename="main.cpp" />
<Extensions />
</Project>
</CodeBlocks_project_file>

View File

@@ -0,0 +1,52 @@
//Client side program in C/C++
#include <stdint.h>
#include <netinet/ip.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
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;
}

View File

@@ -0,0 +1,12 @@
# depslib dependency file v1.0
1699986008 source:/home/hizenberg/Desktop/My Own Redis/Server-Client Program/Client/Client.cpp
<stdint.h>
<netinet/ip.h>
<stdio.h>
<stdlib.h>
<string.h>
<sys/socket.h>
<unistd.h>
<errno.h>
<arpa/inet.h>

Binary file not shown.

Binary file not shown.

Binary file not shown.