Arjun Suresh (talk | contribs) |
Arjun Suresh (talk | contribs) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | <syntaxhighlight lang="C"> | + | <syntaxhighlight lang="C" name="ftpserver"> |
/*FTP server*/ | /*FTP server*/ | ||
Line 7: | Line 7: | ||
#include <stdio.h> | #include <stdio.h> | ||
#include <stdlib.h> | #include <stdlib.h> | ||
+ | |||
/*for getting file size using stat()*/ | /*for getting file size using stat()*/ | ||
#include<sys/stat.h> | #include<sys/stat.h> | ||
− | #include<sys/sendfile.h>// | + | |
− | #include<fcntl.h> | + | /*for sendfile()*/ |
+ | #include<sys/sendfile.h> | ||
+ | |||
+ | /*for O_RDONLY*/ | ||
+ | #include<fcntl.h> | ||
int main(int argc,char *argv[]) | int main(int argc,char *argv[]) | ||
{ | { | ||
− | struct sockaddr_in server,client; | + | struct sockaddr_in server, client; |
struct stat obj; | struct stat obj; | ||
− | int sock1,sock2; | + | int sock1, sock2; |
− | char buf[100],command[5],filename[20]; | + | char buf[100], command[5], filename[20]; |
− | int k,i,size,len,c; | + | int k, i, size, len, c; |
int filehandle; | int filehandle; | ||
− | sock1 = socket(AF_INET,SOCK_STREAM,0); | + | sock1 = socket(AF_INET, SOCK_STREAM, 0); |
− | if(sock1==-1) | + | if(sock1 == -1) |
{ | { | ||
printf("Socket creation failed"); | printf("Socket creation failed"); | ||
Line 29: | Line 34: | ||
server.sin_addr.s_addr = 0; | server.sin_addr.s_addr = 0; | ||
k = bind(sock1,(struct sockaddr*)&server,sizeof(server)); | k = bind(sock1,(struct sockaddr*)&server,sizeof(server)); | ||
− | if(k==-1) | + | if(k == -1) |
{ | { | ||
printf("Binding error"); | printf("Binding error"); | ||
exit(1); | exit(1); | ||
} | } | ||
− | k=listen(sock1,1); | + | k = listen(sock1,1); |
− | if(k==-1) | + | if(k == -1) |
{ | { | ||
printf("Listen failed"); | printf("Listen failed"); | ||
Line 41: | Line 46: | ||
} | } | ||
len = sizeof(client); | len = sizeof(client); | ||
− | sock2 = accept(sock1,(struct sockaddr*)&client,&len); | + | sock2 = accept(sock1,(struct sockaddr*)&client, &len); |
i = 1; | i = 1; | ||
while(1) | while(1) | ||
{ | { | ||
− | recv(sock2,buf,100,0); | + | recv(sock2, buf, 100, 0); |
− | sscanf(buf,"%s",command); | + | sscanf(buf, "%s", command); |
− | if(!strcmp(command,"ls")) | + | if(!strcmp(command, "ls")) |
{ | { | ||
system("ls >temps.txt"); | system("ls >temps.txt"); | ||
Line 53: | Line 58: | ||
stat("temps.txt",&obj); | stat("temps.txt",&obj); | ||
size = obj.st_size; | size = obj.st_size; | ||
− | send(sock2,&size,sizeof(int),0); | + | send(sock2, &size, sizeof(int),0); |
− | filehandle = open("temps.txt",O_RDONLY); | + | filehandle = open("temps.txt", O_RDONLY); |
sendfile(sock2,filehandle,NULL,size); | sendfile(sock2,filehandle,NULL,size); | ||
} | } | ||
else if(!strcmp(command,"get")) | else if(!strcmp(command,"get")) | ||
{ | { | ||
− | sscanf(buf,"%s%s",filename,filename); | + | sscanf(buf, "%s%s", filename, filename); |
− | stat(filename,&obj); | + | stat(filename, &obj); |
− | filehandle = open(filename,O_RDONLY); | + | filehandle = open(filename, O_RDONLY); |
size = obj.st_size; | size = obj.st_size; | ||
− | if(filehandle==-1) | + | if(filehandle == -1) |
size = 0; | size = 0; | ||
− | send(sock2,&size,sizeof(int),0); | + | send(sock2, &size, sizeof(int), 0); |
if(size) | if(size) | ||
− | sendfile(sock2,filehandle,NULL,size); | + | sendfile(sock2, filehandle, NULL, size); |
} | } | ||
− | else if(!strcmp(command,"put")) | + | else if(!strcmp(command, "put")) |
{ | { | ||
− | int c=0,len; | + | int c = 0, len; |
− | char * f; | + | char *f; |
− | sscanf(buf+strlen(command),"%s",filename); | + | sscanf(buf+strlen(command), "%s", filename); |
− | recv(sock2,&size,sizeof(int),0); | + | recv(sock2, &size, sizeof(int), 0); |
i = 1; | i = 1; | ||
while(1) | while(1) | ||
{ | { | ||
− | filehandle = open(filename,O_CREAT|O_EXCL|O_WRONLY,0666); | + | filehandle = open(filename, O_CREAT | O_EXCL | O_WRONLY, 0666); |
if(filehandle == -1) | if(filehandle == -1) | ||
{ | { | ||
− | sprintf(filename+strlen(filename),"%d",i); | + | sprintf(filename + strlen(filename), "%d", i); |
} | } | ||
else | else | ||
Line 88: | Line 93: | ||
} | } | ||
f = malloc(size); | f = malloc(size); | ||
− | recv(sock2,f,size,0); | + | recv(sock2, f, size, 0); |
− | c = write(filehandle,f,size); | + | c = write(filehandle, f, size); |
close(filehandle); | close(filehandle); | ||
− | send(sock2,&c,sizeof(int),0); | + | send(sock2, &c, sizeof(int), 0); |
} | } | ||
− | else if(!strcmp(command,"pwd")) | + | else if(!strcmp(command, "pwd")) |
{ | { | ||
system("pwd>temp.txt"); | system("pwd>temp.txt"); | ||
Line 102: | Line 107: | ||
buf[i-1] = '\0'; | buf[i-1] = '\0'; | ||
fclose(f); | fclose(f); | ||
− | send(sock2,buf,100,0); | + | send(sock2, buf, 100, 0); |
} | } | ||
− | else if(!strcmp(command,"cd")) | + | else if(!strcmp(command, "cd")) |
{ | { | ||
if(chdir(buf+3) == 0) | if(chdir(buf+3) == 0) | ||
Line 110: | Line 115: | ||
else | else | ||
c = 0; | c = 0; | ||
− | send(sock2,&c,sizeof(int),0); | + | send(sock2, &c, sizeof(int), 0); |
} | } | ||
− | else if(!strcmp(command,"bye")||!strcmp(command,"quit")) | + | else if(!strcmp(command, "bye") || !strcmp(command, "quit")) |
{ | { | ||
printf("FTP server quitting..\n"); | printf("FTP server quitting..\n"); | ||
i = 1; | i = 1; | ||
− | send(sock2,&i,sizeof(int),0); | + | send(sock2, &i, sizeof(int), 0); |
exit(0); | exit(0); | ||
} | } | ||
Line 124: | Line 129: | ||
return 0; | return 0; | ||
} | } | ||
+ | </syntaxhighlight> | ||
+ | <syntaxhighlight lang="C" name="ftpclient"> | ||
/*FTP Client*/ | /*FTP Client*/ | ||
Line 132: | Line 139: | ||
#include <stdio.h> | #include <stdio.h> | ||
#include <stdlib.h> | #include <stdlib.h> | ||
− | + | ||
− | #include<sys/ | + | /*for getting file size using stat()*/ |
− | #include< | + | #include<sys/stat.h> |
+ | |||
+ | /*for sendfile()*/ | ||
+ | #include<sys/sendfile.h> | ||
+ | |||
+ | /*for O_RDONLY*/ | ||
+ | #include<fcntl.h> | ||
int main(int argc,char *argv[]) | int main(int argc,char *argv[]) | ||
Line 142: | Line 155: | ||
int sock; | int sock; | ||
int choice; | int choice; | ||
− | char buf[100],command[5],filename[20],*f; | + | char buf[100], command[5], filename[20], *f; |
− | int k,size,status; | + | int k, size, status; |
int filehandle; | int filehandle; | ||
− | sock = socket(AF_INET,SOCK_STREAM,0); | + | sock = socket(AF_INET, SOCK_STREAM, 0); |
− | if(sock==-1) | + | if(sock == -1) |
{ | { | ||
printf("socket creation failed"); | printf("socket creation failed"); | ||
Line 154: | Line 167: | ||
server.sin_port = atoi(argv[1]); | server.sin_port = atoi(argv[1]); | ||
server.sin_addr.s_addr = 0; | server.sin_addr.s_addr = 0; | ||
− | k = connect(sock,(struct sockaddr*)&server,sizeof(server)); | + | k = connect(sock,(struct sockaddr*)&server, sizeof(server)); |
− | if(k==-1) | + | if(k == -1) |
{ | { | ||
printf("Connect Error"); | printf("Connect Error"); | ||
Line 164: | Line 177: | ||
{ | { | ||
printf("Enter a choice:\n1- get\n2- put\n3- pwd\n4- ls\n5- cd\n6- quit\n"); | printf("Enter a choice:\n1- get\n2- put\n3- pwd\n4- ls\n5- cd\n6- quit\n"); | ||
− | scanf("%d",&choice); | + | scanf("%d", &choice); |
switch(choice) | switch(choice) | ||
{ | { | ||
case 1: | case 1: | ||
printf("Enter filename to get: "); | printf("Enter filename to get: "); | ||
− | scanf("%s",filename); | + | scanf("%s", filename); |
− | strcpy(buf,"get "); | + | strcpy(buf, "get "); |
− | strcat(buf,filename); | + | strcat(buf, filename); |
− | send(sock,buf,100,0); | + | send(sock, buf, 100, 0); |
− | recv(sock,&size,sizeof(int),0); | + | recv(sock, &size, sizeof(int), 0); |
if(!size) | if(!size) | ||
{ | { | ||
Line 180: | Line 193: | ||
} | } | ||
f = malloc(size); | f = malloc(size); | ||
− | recv(sock,f,size,0); | + | recv(sock, f, size, 0); |
while(1) | while(1) | ||
{ | { | ||
− | filehandle = open(filename,O_CREAT|O_EXCL|O_WRONLY,0666); | + | filehandle = open(filename, O_CREAT | O_EXCL | O_WRONLY, 0666); |
if(filehandle == -1) | if(filehandle == -1) | ||
{ | { | ||
− | sprintf(filename+strlen(filename),"%d",i);//needed only if same directory is used for both server and client | + | sprintf(filename + strlen(filename), "%d", i);//needed only if same directory is used for both server and client |
} | } | ||
else break; | else break; | ||
} | } | ||
− | write(filehandle,f,size,0); | + | write(filehandle, f, size, 0); |
close(filehandle); | close(filehandle); | ||
− | strcpy(buf,"cat "); | + | strcpy(buf, "cat "); |
− | strcat(buf,filename); | + | strcat(buf, filename); |
system(buf); | system(buf); | ||
break; | break; | ||
case 2: | case 2: | ||
printf("Enter filename to put to server: "); | printf("Enter filename to put to server: "); | ||
− | scanf("%s",filename); | + | scanf("%s", filename); |
− | filehandle = open(filename,O_RDONLY); | + | filehandle = open(filename, O_RDONLY); |
if(filehandle == -1) | if(filehandle == -1) | ||
{ | { | ||
Line 205: | Line 218: | ||
break; | break; | ||
} | } | ||
− | strcpy(buf,"put "); | + | strcpy(buf, "put "); |
− | strcat(buf,filename); | + | strcat(buf, filename); |
− | send(sock,buf,100,0); | + | send(sock, buf, 100, 0); |
− | stat(filename,&obj); | + | stat(filename, &obj); |
size = obj.st_size; | size = obj.st_size; | ||
− | send(sock,&size,sizeof(int),0); | + | send(sock, &size, sizeof(int), 0); |
− | sendfile(sock,filehandle,NULL,size); | + | sendfile(sock, filehandle, NULL, size); |
− | recv(sock,&status,sizeof(int),0); | + | recv(sock, &status, sizeof(int), 0); |
if(status) | if(status) | ||
printf("File stored successfully\n"); | printf("File stored successfully\n"); | ||
Line 219: | Line 232: | ||
break; | break; | ||
case 3: | case 3: | ||
− | strcpy(buf,"pwd"); | + | strcpy(buf, "pwd"); |
− | send(sock,buf,100,0); | + | send(sock, buf, 100, 0); |
− | recv(sock,buf,100,0); | + | recv(sock, buf, 100, 0); |
− | printf("The path of the remote directory is: %s\n",buf); | + | printf("The path of the remote directory is: %s\n", buf); |
break; | break; | ||
case 4: | case 4: | ||
− | strcpy(buf,"ls"); | + | strcpy(buf, "ls"); |
− | send(sock,buf,100,0); | + | send(sock, buf, 100, 0); |
− | recv(sock,&size,sizeof(int),0); | + | recv(sock, &size, sizeof(int), 0); |
f = malloc(size); | f = malloc(size); | ||
− | recv(sock,f,size,0); | + | recv(sock, f, size, 0); |
− | filehandle = creat("temp.txt",O_WRONLY); | + | filehandle = creat("temp.txt", O_WRONLY); |
− | write(filehandle,f,size,0); | + | write(filehandle, f, size, 0); |
close(filehandle); | close(filehandle); | ||
printf("The remote directory listing is as follows:\n"); | printf("The remote directory listing is as follows:\n"); | ||
Line 237: | Line 250: | ||
break; | break; | ||
case 5: | case 5: | ||
− | strcpy(buf,"cd "); | + | strcpy(buf, "cd "); |
printf("Enter the path to change the remote directory: "); | printf("Enter the path to change the remote directory: "); | ||
− | scanf("%s",buf+3); | + | scanf("%s", buf + 3); |
− | send(sock,buf,100,0); | + | send(sock, buf, 100, 0); |
− | recv(sock,&status,sizeof(int),0); | + | recv(sock, &status, sizeof(int), 0); |
if(status) | if(status) | ||
printf("Remote directory successfully changed\n"); | printf("Remote directory successfully changed\n"); | ||
Line 248: | Line 261: | ||
break; | break; | ||
case 6: | case 6: | ||
− | strcpy(buf,"quit"); | + | strcpy(buf, "quit"); |
− | send(sock,buf,100,0); | + | send(sock, buf, 100, 0); |
− | recv(sock,&status,100,0); | + | recv(sock, &status, 100, 0); |
if(status) | if(status) | ||
{ | { |
<syntaxhighlight lang="C" name="ftpserver">
/*FTP server*/
/*for getting file size using stat()*/
/*for sendfile()*/
/*for O_RDONLY*/
int main(int argc,char *argv[]) {
struct sockaddr_in server, client; struct stat obj; int sock1, sock2; char buf[100], command[5], filename[20]; int k, i, size, len, c; int filehandle; sock1 = socket(AF_INET, SOCK_STREAM, 0); if(sock1 == -1) { printf("Socket creation failed"); exit(1); } server.sin_port = atoi(argv[1]); server.sin_addr.s_addr = 0; k = bind(sock1,(struct sockaddr*)&server,sizeof(server)); if(k == -1) { printf("Binding error"); exit(1); } k = listen(sock1,1); if(k == -1) { printf("Listen failed"); exit(1); } len = sizeof(client); sock2 = accept(sock1,(struct sockaddr*)&client, &len); i = 1; while(1) { recv(sock2, buf, 100, 0); sscanf(buf, "%s", command); if(!strcmp(command, "ls"))
{ system("ls >temps.txt"); i = 0; stat("temps.txt",&obj); size = obj.st_size; send(sock2, &size, sizeof(int),0); filehandle = open("temps.txt", O_RDONLY); sendfile(sock2,filehandle,NULL,size); }
else if(!strcmp(command,"get"))
{ sscanf(buf, "%s%s", filename, filename); stat(filename, &obj); filehandle = open(filename, O_RDONLY); size = obj.st_size; if(filehandle == -1) size = 0; send(sock2, &size, sizeof(int), 0); if(size) sendfile(sock2, filehandle, NULL, size);
}
else if(!strcmp(command, "put")) {
int c = 0, len; char *f; sscanf(buf+strlen(command), "%s", filename); recv(sock2, &size, sizeof(int), 0); i = 1; while(1) { filehandle = open(filename, O_CREAT | O_EXCL | O_WRONLY, 0666); if(filehandle == -1) { sprintf(filename + strlen(filename), "%d", i); } else break; } f = malloc(size); recv(sock2, f, size, 0); c = write(filehandle, f, size); close(filehandle); send(sock2, &c, sizeof(int), 0);
} else if(!strcmp(command, "pwd"))
{ system("pwd>temp.txt"); i = 0;
FILE*f = fopen("temp.txt","r"); while(!feof(f)) buf[i++] = fgetc(f); buf[i-1] = '\0';
fclose(f);
send(sock2, buf, 100, 0);
}
else if(!strcmp(command, "cd")) { if(chdir(buf+3) == 0)
c = 1; else c = 0;
send(sock2, &c, sizeof(int), 0); }
else if(!strcmp(command, "bye") || !strcmp(command, "quit"))
{ printf("FTP server quitting..\n"); i = 1; send(sock2, &i, sizeof(int), 0); exit(0); }
} return 0;
} </syntaxhighlight>
<syntaxhighlight lang="C" name="ftpclient"> /*FTP Client*/
/*for getting file size using stat()*/
/*for sendfile()*/
/*for O_RDONLY*/
int main(int argc,char *argv[]) {
struct sockaddr_in server; struct stat obj; int sock; int choice; char buf[100], command[5], filename[20], *f; int k, size, status; int filehandle; sock = socket(AF_INET, SOCK_STREAM, 0); if(sock == -1) { printf("socket creation failed"); exit(1); } server.sin_family = AF_INET; server.sin_port = atoi(argv[1]); server.sin_addr.s_addr = 0; k = connect(sock,(struct sockaddr*)&server, sizeof(server)); if(k == -1) { printf("Connect Error"); exit(1); } int i = 1; while(1) { printf("Enter a choice:\n1- get\n2- put\n3- pwd\n4- ls\n5- cd\n6- quit\n"); scanf("%d", &choice); switch(choice)
{ case 1: printf("Enter filename to get: "); scanf("%s", filename); strcpy(buf, "get "); strcat(buf, filename); send(sock, buf, 100, 0); recv(sock, &size, sizeof(int), 0); if(!size) { printf("No such file on the remote directory\n\n"); break; } f = malloc(size); recv(sock, f, size, 0); while(1) { filehandle = open(filename, O_CREAT | O_EXCL | O_WRONLY, 0666); if(filehandle == -1) { sprintf(filename + strlen(filename), "%d", i);//needed only if same directory is used for both server and client } else break; } write(filehandle, f, size, 0); close(filehandle); strcpy(buf, "cat "); strcat(buf, filename); system(buf); break; case 2: printf("Enter filename to put to server: ");
scanf("%s", filename);
filehandle = open(filename, O_RDONLY);
if(filehandle == -1) { printf("No such file on the local directory\n\n"); break; } strcpy(buf, "put ");
strcat(buf, filename); send(sock, buf, 100, 0); stat(filename, &obj); size = obj.st_size; send(sock, &size, sizeof(int), 0); sendfile(sock, filehandle, NULL, size); recv(sock, &status, sizeof(int), 0); if(status) printf("File stored successfully\n"); else printf("File failed to be stored to remote machine\n"); break; case 3: strcpy(buf, "pwd"); send(sock, buf, 100, 0); recv(sock, buf, 100, 0); printf("The path of the remote directory is: %s\n", buf); break; case 4: strcpy(buf, "ls");
send(sock, buf, 100, 0);
recv(sock, &size, sizeof(int), 0);
f = malloc(size); recv(sock, f, size, 0);
filehandle = creat("temp.txt", O_WRONLY); write(filehandle, f, size, 0); close(filehandle);
printf("The remote directory listing is as follows:\n");
system("cat temp.txt"); break; case 5: strcpy(buf, "cd "); printf("Enter the path to change the remote directory: "); scanf("%s", buf + 3);
send(sock, buf, 100, 0);
recv(sock, &status, sizeof(int), 0);
if(status) printf("Remote directory successfully changed\n"); else printf("Remote directory failed to change\n"); break;
case 6: strcpy(buf, "quit");
send(sock, buf, 100, 0); recv(sock, &status, 100, 0);
if(status) { printf("Server closed\nQuitting..\n"); exit(0); } printf("Server failed to close connection\n"); }
}
}
</syntaxhighlight>
<syntaxhighlight lang="C">
/*FTP server*/
/*for getting file size using stat()*/
int main(int argc,char *argv[]) {
struct sockaddr_in server,client; struct stat obj; int sock1,sock2; char buf[100],command[5],filename[20]; int k,i,size,len,c; int filehandle; sock1 = socket(AF_INET,SOCK_STREAM,0); if(sock1==-1) { printf("Socket creation failed"); exit(1); } server.sin_port = atoi(argv[1]); server.sin_addr.s_addr = 0; k = bind(sock1,(struct sockaddr*)&server,sizeof(server)); if(k==-1) { printf("Binding error"); exit(1); } k=listen(sock1,1); if(k==-1) { printf("Listen failed"); exit(1); } len = sizeof(client); sock2 = accept(sock1,(struct sockaddr*)&client,&len); i = 1; while(1) { recv(sock2,buf,100,0); sscanf(buf,"%s",command); if(!strcmp(command,"ls"))
{ system("ls >temps.txt"); i = 0; stat("temps.txt",&obj); size = obj.st_size; send(sock2,&size,sizeof(int),0); filehandle = open("temps.txt",O_RDONLY); sendfile(sock2,filehandle,NULL,size); }
else if(!strcmp(command,"get"))
{ sscanf(buf,"%s%s",filename,filename); stat(filename,&obj); filehandle = open(filename,O_RDONLY); size = obj.st_size; if(filehandle==-1) size = 0; send(sock2,&size,sizeof(int),0); if(size) sendfile(sock2,filehandle,NULL,size);
}
else if(!strcmp(command,"put")) {
int c=0,len; char * f; sscanf(buf+strlen(command),"%s",filename); recv(sock2,&size,sizeof(int),0); i = 1; while(1) { filehandle = open(filename,O_CREAT|O_EXCL|O_WRONLY,0666); if(filehandle == -1) { sprintf(filename+strlen(filename),"%d",i); } else break; } f = malloc(size); recv(sock2,f,size,0); c = write(filehandle,f,size); close(filehandle); send(sock2,&c,sizeof(int),0);
} else if(!strcmp(command,"pwd"))
{ system("pwd>temp.txt"); i = 0;
FILE*f = fopen("temp.txt","r"); while(!feof(f)) buf[i++] = fgetc(f); buf[i-1] = '\0';
fclose(f);
send(sock2,buf,100,0);
}
else if(!strcmp(command,"cd")) { if(chdir(buf+3) == 0)
c = 1; else c = 0;
send(sock2,&c,sizeof(int),0); }
else if(!strcmp(command,"bye")||!strcmp(command,"quit"))
{ printf("FTP server quitting..\n"); i = 1; send(sock2,&i,sizeof(int),0); exit(0); }
} return 0;
}
/*FTP Client*/
int main(int argc,char *argv[]) {
struct sockaddr_in server; struct stat obj; int sock; int choice; char buf[100],command[5],filename[20],*f; int k,size,status; int filehandle; sock = socket(AF_INET,SOCK_STREAM,0); if(sock==-1) { printf("socket creation failed"); exit(1); } server.sin_family = AF_INET; server.sin_port = atoi(argv[1]); server.sin_addr.s_addr = 0; k = connect(sock,(struct sockaddr*)&server,sizeof(server)); if(k==-1) { printf("Connect Error"); exit(1); } int i = 1; while(1) { printf("Enter a choice:\n1- get\n2- put\n3- pwd\n4- ls\n5- cd\n6- quit\n"); scanf("%d",&choice); switch(choice)
{ case 1: printf("Enter filename to get: "); scanf("%s",filename); strcpy(buf,"get "); strcat(buf,filename); send(sock,buf,100,0); recv(sock,&size,sizeof(int),0); if(!size) { printf("No such file on the remote directory\n\n"); break; } f = malloc(size); recv(sock,f,size,0); while(1) { filehandle = open(filename,O_CREAT|O_EXCL|O_WRONLY,0666); if(filehandle == -1) { sprintf(filename+strlen(filename),"%d",i);//needed only if same directory is used for both server and client } else break; } write(filehandle,f,size,0); close(filehandle); strcpy(buf,"cat "); strcat(buf,filename); system(buf); break; case 2: printf("Enter filename to put to server: ");
scanf("%s",filename);
filehandle = open(filename,O_RDONLY);
if(filehandle == -1) { printf("No such file on the local directory\n\n"); break; } strcpy(buf,"put ");
strcat(buf,filename); send(sock,buf,100,0); stat(filename,&obj); size = obj.st_size; send(sock,&size,sizeof(int),0); sendfile(sock,filehandle,NULL,size); recv(sock,&status,sizeof(int),0); if(status) printf("File stored successfully\n"); else printf("File failed to be stored to remote machine\n"); break; case 3: strcpy(buf,"pwd"); send(sock,buf,100,0); recv(sock,buf,100,0); printf("The path of the remote directory is: %s\n",buf); break; case 4: strcpy(buf,"ls");
send(sock,buf,100,0);
recv(sock,&size,sizeof(int),0);
f = malloc(size); recv(sock,f,size,0);
filehandle = creat("temp.txt",O_WRONLY); write(filehandle,f,size,0); close(filehandle);
printf("The remote directory listing is as follows:\n");
system("cat temp.txt"); break; case 5: strcpy(buf,"cd "); printf("Enter the path to change the remote directory: "); scanf("%s",buf+3);
send(sock,buf,100,0);
recv(sock,&status,sizeof(int),0);
if(status) printf("Remote directory successfully changed\n"); else printf("Remote directory failed to change\n"); break;
case 6: strcpy(buf,"quit");
send(sock,buf,100,0); recv(sock,&status,100,0);
if(status) { printf("Server closed\nQuitting..\n"); exit(0); } printf("Server failed to close connection\n"); }
}
}
</syntaxhighlight>