-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.cpp
More file actions
146 lines (117 loc) · 3.86 KB
/
Client.cpp
File metadata and controls
146 lines (117 loc) · 3.86 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
// Created by Edo on 2/11/2019.
//
#include <thread>
#include <string.h>
#include "Application.h"
#include "vusocket.h"
#include "Client.h"
#include <iostream>
void Client::tick() {
if(loginStatus == ConnStatus::SUCCESS) {
while(true){
char username[] = "";
std::cout << "Please enter your message:";
fgets(message.out, BUFFER_LENGTH, stdin);
strcat(username, message.out);
if (strcmp(username, "!quit\n") == 0){
loginStatus = ConnStatus::QUIT;
std::cout << "Quiting chat client." << std::endl;
return;
}
}
}
}
int Client::readFromSocket() {
return 0;
}
int Client::readFromStdin() {
return 0;
}
void Client::createSocketAndLogIn() {
OutputDebugStringW(L"Creating socket.");
std::cout << "Creating socket and log in" << std::endl;
loginStatus == ConnStatus::IN_PROGRESS;
struct addrinfo hints = {0}, *addrs;
const char *host = "52.58.97.202";
const char *port = "5378";
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
sock_init();
const int addrinfo = getaddrinfo(host, port, &hints, &addrs);
if (addrinfo != 0) {
std::cout << "Host not found." << std::endl;
abort();
}
for (struct addrinfo *adr = addrs; adr != nullptr; adr = adr -> ai_next) {
if ((sock = socket(adr -> ai_family, adr -> ai_socktype, adr -> ai_protocol)) == -1) {
sock_error_code();
continue;
}
if (connect((SOCKET) sock, adr -> ai_addr, adr -> ai_addrlen) == -1) {
sock_error_code();
sock_close(sock);
continue;
}
std::cout << "Successfully connected to the server" << std::endl;
break;
}
while (loginStatus == ConnStatus::IN_PROGRESS) {
if (SendUserName()) {
loginStatus = ReceiveResponseFromServer();
}
}
}
// Send username to server
bool Client::SendUserName() {
memset(&message.out, 0x00, sizeof(message.out));
char username[] = "", send_message[] = "HELLO-FROM ";
std::cout << "Please enter your user name:";
fgets(message.out, BUFFER_LENGTH, stdin);
strcat(username, message.out);
if (strcmp(username, "!quit\n") == 0){
loginStatus = ConnStatus::QUIT;
std::cout << "Quiting chat client." << std::endl;
return false;
}
strcat(send_message, username);
puts(send_message);
int len = strlen(send_message);
int send_len = send(sock, send_message, len, 0);
if (send_len) {
OutputDebugStringW(L"Send Success! SIZE: " + send_len);
return true;
}else {
OutputDebugStringW(L"Error sending username.");
return false;
}
}
// Receive response of server
ConnStatus Client::ReceiveResponseFromServer() {
memset(&message.in, 0x00, sizeof(message.in));
int recv_length = recv(sock, message.in, BUFFER_LENGTH, 0);
if (recv_length != -1) {
OutputDebugStringW(L"Send Success! SIZE: " + recv_length);
std::cout << "SERVER: " << message.in << std::endl;
if (!strncmp("IN-USE", message.in, 6)) {
OutputDebugStringW(L"Username in-use, ask for new username.");
printf("Username in-use, try another username.");
return ConnStatus::IN_PROGRESS;
}
else if (!strncmp("BUSY", message.in, 4)) {
std::cout << ("Server is busy. Try again later.") << std::endl;
return ConnStatus::BUSY;
}
else {
OutputDebugStringW(L"Successfully established connection.");
return ConnStatus::SUCCESS;
}
}else {
std::cout << "Failed to establish connection with server";
return ConnStatus::FAILED;
}
}
void Client::closeSocket() {
std::cout << "Closing socket" << std::endl;
}