-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.cpp
73 lines (59 loc) · 2.11 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
73
#include <cstring>
#include <iostream>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
using namespace std;
int main() {
// Creating socket
int clientSocket = socket(AF_INET, SOCK_STREAM, 0);
if (clientSocket == -1) {
cerr << "Failed to create socket." << endl;
return 1;
}
// Specifying address
sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(8080);
serverAddress.sin_addr.s_addr = INADDR_ANY; // Connect to localhost; change if needed.
// Sending connection request
if (connect(clientSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) == -1) {
cerr << "Failed to connect to the server." << endl;
close(clientSocket); // Clean up
return 1;
}
cout << "Connected to the server." << endl;
// Loop to send Y86 instructions until "quit" or "q" is sent
string message;
char buffer[1024] = {0};
while (true) {
cout << "Enter Y86 instruction (or 'quit' to exit): ";
getline(cin, message);
// Check if the message is "quit" or "q"
if (message == "quit" || message == "q") {
cout << "Closing connection..." << endl;
break;
}
// Sending data to the server
ssize_t bytesSent = send(clientSocket, message.c_str(), message.size(), 0);
if (bytesSent < 0) {
cerr << "Error sending message to server." << endl;
break;
}
// Receiving the server's response
memset(buffer, 0, sizeof(buffer)); // Clear buffer before receiving
int bytesReceived = recv(clientSocket, buffer, sizeof(buffer), 0);
if (bytesReceived <= 0) {
cerr << "Error receiving message from server or server disconnected." << endl;
break;
}
// Ensure the buffer is null-terminated
buffer[bytesReceived] = '\0';
// Print the server's response
cout << "Server response: " << buffer << endl;
}
// Closing the socket
close(clientSocket);
cout << "Client closed." << endl;
return 0;
}