1+ #include < arpa/inet.h>
2+ #include < stdio.h>
3+ #include < string.h>
4+ #include < sys/socket.h>
5+ #include < unistd.h>
6+ #include < iostream>
7+ #include " ../utils/string_utility.h"
8+ #define PORT 6378
9+
10+ int main (int argc, char const * argv[]) {
11+
12+ int status, valread, client_fd;
13+ struct sockaddr_in serv_addr;
14+
15+ // creating socket
16+ char buffer[1024 ] = { 0 };
17+ if ((client_fd = socket (AF_INET, SOCK_STREAM, 0 )) < 0 ) {
18+ printf (" \n redis-cli: Socket creation error \n " );
19+ return -1 ;
20+ }
21+
22+ serv_addr.sin_family = AF_INET;
23+ serv_addr.sin_port = htons (PORT);
24+
25+ // Convert IPv4 and IPv6 addresses from text to binary
26+ if (inet_pton (AF_INET, " 127.0.0.1" , &serv_addr.sin_addr ) <= 0 ) {
27+ printf (
28+ " \n Invalid address/ Address not supported \n " );
29+ return -1 ;
30+ }
31+
32+ // Connecting to socket
33+ if ((status = connect (client_fd, (struct sockaddr *)&serv_addr,
34+ sizeof (serv_addr))) < 0 ) {
35+ printf (" \n Connection Failed \n " );
36+ return -1 ;
37+ }
38+
39+ printf (" Connection established\n " );
40+
41+ // client commands input logic
42+ while (true ) {
43+ std::cout<<" >" ;
44+ std::string input;
45+ std::getline (std::cin, input);
46+
47+ string cmd = split_string (input)[0 ];
48+ transform (cmd.begin (), cmd.end (), cmd.begin (), ::toupper);
49+
50+ if (cmd == " EXIT" || cmd == " QUIT" || cmd == " Q" ) {
51+ break ;
52+ }
53+
54+ int sentBytes;
55+ if (sentBytes = send (client_fd, input.c_str (), input.size (), 0 ) < 0 ) {
56+ printf (" \n Error while sending data to server\n " );
57+ continue ;
58+ }
59+
60+ valread = read (client_fd, buffer, 1024 );
61+ printf (" %s\n " , buffer);
62+ }
63+
64+ return 0 ;
65+
66+ }
0 commit comments