-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
87 lines (72 loc) · 2.48 KB
/
client.cpp
File metadata and controls
87 lines (72 loc) · 2.48 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
#include <iostream>
#include <string>
#include <cstring>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/select.h>
using namespace std;
const int PORT = 8080;
const int BUFFER_SIZE = 1024;
// Function to format user input into protocol commands
string format_command(const string& line) {
if (line.rfind("/nick ", 0) == 0) { // Check if line starts with "/nick "
return "NICK " + line.substr(6);
}
if (line.rfind("/msg ", 0) == 0) { // Check if line starts with "/msg "
return "MSG " + line.substr(5);
}
if (line == "/list") {
return "LIST";
}
return line; // Not a command, return as is
}
int main() {
int sock = 0;
struct sockaddr_in serv_addr;
char buffer[BUFFER_SIZE] = {0};
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
cout << "Socket creation error" << endl; return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
cout << "Invalid address/ Address not supported" << endl; return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
cout << "Connection Failed" << endl; return -1;
}
cout << "Connected to the server. You can start typing." << endl;
cout << "Commands: /nick <name>, /msg <name> <message>, /list" << endl;
while (true) {
fd_set read_fds;
FD_ZERO(&read_fds);
FD_SET(STDIN_FILENO, &read_fds);
FD_SET(sock, &read_fds);
int fdmax = (STDIN_FILENO > sock) ? STDIN_FILENO : sock;
cout << "> ";
fflush(stdout);
if (select(fdmax + 1, &read_fds, NULL, NULL, NULL) == -1) {
perror("select"); exit(EXIT_FAILURE);
}
if (FD_ISSET(sock, &read_fds)) {
int bytes_received = recv(sock, buffer, BUFFER_SIZE, 0);
if (bytes_received <= 0) {
cout << "\nServer disconnected." << endl; break;
}
cout << "\r" << string(buffer, bytes_received) << endl; // Use \r to overwrite prompt
memset(buffer, 0, BUFFER_SIZE);
}
if (FD_ISSET(STDIN_FILENO, &read_fds)) {
string line;
getline(cin, line);
if (line == "/exit") {
break;
}
string command = format_command(line);
send(sock, command.c_str(), command.length(), 0);
}
}
close(sock);
return 0;
}