guruignouCourse Code : BCSL-056 Course Title : Network Programming and Administration (Lab)...

14
https://guruignou.com https://guruignou.com BCA Solved Assignment 2019-20 Course Code : BCSL-056 Course Title : Network Programming and Administration (Lab) Assignment Number: BCA(5)/056/Assignment/2019-20 Q1. Write and execute a TCP client and a server program in C-language to perform the following tasks: - The TCP client program sends two strings to the TzP server program to concatenate these two strings. - The TCP server program sends the concatenated strings to the client. Ans: Server.C #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #include <stdio.h> #include <unistd.h> #include <string.h> #include <strings.h> #define MAX_MSG 100 #define SERVER_ADDR "127.0.0.1" #define CLIENT_ADDR "127.0.0.1" https://guruign ou.com

Transcript of guruignouCourse Code : BCSL-056 Course Title : Network Programming and Administration (Lab)...

Page 1: guruignouCourse Code : BCSL-056 Course Title : Network Programming and Administration (Lab) Assignment Number: BCA(5)/056/Assignment/2019 -20 Q1. Write and execute a TCP client and

https://guruignou.com

https://guruignou.com

BCA Solved Assignment 2019-20

Course Code : BCSL-056

Course Title : Network Programming and Administration (Lab)

Assignment Number: BCA(5)/056/Assignment/2019-20

Q1. Write and execute a TCP client and a server program in C-language to perform the following tasks:

- The TCP client program sends two strings to the TzP server program to concatenate these two strings. - The TCP server program sends the concatenated strings to the client.

Ans: Server.C

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <netdb.h>

#include <stdio.h>

#include <unistd.h>

#include <string.h>

#include <strings.h>

#define MAX_MSG 100

#define SERVER_ADDR "127.0.0.1"

#define CLIENT_ADDR "127.0.0.1"

https://guruignou.com

Page 2: guruignouCourse Code : BCSL-056 Course Title : Network Programming and Administration (Lab) Assignment Number: BCA(5)/056/Assignment/2019 -20 Q1. Write and execute a TCP client and

https://guruignou.com

#define SERVER_PORT 3786

#define CLIENT_PORT 8229

main ()

{

int sd, rc, i,n;

struct sockaddr_in clientAddr, servAddr;

char line[MAX_MSG];

/* build server address structure */

bzero((char *)&servAddr, sizeof(servAddr));

servAddr.sin_family = AF_INET;

servAddr.sin_addr.s_addr = inet_addr(SERVER_ADDR);

servAddr.sin_port = htons(SERVER_PORT);

bzero((char *)&servAddr, sizeof(servAddr));

servAddr.sin_family = AF_INET;

inet_aton(SERVER_ADDR, &servAddr.sin_addr);

servAddr.sin_port = htons(SERVER_PORT);

/* build client address structure */

bzero((char *)&clientAddr,

sizeof(clientAddr)); clientAddr.sin_family =

AF_INET; clientAddr.sin_addr.s_addr =

INADDR_ANY; clientAddr.sin_port = htons(0);

bzero((char *)&clientAddr, sizeof(clientAddr));

https://guruignou.com

Page 3: guruignouCourse Code : BCSL-056 Course Title : Network Programming and Administration (Lab) Assignment Number: BCA(5)/056/Assignment/2019 -20 Q1. Write and execute a TCP client and

https://guruignou.com

clientAddr.sin_family = AF_INET;

clientAddr.sin_addr.s_addr = inet_addr(CLIENT_ADDR);

clientAddr.sin_port = htons(CLIENT_PORT);

/* create stream socket */

sd = socket(AF_INET, SOCK_STREAM, 0);

printf("successfully created stream socket \n");

/* bind local port number */

bind(sd, (struct sockaddr *) &clientAddr, sizeof(clientAddr));

printf("bound local port successfully\n");

/* connect to server */

connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr));

printf("connected to server successfully\n");

/* send data to server */

Do

{

printf("Enter string to send to server : ");

scanf("%s", line);

send(sd, line, strlen(line) + 1, 0);

printf("data sent (%s)\n", line);

printf("Enter another string to send to server :

"); scanf("%s", line);

send(sd, line, strlen(line) + 1, 0);

printf("data sent (%s)\n", line);

n=recv(sd, line, MAX_MSG, 0);

printf("received from server %s\n", line);

}

https://guruignou.com

Page 4: guruignouCourse Code : BCSL-056 Course Title : Network Programming and Administration (Lab) Assignment Number: BCA(5)/056/Assignment/2019 -20 Q1. Write and execute a TCP client and

https://guruignou.com

while(strcmp(line, "quit"));

printf("closing connection with the server\n");

close(sd);

}

Server.C

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <netdb.h>

#include <stdio.h>

#include <unistd.h>

#include <strings.h>

#include <string.h>

#define MAX_MSG 100

#define SERVER_ADDR "127.0.0.1"

#define SERVER_PORT 3786

main ( )

