-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.cpp
More file actions
42 lines (32 loc) · 1.06 KB
/
Copy pathClient.cpp
File metadata and controls
42 lines (32 loc) · 1.06 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
#include "SocketUtil.cpp"
int main() {
int socketFD = createTCPIpv4Socket();
char *ip = (char *)"127.0.0.1";
struct sockaddr_in* address = createIPv4Address(ip, 7000);
int res = connect(socketFD, reinterpret_cast<struct sockaddr*>(address), sizeof(*address));
if (res == 0) {
cout << "Successful Connection!!" << endl;
} else {
cout << "Unsuccessful Connection" << endl;
return 1;
}
cout << "Enter your username: ";
char username[1024];
cin.getline(username, 1024);
startListeningAndDisplay(socketFD);
char* line = NULL;
size_t len = 0;
cout << "Write some message to send" << endl;
while(true) {
ssize_t charCount = getline(&line, &len, stdin);
if(charCount > 0) {
if(strcmp(line, "exit\n") == 0) {
break;
}
string messageToSend = string(username) + ": " + string(line);
ssize_t amountSent = send(socketFD, messageToSend.c_str(), messageToSend.length(), 0);
}
}
close(socketFD);
return 0;
}