-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.cpp
72 lines (59 loc) · 1.97 KB
/
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
#include <iostream>
#include <unistd.h>
#include <arpa/inet.h>
#include <string.h>
#define PORT 8080
#define BUFFER_SIZE 1024
using namespace std;
int main() {
int sock;
struct sockaddr_in server_addr;
char buffer[BUFFER_SIZE];
// Create socket
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("Socket failed");
exit(EXIT_FAILURE);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
// Connect to server (replace with actual IP address if necessary)
if (inet_pton(AF_INET, "127.0.0.1/8", &server_addr.sin_addr) <= 0) {
perror("Invalid address");
exit(EXIT_FAILURE);
}
if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("Connection failed");
exit(EXIT_FAILURE);
}
// Read server greeting and options
memset(buffer, 0, BUFFER_SIZE);
read(sock, buffer, BUFFER_SIZE);
cout << buffer;
memset(buffer, 0, BUFFER_SIZE);
read(sock, buffer, BUFFER_SIZE);
cout << buffer;
// Send choice (Register or Login)
string choice;
cout << "Enter your choice: ";
cin >> choice;
cin.ignore(); // Ignore any remaining newline characters in the input buffer.
send(sock, choice.c_str(), choice.length(), 0);
// Handle registration or login based on choice
if (choice == "1") {
string username, password;
cout << "Enter username: ";
getline(cin, username);
cout << "Enter password: ";
getline(cin, password);
// Send username and password to server
send(sock, username.c_str(), username.length(), 0);
send(sock, "\n", 1, 0); // Send newline to separate username and password
send(sock, password.c_str(), password.length(), 0);
cout << "Registering..." << endl;
} else if (choice == "2") {
cout << "Logging in..." << endl;
}
// Close connection
close(sock);
return 0;
}