-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
executable file
·223 lines (184 loc) · 5.56 KB
/
Copy pathserver.c
File metadata and controls
executable file
·223 lines (184 loc) · 5.56 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#define MYPORT 5000
#define BACKLOG 10
#define MAX 80
char* serverDir = "/home/alex/ServerDir/";
// where the wanted file is stored
void send_file(char filename[],int new_fd)
{
char full_path[30]={0};
printf("File is - %s\n",filename);
// // construct full path to file location
// strcat(full_path,serverDir);
// printf("Full path - %s\n",full_path);
// strcat(full_path,filename);
// printf("Full command 2- %s\n",full_path);
sprintf(full_path,"%s%s",serverDir,filename); // construct full path using serverDir and the filename received
printf("Command full is - %s\n",full_path);
char ch;
FILE *file = fopen(full_path,"r");
if(file == NULL)
{
perror("Failed: ");
}
char buffer[100]={0};
int bytes_read;
printf("Opened with succes.\n");
while (!feof(file))
{
if ((bytes_read = fread(&buffer, 1, sizeof(buffer), file)) > 0) // as long as there are bytes to read keep sending them
{
send(new_fd, buffer, bytes_read, 0);
printf("Inside if.\n");
}
else
{
printf("End of file...\n");
break;
}
}
bzero(buffer,sizeof(buffer));
buffer[0]=0;
send(new_fd,buffer,sizeof(buffer),0); // sending 0 will notify the client that all data was sent
printf("File sent.");
fclose(file);
}
// function that sends the output of ls
void send_dir_ls(char* command,int new_fd)
{
char full_command[26]={0};
printf("Command is - %s\n",command);
// adding ls to our full buffer and the server dir
strcat(full_command,"ls ");
printf("Full command - %s\n",full_command);
strcat(full_command,serverDir);
printf("Full command 2- %s\n",full_command);
printf("Command full is - %s\n",full_command);
FILE *fp=popen(full_command,"r");
if(!fp)
{
fprintf(stderr,"Pipe could not be opened.\n");
exit(1);
}
char buff[256] = { 0 };
int s= 256;
while(fgets(buff,256,fp)) // read from pipe command 256 bytes at a time
{
printf("%s\n",buff);
// seding the number of files first
if(send(new_fd,buff,256,0) == -1)
{
printf("Error sending data...");
exit(1);
}
if(feof(fp))
{
printf("End of file...");
break;
}
printf("Still reading file...\n");
}
bzero(buff,sizeof(buff));
buff[0] = 0;
send(new_fd,buff,sizeof(buff),0); // sending 0 will notify the client that all data was sent
fclose(fp);
}
// Function designed for chat between client and server.
void communicate(int sockfd)
{
char buff[MAX];
int n;
int exit = 0;
// infinite loop for chat
do{
bzero(buff, MAX); // reinitialize the buffer , empty it's value
// read the message from client and copy it in buffer
//printf("Before recv\n");
if(recv(sockfd, buff, sizeof(buff),0)==-1)
{
perror("Failed to receive: ");
}
//printf("After recv..\n");
// print buffer which contains the client contents
//printf("From client: %s\n", buff);
if(strcmp(buff,"ls\n")==0)
{
printf("From client: %s\n", buff);
bzero(buff, MAX);
send_dir_ls("ls",sockfd);
printf("%s\n",buff);
//break;
}
else if(strcmp(buff,"exit\n")==0)
{
printf("From client: %s\n", buff);
printf("Client disconnected...\n");
exit=1;
//break;
}
else if(strcmp(buff,"")!=0)
{
printf("From client: %s\n", buff);
//bzero(buff, MAX);
printf("Sending file.\n");
strtok(buff,"\n"); // it will write a string terminator when it encounter "\n"
send_file(buff,sockfd);
//break;
}
}while(exit==0);
}
int main(void)
{
int sockfd, new_fd;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
int sin_size;
int yes=1;
// linux ls command variables
char command[40];
char* data;
char file_wanted[40]={0};
printf("Server started...");
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
perror("setsockopt");
exit(1);
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT);
my_addr.sin_addr.s_addr = INADDR_ANY;
memset(&(my_addr.sin_zero), '\0', 8);
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
}
if (listen(sockfd, BACKLOG) == -1) {
perror("listen");
exit(1);
}
while(1) {
sin_size = sizeof(struct sockaddr_in);
if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr,&sin_size)) == -1) {
perror("accept");
continue;
}
printf("server: conexiune de la: %s\n",inet_ntoa(their_addr.sin_addr));
printf("Wating for a command...\n");
communicate(new_fd); // functions that handles the client requests (ls or filenames)
close(new_fd);
}
return 0;
}