-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_echo.cpp
209 lines (194 loc) · 5.11 KB
/
client_echo.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <iostream>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sstream>
#include <unistd.h>
#include <string.h>
#include <fstream>
using namespace std;
string get_data_string(string file_name) //takes name of file as the input and returns a string that contains all the text
{
ifstream file(file_name.c_str());
string word;
char x ;
word.clear();
int count = 0;
while ((x = file.get())!= EOF)
{
word = word + x;
}
file.close();
return word;
}
void str2char(string data, char* destination){
int size = data.size();
char strarray[size];
for(int i = 0;i < size;i++){
destination[i] = (char)data[i];
}
}
int send(int file_descriptor,string message,int count){
char buffer[2*count];
bzero(buffer,2*count);
str2char(message,buffer);
int n = -1;
n = write(file_descriptor,buffer,count);
if(n<0){
cout<<"Error writing to port"<<endl;
}
return n;
}
string recieve(int file_descriptor, int count){
char buffer[2*count];
bzero(buffer,2*count);
int n = read(file_descriptor,buffer,count);
if(n<0){
cout<<"ERROR reading from socket"<<endl;
}
string stri(buffer);
return stri;
}
void show_available_files(int file_descriptor){
int n = send(file_descriptor,"ls",128);
if(n<0){
cout<<"error asking to show files"<<endl;
}
string list = recieve(file_descriptor,512);
if(list.size()<1){
cout<<"error reading list of files"<<endl;
}
else{
cout<<list<<endl;
}
}
void put_file(int file_descriptor, string file_name){
string content = get_data_string(file_name);
int n = send(file_descriptor,string("put")+" "+file_name,256);
if(n<0){
cout<<"error sending file..."<<endl;
}
n = send(file_descriptor,content,1024);
if(n<0){
cout<<"error uploading file"<<endl;
}
}
void get_file(int file_descriptor,string file_name){
int n = send(file_descriptor,string("get")+" "+file_name,256);
string content = "";
if(n<0){
cout<<"error requesting file"<<endl;
}
else{
content = recieve(file_descriptor,1024);
if(content.size()<1){
cout<<"error in downloading file"<<endl;
}
else if(content == "No such files found"){
cout<<content<<endl;
cout<<"try ls to check what files are present"<<endl;
}
else{
ofstream file;
file.open(file_name.c_str());
file << content;
file.close();
cout<<"download successful"<<endl;
}
}
}
void show_menu(int file_descriptor){
int loop_invariant = 0;
int choice = -1;
string file_name = "";
while(loop_invariant == 0){
cout<<"Enter command to"<<endl<<"1. get list of files"<<endl<<"2. download file"
<<endl<<"3. upload file" <<endl<<"4. get current time "<<endl<<"5. get current directory"<<endl<<"6. create new file "<<endl;
cin>>choice;
switch(choice){
case 1: show_available_files(file_descriptor);
break;
case 2:cout<<"enter the file name to download"<<endl;
cin>>file_name;
get_file(file_descriptor,file_name);
break;
case 3:cout<<"enter the file name to upload"<<endl;
file_name.clear();
cin>>file_name;
put_file(file_descriptor,file_name);
break;
case 4: cout<<"Current time at server is: "<<endl;
send(file_descriptor,string("time"),256);
cout<<recieve(file_descriptor,25);
break;
case 5: cout<<"Current working directory is: "<<endl;
send(file_descriptor,string("time"),256);
cout<<recieve(file_descriptor,25);
break;
case 6: cout<<"Enter file name"<<endl;
cin>>file_name;
send(file_descriptor,string("touch")+" "+file_name,256);
break;
case 9: loop_invariant = 1;
break;
}
}
}
void telnet_sum(int file_descriptor){
int n = -11;
string no;
int p =0;
stringstream stream;
cout<<"Enter numbers to add"<<endl;
while(p==0){
cin>>n;
if(n<0){
stream << n;
string no = stream.str();
send(file_descriptor,no,256);
break;
}
stream << n;
string no = stream.str();
send(file_descriptor,no,256);
}
cout<<"The sum is : "<<recieve(file_descriptor,256)<<endl;
}
int main(){
int domain = AF_INET;
int type = SOCK_STREAM;
int protocol = 0;
int status = 0;
int file_descriptor = 0;
int port_number = 5010;
struct sockaddr_in server_socket;
string name = "sherlock";
file_descriptor = socket(domain,type,protocol);
if(file_descriptor < 0){
cout<<"Error, failed to create sockets"<<endl;
}
else{
bzero((char *) &server_socket, sizeof(server_socket));
server_socket.sin_family = AF_INET;
server_socket.sin_addr.s_addr = inet_addr("127.1.0.1");
server_socket.sin_port = htons(port_number);
status = connect(file_descriptor, (struct sockaddr*)&server_socket, sizeof(server_socket));
if(status < 0){
cout<<"Could not connect, please try again..."<<endl;
}
else{
cout<<" ..........connected succesfully.........."<<endl;
cout<<"enter your name"<<endl;
cin>>name;
int n = send(file_descriptor,string("hello from ")+name,20);
if(n<0){
cout<<"error in client"<<endl;
}
else{
cout<<recieve(file_descriptor,20);
}
}
}
}