aboutsummaryrefslogtreecommitdiff
path: root/works/life/computer-network-experiment/client.cpp
blob: 922ecdc26ea1bf85fae0efcb32bb6866b4ed0a60 (plain)
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
/** Created by crupest.
 *  This is the client program.
 */

#include "Common.h"
#include "Output.h"

#ifdef WIN32
#include <Windows.h>
#include <winsock.h>
#else
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>

#endif

const auto connect_address = "127.0.0.1"; // control connect address
const u_short port = 1234;                // control connect port

int Main() {
  int client_socket;

  if ((client_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    PrintErrorMessageAndExit(CRUT("Failed to create socket!\n"));
  }

  sockaddr_in server_address;

  server_address.sin_family = AF_INET;
  server_address.sin_port = htons(port);
  server_address.sin_addr.s_addr = inet_addr(connect_address);
  memset(&(server_address.sin_zero), 0, sizeof(server_address.sin_zero));

  if (connect(client_socket, (sockaddr *)&server_address, sizeof(sockaddr)) ==
      -1) {
    PrintErrorMessageAndExit(CRUT("Failed to connect!"));
  }

  const int buffer_size = 100;
  char *buffer = new char[buffer_size];

  int received_number = recv(client_socket, buffer, buffer_size, 0);

  if (received_number == -1) {
    PrintErrorMessageAndExit(CRUT("Failed to recv."));
  }

  std::string s(buffer, received_number);

  SendOutput(OutputColor::Green, CRUT("Received message:\n"));
  SendOutput(OutputColor::Normal, CRUT("{}\n"), ConvertCharString(s));

  CloseSocket(client_socket);
  return 0;
}