Skip to content

Commit 667ed68

Browse files
committed
CN programs
0 parents  commit 667ed68

24 files changed

+572
-0
lines changed

.vscode/settings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"files.associations": {
3+
"fcntl.h": "c",
4+
"stat.h": "c",
5+
"inet.h": "c"
6+
}
7+
}

.vscode/tasks.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"tasks": [
3+
{
4+
"type": "cppbuild",
5+
"label": "C/C++: gcc-9 build active file",
6+
"command": "/usr/bin/gcc-9",
7+
"args": [
8+
"-fdiagnostics-color=always",
9+
"-g",
10+
"${file}",
11+
"-o",
12+
"${fileDirname}/${fileBasenameNoExtension}"
13+
],
14+
"options": {
15+
"cwd": "${fileDirname}"
16+
},
17+
"problemMatcher": [
18+
"$gcc"
19+
],
20+
"group": {
21+
"kind": "build",
22+
"isDefault": true
23+
},
24+
"detail": "Task generated by Debugger."
25+
}
26+
],
27+
"version": "2.0.0"
28+
}

FTP/client/a.out

17 KB
Binary file not shown.

FTP/client/ftpclient.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <stdlib.h>
4+
#include <sys/stat.h>
5+
#include <sys/types.h>
6+
#include <sys/socket.h>
7+
#include <netinet/in.h>
8+
#include <arpa/inet.h>
9+
#include <fcntl.h>
10+
#include <sys/sendfile.h>
11+
int main(int argv, char *argc[])
12+
{
13+
struct sockaddr_in server;
14+
int bind_test, lfd, choice, size, filehandle, status;
15+
char buf[100] = "", sBuf[100] = "", filename[20], *f;
16+
lfd = socket(AF_INET, SOCK_STREAM, 0);
17+
if (lfd == -1)
18+
{
19+
printf("socket creation failed");
20+
exit(1);
21+
}
22+
server.sin_family = AF_INET;
23+
server.sin_port = atoi(argc[1]);
24+
server.sin_addr.s_addr = inet_addr("127.0.0.1");
25+
printf("\nClient ready....");
26+
bind_test = connect(lfd, (struct sockaddr *)&server, sizeof server);
27+
if (bind_test == -1)
28+
{
29+
printf("Connect Error");
30+
exit(1);
31+
}
32+
int i = 1;
33+
while (1)
34+
{
35+
printf("Enter a choice:\n1- get\n2- quit\n");
36+
scanf("%d", &choice);
37+
switch (choice)
38+
{
39+
case 1:
40+
printf("Enter filename to get: ");
41+
scanf("%s", filename);
42+
strcpy(buf, "get");
43+
strcat(buf, filename);
44+
send(lfd, buf, 100, 0);
45+
recv(lfd, &size, sizeof(int), 0);
46+
if (!size)
47+
{
48+
printf("No such file on the remote directory\n\n");
49+
break;
50+
}
51+
f = malloc(size);
52+
recv(lfd, f, size, 0);
53+
while (1)
54+
{
55+
filehandle = open(filename, O_CREAT | O_EXCL | O_WRONLY, 0666);
56+
if (filehandle == -1)
57+
{
58+
sprintf(filename + strlen(filename), "%d", i);
59+
// needed only if same directory is used for both server and client
60+
}
61+
else
62+
break;
63+
}
64+
write(filehandle, f, size, 0);
65+
close(filehandle);
66+
strcpy(buf, "cat ");
67+
strcat(buf, filename);
68+
system(buf);
69+
break;
70+
case 2:
71+
strcpy(buf, "quit");
72+
send(lfd, buf, 100, 0);
73+
recv(lfd, &status, 100, 0);
74+
if (status)
75+
{
76+
printf("Server closed\nQuitting..\n");
77+
78+
close(lfd);
79+
exit(0);
80+
}
81+
printf("Server failed to close connection\n");
82+
}
83+
}
84+
}

FTP/client/sample.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sample

FTP/client/sample.txt1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sample

