0% found this document useful (0 votes)
2K views12 pages

BCSL 56 em

1. The document provides sample code for a TCP client-server program to concatenate two strings sent from a client. 2. The TCP client program sends two strings to the server and receives the concatenated result back. 3. The TCP server program receives the two strings, concatenates them, and sends the result back to the client.

Uploaded by

AnjnaKandari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views12 pages

BCSL 56 em

1. The document provides sample code for a TCP client-server program to concatenate two strings sent from a client. 2. The TCP client program sends two strings to the server and receives the concatenated result back. 3. The TCP server program receives the two strings, concatenates them, and sends the result back to the client.

Uploaded by

AnjnaKandari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

n g

di
ea
R
e
lin ks
n o
O b o
o r -
f E
b n d
u a
H
h e
T

1
B.C.S.L.-56
Network Programming and
Administration Lab
Disclaimer/Special Note: These are just the sample of the Answers/Solutions to some of the Questions given in the
Assignments. These Sample Answers/Solutions are prepared by Private Teacher/Tutors/Authors for the help and guidance
of the student to get an idea of how he/she can answer the Questions given the Assignments. We do not claim 100%
accuracy of these sample answers as these are based on the knowledge and capability of Private Teacher/Tutor. Sample

g
answers may be seen as the Guide/Help for the reference to prepare the answers of the Questions given in the assignment.

n
As these solutions and answers are prepared by the private Teacher/Tutor so the chances of error or mistake cannot be

di
denied. Any Omission or Error is highly regretted though every care has been taken while preparing these Sample Answers/
Solutions. Please consult your own Teacher/Tutor before you prepare a Particular Answer and for up-to-date and exact

a
information, data and solution. Student should must read and refer the official study material provided by the university.

Re
Note: Answer all the questions in the assignment having 40 marks in total. 10 marks are for viva voce.
You may use illustrations and diagrams to enhance the explanations. Please go through the guidelines re-

e
garding assignments given in the Programme Guide for the format of presentation. Make suitable assump-

in ks
tion is necessary.

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

n o
The TCP client program sends two strings to the TCP server program to concatenate these two

O o
strings.

r - b
The TCP server program sends the concatenated strings to the client.

o E
Ans. TCP Client program
#include <sys/types.h>
f
b d
#include <sys/socket.h>

u a n
#include <netinet/in.h>
#include <arpa/inet.h>

H
#include <netdb.h>

e
#include <stdio.h>

h
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <strings.h>
#define MAX_MSG 100
T
#define SERVER_ADDR“127.0.01”
#define CLIENT_ADDR“127.0.01”
#define SERVER_PORT 3786
#define CLIENT_PORT 8229

main () {

22
int sd, rc, i, n;
struct sockaddr_in clientAddr, servddra;
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));

g
servAddr.sin_family = AF_INET;

n
inet_aton(SERVER_ADDR,&servAddr.sin_addr);
servAddr.sin_port = htons(SERVER_PORT);

di
a
*/

e
/***************************/

R
/* build client address structure*/
/****************************/

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

in ks
clientAddr.sin Jamily = AF_INET;

l
clientAddr.sin_addr.s_addr = INADDR_ANY;

n o
clientAddr.sin_port = htons(O);

O o
/*

r - b
bzero((char *)&clientAddr, sizeof(clientAddr));

o
clientAddr.sin Jamily = AF_INET;

f E
clientAddr.sin_addr.s_addr = inet_addr(CLIENT_ADDR);

d
clientAddr.sin_port = htons(CLIENT_PORT);

/* b
u a n
H
/******************/
/*create stream socket */

e
sd = socket(AFJNET, SOCK_STREAM, 0);

h
printf(“successfully created stream socket \n”);

T
/*******************************/
/* bind local port number */

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


printf(“bound local port successfully\n”);
/**********************/
/* connect to server */
/**********************/
connected, (struct sockaddr *) &servAddr, sizeof(servAddr));
printf(“connected to server successfully\n”);

33
/* 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);

g
}while(strcmp(line, “quit”));

in
printf(“closing connection with the server\n”);
a d
e
close(sd);

R
}

e
in ks
TCP Server Program

l
#include<sys/types.h>

n o
#include<sys/socket.h>
#include <netinet/in.h>

O o
#include <arpa/inet.h>

r - b
#include<netdb.h>

o
#include<stdio.h>

f E
d
#include<unistd.h>

b
#include<st rings.h>
#include<string.h>
u a n
H
#define MAX_MSG 100
#define SERVER_ADDR “127.0.0.1”

e
#define SERVER_PORT 3786

main () {

T
int sd, newSd, cliLen, n;
h
struct sockaddrjn cliAddr, servAddr;
charline[MAX_MSG],linel[MAX_MSG];

/***********************************/
/* build server address structure */

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

4
servAddr.sin_family = AFINET;
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);

n g
printf(“successfully created stream socket \n”);

di
a
/***************************/

e
/* bind local port number */

R
/***************************/

e
bind(sd, (struct sockaddr *) &servAddr, sizeof(servAddr));

in ks
printf(“bound local port successfully\n”);

l
n o
O o
/***************************/

r - b
/* specify number of concurrent */

o
/* clients to listen for */

f E
d
listen(sd,5);

while(l) { b
u a n
H
printf(“waiting for client connection on port TCP %u\n”,SERVER_PORT);

/************************/

h e
T
/* wait for client connection*/
/*************************/

cliLen = sizeof(cliAddr);
newSd = accepted, (struct sockaddr *) &cliAddr, &clil_en);

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 */
/***************************/

