aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--q7/client.c49
-rw-r--r--q7/server.c62
-rw-r--r--readme.txt12
3 files changed, 121 insertions, 2 deletions
diff --git a/q7/client.c b/q7/client.c
new file mode 100644
index 0000000..32d89f3
--- /dev/null
+++ b/q7/client.c
@@ -0,0 +1,49 @@
1#include <stdio.h>
2#include <stdlib.h>
3#include <sys/types.h>
4#include <sys/socket.h>
5#include <netinet/in.h>
6#include <unistd.h>
7#include <string.h>
8#include <arpa/inet.h>
9#include <string.h>
10
11int main(int argc, char* argv[]) {
12 const char *server_name = "127.0.0.1";
13 int sockfd, connfd;
14 struct sockaddr_in servaddr, cli;
15
16 sockfd = socket(AF_INET, SOCK_STREAM, 0);
17 servaddr.sin_family = AF_INET;
18 inet_pton(AF_INET, server_name, &servaddr.sin_addr);
19
20 int port = atoi(argv[1]);
21 servaddr.sin_port = htons(port);
22
23 if((connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) == 0) {
24 printf("Connected to server!\n");
25 fflush(stdout);
26 } else {
27 printf("Invalid or busy server address\n");
28 fflush(stdout);
29 exit(0);
30 };
31
32 int cont = 0;
33 char *buf = malloc(1024);
34 char recevied[1024];
35 printf("Client input: ");
36 fgets(buf, 100, stdin);
37 while(strcmp(buf, "quit\n") != 0) {
38 send(sockfd, buf, 100, 0);
39 if ((cont = recv(sockfd, recevied, 1024, 0)) > 0) {
40 printf("Server response: %s\n", recevied);
41 fflush(stdout);
42 memset(recevied, 0, sizeof(recevied));
43 }
44 printf("Client input: ");
45 fgets(buf, 100, stdin);
46 };
47
48 close(sockfd);
49}
diff --git a/q7/server.c b/q7/server.c
new file mode 100644
index 0000000..12df5bb
--- /dev/null
+++ b/q7/server.c
@@ -0,0 +1,62 @@
1#include <stdio.h>
2#include <stdlib.h>
3#include <sys/types.h>
4#include <sys/socket.h>
5#include <netinet/in.h>
6#include <unistd.h>
7#include <string.h>
8#include <arpa/inet.h>
9#include <wait.h>
10
11int main(int argc, char *argv[]) {
12 int sockfd, connfd;
13 struct sockaddr_in servaddr, cli;
14
15 sockfd = socket(AF_INET, SOCK_STREAM, 0);
16 servaddr.sin_family = AF_INET;
17 servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
18
19 int port = atoi(argv[1]);
20 servaddr.sin_port = htons(port);
21
22 bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
23
24 listen(sockfd, 128);
25 printf("Server started at %s...\n", inet_ntoa(servaddr.sin_addr));
26 fflush(stdout);
27
28 while(1) {
29 socklen_t len = sizeof(cli);
30 connfd = accept(sockfd, (struct sockaddr *)&cli, &len);
31 int i = getpeername(connfd, (struct sockaddr*)&cli, &len);
32
33 printf("Client connected at port %d\n", htons(cli.sin_port));
34 int n;
35 char buf[100];
36 int pid;
37
38 if((pid=fork())==0){
39 close(sockfd);
40 while((n = recv(connfd, buf, 100, 0)) > 0) {
41 printf("Client message: %s\n", buf);
42 int pipes[2];
43 pipe(&pipes[0]);
44 if ((pid=fork()) == 0) {
45 dup2(pipes[1], STDOUT_FILENO);
46 close(pipes[0]);
47 execl("/bin/sh", "sh", "-c", buf, (char *) NULL);
48 } else {
49 close(pipes[1]);
50 char cmd_output[1024];
51 int cmd_size = read(pipes[0], cmd_output, sizeof(cmd_output));
52 send(connfd, cmd_output, cmd_size, 0);
53 wait(NULL);
54 }
55 fflush(stdout);
56 };
57 exit(0);
58 }
59 close(connfd);
60 }
61 return 0;
62}
diff --git a/readme.txt b/readme.txt
index 66b08b0..6aabaaf 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,6 +1,5 @@
118CS54 - network programming and security 118CS54 - network programming and security
2------------------------------------------------------------ 2------------------------------------------------------------
3
4Q1: Write a networking program consisting of a client and 3Q1: Write a networking program consisting of a client and
5 server components. The client must request for a file by 4 server components. The client must request for a file by
6 providing a file name, and the server must respond with the 5 providing a file name, and the server must respond with the
@@ -10,8 +9,8 @@ Q1: Write a networking program consisting of a client and
10 9
11A1: https://u.peppe.rs/qt.png 10A1: https://u.peppe.rs/qt.png
12 11
13------------------------------------------------------------
14 12
13------------------------------------------------------------
15Q6: Write a networking program to demonstrate the following: 14Q6: Write a networking program to demonstrate the following:
16 a) Concurrent TCP server 15 a) Concurrent TCP server
17 b) Iterative TCP server 16 b) Iterative TCP server
@@ -21,5 +20,14 @@ A6: a) https://u.peppe.rs/E3.png
21 b) https://u.peppe.rs/VA.png 20 b) https://u.peppe.rs/VA.png
22 c) https://u.peppe.rs/ei.png 21 c) https://u.peppe.rs/ei.png
23 22
23
24------------------------------------------------------------
25Q7: Write a networking program that recieves a shell command
26 from a client and returns the output of the command to the
27 client.
28
29A7: https://u.peppe.rs/QD.png
30
31
24------------------------------------------------------------ 32------------------------------------------------------------
25mirror of git.peppe.rs/university/nps 33mirror of git.peppe.rs/university/nps