-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbf_client.cpp
75 lines (64 loc) · 2.38 KB
/
bf_client.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
// Created by cuongbv on 28/04/2020.
//
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <string>
#include <iostream>
#define PORT 6666
/** The main program run as a Socket Client (C)
*
* @param argc
* @param argv
* @return
*/
int main(int argc, char const *argv[]) {
int socketClient; // The socket file description of the socket client side
int readStatus; // status when read response from a Socket Server
struct sockaddr_in sockServerAddr{}; // The structure of a Socket Server address
if (argc < 2) {
std::cerr << "\"$ ./bf_client help\" for usage." << std::endl;
return 1;
}
// Handle command line arguments
std::string command (argv[1]); // The command will impact with a Socket Server side
std::string param; // The parameter will pass to a Socket Server side
if (argc == 3) param = argv[2];
else param = "";
std::string fullCommand (command + " " + param);
char *passingCommand = const_cast<char *>(fullCommand.c_str());
char receiveBuffer[1024] = {0}; // The buffer which will contain a response from a Socket Server
// Create the Socket Client
if ((socketClient = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("Socket creation error\n");
return 1;
}
// Assign parameters into Socket Server address structure
sockServerAddr.sin_family = AF_INET; // IPv4
sockServerAddr.sin_port = htons(PORT); // Encode port value
if(inet_pton(AF_INET, "127.0.0.1", &sockServerAddr.sin_addr) <= 0) {
printf("\nInvalid address/ Address not supported \n");
return 1;
}
// Construct a connection to a Socket Server side
if (connect(socketClient, (struct sockaddr *)&sockServerAddr, sizeof(sockServerAddr)) < 0) {
printf("\nConnection Failed \n");
return 1;
}
// Send the request command to Socket Server side
send(socketClient , passingCommand , strlen(passingCommand) , 0 );
// Read and store the response from the Socket Server side
readStatus = read(socketClient , receiveBuffer, 1024);
if (readStatus < 0) {
printf("Fail to read response from Socket server!\n");
return 1;
}
// Print the response out to console
printf("%s\n", receiveBuffer);
// Close socket and return
close(socketClient);
return 0;
}