5
do{
memset(line/OxO,MAX_MSG);

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


strcpy(lineljine);
n=recv(newSd, line, MAX_MSG, 0);
strcat(linel,line);

printf(“received from host [IP %s JCP port %d]: %s\n”,


inet_ntoa(cliAddr.sin_addr), ntohs(cliAddr.sin_port), linel);
send(newSd, linel, strlen(linel) + 1, 0);

}while(abs(strcmp(line, “quit”)));

n g
i
/**************************/

d
/* close client connection*/

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

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

e
close(newSd);

in ks
}

l
}

n o
Q. 2. (a) Run the following Linux commands on your machine and show the output:

o
1. df–h
2. du
3. ping O
r - b
o E
4. more

f
r
5. tail-f

b d
Ans.

u a n
1. df-h

Output:
H
h e
T

6
2. du :-

du /home/mandeep/test

Output:

44 /home/mandeep/test/data
2012 /home/mandeep/test/system design
24 /home/mandeep/test/table/sample_table/tree
28 /home/mandeep/test/table/sample_table
32 /home/mandeep/test/table
100104 /home/mandeep/test

3. ping :-

Output:

n g
di
ea
R
e
lin ks
n o
O b o
o r -
f E
b n d
u a
H
4. more :-

more [filename]

h e
T
Output:

7
n g
di
ea
R
e
lin ks
n o
O b o
o r -
f E
b n d
u a
H
5. tail-f
Output :

h e
T

8
(b) Write and run commands in Linux for the following tasks:
1. Add new users
2. Display the list of users who belong to different groups
3. Display the list of users who are currently logged on.
4. List all the process which are currently running in the systems.
5. Show all the active internet connection in the system.
Ans.

1. Add new user

How to add a User to Linux

Follow thest steps to add an existing user to a group in Linux:

g
1. Log in as root

2.
in
Use the command useradd “name of the user” (for example, user add roman)

a d
Re
e
lin ks
n o
O b o
o r -
f E
3.
b d
Use surplus the name of the user you just added to log on.

n
u a
4. “Exit” will log you out.

H
Q. 2 Display the list of users who belong to different groups

e
Ans. Use the group command as follows:

h
S group ‘group-name-here’ /etc/group

T
S group ‘ftponly’ /etc/group
S group -i —colour ‘ftponly’ /etc/group

Sample outputs: ,

ftponly:X:lOOlTaj/vivek/archana/sai,sayali

To get just a list of all members of a group called ftponly, type:

“awk -F’:’ ‘/ftponly/{print $4}’ /etc/group

9
Q. 3. Display the list of users who are currently logged on.
Ans. who command - Shows information about users who are currently logged on.

who command works on all Unix like operating systems:


# who
Outputs:

root pts/0 2013-03-12 15:10 (10.1.3.177)

Pass the -a option to who command:


#who -a
outputs:

g
system boot 2013-03-02 04:10

run-level 3 2013-03-02 04:10


in
LOGIN /dev/ttyS1 2013-03-02 04:11

a d
795 id=v/tt

LOGIN tty2 2013-03-02 04:11

Re
7953 id=2

e
LOGIN tty1 2013-03-02 04:11 7950 id=1

LOGIN tty3 2013-03-02 04:11

lin ks
7955 id=3

n o
o
LOGIN tty4 2013-03-02 04:11 7957 id=4

LOGIN tty5 2013-03-02 04:11 O


r - b7959 id=5

LOGIN tty6 2013-03-02 04:11


fo E 7961 id=6

b n d
u a
root +pts/0 2013-03-12 15:10 7451 (10.1.3.177)

pts/1
H
2013-03-08 12:29 23510 id=ts/1 tern=0

h e
Q. 4. List all the processes which are currently running in the systems.

T
Ans. Type the following ps command to display all running process:
# ps -aux | less
OR
# ps aux | less
Where,
- A : Select all processes
- u : Select all processes on a terminal, including those of other users
- x : Select processes without controlling ttys

10
n g
di
ea
R
e
lin ks
n o
O b o
o r -
f E
b n d
u a
H
h e
T

11
Q. 5. Show all the active internet connection in the system.
Ans. It may be necessary to display what Internet connections are active on your Linux box. For example, seeing if the
Apache service is actively running, and if running what network ports it’s listening to can be done with the following command.

Command :- netstat -natp


If you have root privileges running the above command gives an output similar to the following example.
Output :-
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name

tcp 0 0 127.0.0.1:2208 0.0.0.0:* LISTEN 16271/hpiod

tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 4917/mysqld

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8030/apache2

tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 15978/cupsd

n g
tcp 0 0 127.0.0.1:2207 0.0.0.0:* LISTEN 16276/python
di
tcp6 0 0 :::5900 :::* LISTEN 5752/vino-server|

ea
tcp600 :::22 :::* LISTEN 5062/sshd
R
e
in ks
tcp6 0 148 ::ffff:192.168.2.102:22 ::ffff: 192.168.2.1:3027 ESTABLISHED7534/sshd: hope [

l
n o
tcp60797 ::ffff:192.168.2.1:5900 ::ffff: 192.168.2.1:2592 ESTABLISHED5752/vino-server.

O b o
o r -
f E
b n d
u a
H
h e
T

12

You might also like