{

int sd, newSd, cliLen, n;

struct sockaddr_in cliAddr, servAddr;

char line[MAX_MSG],line1[MAX_MSG];

/* build server address structure */

bzero((char *)&servAddr, sizeof(servAddr));

servAddr.sin_family = AF_INET;

https://guruignou.com

Page 5: guruignouCourse Code : BCSL-056 Course Title : Network Programming and Administration (Lab) Assignment Number: BCA(5)/056/Assignment/2019 -20 Q1. Write and execute a TCP client and

https://guruignou.com

servAddr.sin_addr.s_addr = inet_addr(SERVER_ADDR);

servAddr.sin_port = htons(SERVER_PORT);

bzero((char *)&servAddr, sizeof(servAddr));

servAddr.sin_family = AF_INET;

inet_aton(SERVER_ADDR, &servAddr.sin_addr);

servAddr.sin_port = htons(SERVER_PORT);

/* create stream socket */

sd = socket(AF_INET, SOCK_STREAM, 0);

printf("successfully created stream socket

\n"); /* bind local port number */

bind(sd, (struct sockaddr *) &servAddr,

sizeof(servAddr)); printf("bound local port

successfully\n"); /* specify number of concurrent */

/* clients to listen for */

listen(sd,5);

while(1)

{

printf("waiting for client connection on port TCP %u\n",SERVER_PORT);

/* wait for client connection*/

cliLen = sizeof(cliAddr);

newSd = accept(sd, (struct sockaddr *) &cliAddr, &cliLen);

printf("received connection from host [IP %s ,TCP port

%d]\n", inet_ntoa(cliAddr.sin_addr), ntohs(cliAddr.sin_port));

/* wait for data from client */

https://guruignou.com

Page 6: guruignouCourse Code : BCSL-056 Course Title : Network Programming and Administration (Lab) Assignment Number: BCA(5)/056/Assignment/2019 -20 Q1. Write and execute a TCP client and

https://guruignou.com

do

{

memset(line,0x0,MAX_MSG);

n=recv(newSd, line, MAX_MSG, 0);

strcpy(line1,line);

n=recv(newSd, line, MAX_MSG, 0);

strcat(line1,line);

printf("received from host [IP %s ,TCP port %d] : %s\n",

inet_ntoa(cliAddr.sin_addr), ntohs(cliAddr.sin_port), line1);

send(newSd, line1, strlen(line1) + 1, 0); }

while(abs(strcmp(line, "quit")));

/* close client connection*/

printf("closing connection with host [IP %s ,TCP port %d]\n",

inet_ntoa(cliAddr.sin_addr), ntohs(cliAddr.sin_port));

close(newSd);

}

}

Q2. (a) Run the following Linux commands on your machine and show the output:

https://guruignou.com

Page 7: guruignouCourse Code : BCSL-056 Course Title : Network Programming and Administration (Lab) Assignment Number: BCA(5)/056/Assignment/2019 -20 Q1. Write and execute a TCP client and

https://guruignou.com

Ans: (1)df -h

df -h option to display size in power of 1024

Output:

(2) du- du command, short for disk usage, is used to estimate file space usage. The du command can be used to track the files and directories which are consuming excessive amount of space on hard disk drive.

Syntax:

du [OPTION]... [FILE]...

Output:

https://guruignou.com

Page 8: guruignouCourse Code : BCSL-056 Course Title : Network Programming and Administration (Lab) Assignment Number: BCA(5)/056/Assignment/2019 -20 Q1. Write and execute a TCP client and

https://guruignou.com

(3) ping- PING (Packet Internet Groper) command is used to check the network connectivity between host and server/host. This command takes as input the IP address or the URL and sends a data packet to the specified address with the message “PING” and get a response from the server/host this time is recorded which is called latency. Fast ping low latency means faster connection.

https://guruignou.com

Page 9: guruignouCourse Code : BCSL-056 Course Title : Network Programming and Administration (Lab) Assignment Number: BCA(5)/056/Assignment/2019 -20 Q1. Write and execute a TCP client and

https://guruignou.com

(4)more- more command is used to view the text files in the command prompt, displaying one screen at a time in case the file is large (For example log files). The more command also allows the user do scroll up and down through the page. The syntax along with options and command is as follows. Another application of more is to use it with some other command after a pipe. When the output is large, we can use more command to see output one by one.

(5)tail –f- This option is mainly used by system administration to monitor the growth

of the log files written by many Unix program as they are running. This option shows

the last ten lines of a file and will update when new lines are added. As new lines are

written to the log, the console will update with the new lines. The prompt doesn’t

return even after work is over so, we have to use the interrupt key to abort this

command. In general, the applications writes error messages to log files. You can use

the -f option to check for the error messages as and when they appear in the log file.

https://guruignou.com

Page 10: guruignouCourse Code : BCSL-056 Course Title : Network Programming and Administration (Lab) Assignment Number: BCA(5)/056/Assignment/2019 -20 Q1. Write and execute a TCP client and

https://guruignou.com

Q2) (b) Write and run commands in Linux for the following tasks:

Ans: -Add new User

https://guruignou.com

Page 11: guruignouCourse Code : BCSL-056 Course Title : Network Programming and Administration (Lab) Assignment Number: BCA(5)/056/Assignment/2019 -20 Q1. Write and execute a TCP client and

https://guruignou.com

-Display the list of users who belong to different groups

-Display the list of users who are currently logged on.

https://guruignou.com

Page 12: guruignouCourse Code : BCSL-056 Course Title : Network Programming and Administration (Lab) Assignment Number: BCA(5)/056/Assignment/2019 -20 Q1. Write and execute a TCP client and

https://guruignou.com

-List all the processes which are currently running in the systems.

https://guruignou.com

Page 13: guruignouCourse Code : BCSL-056 Course Title : Network Programming and Administration (Lab) Assignment Number: BCA(5)/056/Assignment/2019 -20 Q1. Write and execute a TCP client and

https://guruignou.com

-Show all the active internet connection in the system.

Disclaimer/Note: These are just the sample of the answers/solutions to some of the questions given in the

Assignments. These sample answers are submitted by students / private tutors. These sample answers may be seen as the Guide/Help for the reference to prepare the

answers of the questions given the assignment. Student should read and refer the official study material provided by the university.

https://guruignou.com/ All the Latest Assignment Visit The Link