mirror of
https://github.com/Hizenberg469/My-Own-Redis.git
synced 2026-04-20 02:52:22 +03:00
Simple Hello Server/Client program
This commit is contained in:
52
Server-Client Program/Client/Client.cpp
Normal file
52
Server-Client Program/Client/Client.cpp
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user