FTP/ftpserver.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <sys/stat.h>
4+
#include <sys/types.h>
5+
#include <sys/socket.h>
6+
#include <netinet/in.h>
7+
#include <arpa/inet.h>
8+
#include <sys/sendfile.h>
9+
#include <stdlib.h>
10+
/*for O_RDONLY*/
11+
#include <fcntl.h>
12+
int main()
13+
{
14+
struct sockaddr_in client, server;
15+
struct stat obj;
16+
int lfd, n, confd, k, size, filehandle;
17+
char rbuf[100] = "", sBuf[100] = "", filename[20], command[5];
18+
lfd = socket(AF_INET, SOCK_STREAM, 0);
19+
20+
server.sin_family = AF_INET;
21+
server.sin_port = 3500;
22+
// server.sin_addr.s_addr=inet_addr("127.0.0.1");
23+
server.sin_addr.s_addr = inet_addr("127.0.0.1");
24+
25+
k = bind(lfd, (struct sockaddr *)&server, sizeof server);
26+
if (k == -1)
27+
{
28+
printf("Binding error");
29+
exit(1);
30+
}
31+
k = listen(lfd, 1);
32+
if (k == -1)
33+
{
34+
printf("Listen failed");
35+
exit(1);
36+
}
37+
38+
printf("\nServer ready,waiting for client....");
39+
n = sizeof client;
40+
confd = accept(lfd, (struct sockaddr *)&client, &n);
41+
42+
int i = 1;
43+
while (1)
44+
{
45+
recv(confd, rbuf, sizeof rbuf, 0);
46+
sscanf(rbuf, "%s", command);
47+
if (!strcmp(command, "get"))
48+
{
49+
sscanf(rbuf, "%s%s", filename, filename);
50+
stat(filename, &obj);
51+
filehandle = open(filename, O_RDONLY);
52+
size = obj.st_size;
53+
if (filehandle == -1)
54+
size = 0;
55+
send(confd, &size, sizeof(int), 0);
56+
if (size)
57+
sendfile(confd, filehandle, NULL, size);
58+
}
59+
else if (!strcmp(command, "bye") || !strcmp(command, "quit"))
60+
{
61+
printf("FTP server quitting..\n");
62+
i = 1;
63+
send(confd, &i, sizeof(int), 0);
64+
close(confd);
65+
close(lfd);
66+
exit(0);
67+
}
68+
}
69+
return 0;
70+
}

FTP/sample.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sample

FTP/server

17 KB
Binary file not shown.

FTP/server.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <sys/types.h>
4+
#include <sys/socket.h>
5+
#include <netinet/in.h>
6+
#include <arpa/inet.h>
7+
#include <sys/sendfile.h>
8+
#include <fcntl.h>
9+
10+
void main()
11+
{
12+
struct sockaddr_in client, server;
13+
struct stat obj;
14+
int lfd, n, confd, k, size, filehandle;
15+
char rbuf[100] = "", sBuf[100] = "", filename[20], command[5];
16+
lfd = socket(AF_INET, SOCK_STREAM, 0);
17+
server.sin_family = AF_INET;
18+
server.sin_port = 3500;
19+
// server.sin_addr.s_addr=inet_addr("127.0.0.1");
20+
server.sin_addr.s_addr = inet_addr("127.0.0.1");
21+
k = bind(lfd, (struct sockaddr *)&server, sizeof server);
22+
k = listen(lfd, 1);
23+
printf("\nServer ready,waiting for client....");
24+
n = sizeof client;
25+
confd = accept(lfd, (struct sockaddr *)&client, &n);
26+
int i = 1;
27+
while (1)
28+
{
29+
recv(confd, rbuf, sizeof rbuf, 0);
30+
sscanf(rbuf, "%s", command);
31+
if (!strcmp(command, "get"))
32+
{
33+
sscanf(rbuf, "%s%s", filename, filename);
34+
stat(filename, &obj);
35+
filehandle = open(filename, O_RDONLY);
36+
size = obj.st_size;
37+
if (filehandle == -1)
38+
size = 0;
39+
send(confd, &size, sizeof(int), 0);
40+
if (size)
41+
sendfile(confd, filehandle, NULL, size);
42+
}
43+
else if (!strcmp(command, "bye") || !strcmp(command, "quit"))
44+
{
45+
printf("FTP server quitting..\n");
46+
i = 1;
47+
send(confd, &i, sizeof(int), 0);
48+
close(confd);
49+
close(lfd);
50+
exit(0);
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)