-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
executable file
·122 lines (106 loc) · 3.48 KB
/
Copy pathclient.cpp
File metadata and controls
executable file
·122 lines (106 loc) · 3.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
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
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <unistd.h>
#include <arpa/inet.h>
#include <algorithm>
using namespace std;
#define PORT 8080
#define BUFFER_SIZE 1024
#define SERVER_IP "13.201.81.26"
class UDPClient
{
private:
int sockfd;
struct sockaddr_in servaddr;
socklen_t len;
public:
UDPClient()
{
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0)
{
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
cout << "[DEBUG CLIENT] Socket created successfully." << endl;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
int res = inet_pton(AF_INET, SERVER_IP, &servaddr.sin_addr);
if (res <= 0)
{
perror("Invalid address/ Address not supported");
exit(EXIT_FAILURE);
}
cout << "[DEBUG CLIENT] Server address set successfully." << endl;
len = sizeof(servaddr);
}
void run()
{
string filename;
cout << "Enter the file name: ";
cin >> filename;
filename.erase(remove_if(filename.begin(), filename.end(), [](unsigned char c)
{ return isspace(c); }),
filename.end());
cout << "[DEBUG CLIENT] Cleaned filename: " << filename << endl;
int bytesSent = sendto(sockfd, filename.c_str(), filename.size(), 0, (const struct sockaddr *)&servaddr, len);
if (bytesSent < 0)
{
perror("Failed to send filename");
close(sockfd);
exit(EXIT_FAILURE);
}
cout << "[DEBUG CLIENT] Filename sent to server: " << filename << endl;
char buffer[BUFFER_SIZE];
int bytesReceived = recvfrom(sockfd, buffer, BUFFER_SIZE, 0, (struct sockaddr *)&servaddr, &len);
if (bytesReceived < 0)
{
perror("Error receiving from server");
close(sockfd);
exit(EXIT_FAILURE);
}
buffer[bytesReceived] = '\0';
cout << "[DEBUG CLIENT] Received response from server: " << buffer << endl;
if (strcmp(buffer, "NOTFOUND") == 0)
{
cout << "File not found on server." << endl;
}
else
{
ofstream newFile("received_file.txt");
newFile << buffer << endl;
cout << "[DEBUG CLIENT] Received and saved first word: " << buffer << endl;
while (true)
{
bytesReceived = recvfrom(sockfd, buffer, BUFFER_SIZE, 0, (struct sockaddr *)&servaddr, &len);
if (bytesReceived < 0)
{
perror("Error receiving word from server");
break;
}
buffer[bytesReceived] = '\0';
if (strcmp(buffer, "END") == 0)
{
cout << "[DEBUG CLIENT] 'END' received. Stopping file transfer." << endl;
break;
}
newFile << buffer << endl;
cout << "[DEBUG CLIENT] Received and saved word: " << buffer << endl;
}
newFile.close();
}
cout<<"[DEBUG CLIENT] before socket close"<<endl;
close(sockfd);
cout<<"[DEBUG CLIENT] after socket close"<<endl;
exit(EXIT_SUCCESS); // Exit the program successfully
}
};
int main()
{
UDPClient client;
client.run();
return 0;
}