0 ratings0% found this document useful (0 votes) 209 views15 pagesBCSL-056 Network Programming and Administration Lab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
gesfs Tony Stuny Henpee
www ignousite.com Nene Sunt Poona
Course Code : BCSL-056
Course Title : Network Programming and Administration Lab
Assignment Number : BCA(5)/L-056/Assignment/2022-23
Maximum Marks : 50
Weightage : 25% www.ignousite.com
Last date of Submission : 31st October, 2022 (For July, Sessivs
115th April. 2023 (For January. SessiDN a
Ql. 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 TCP server program to concatenate these two strings.
- The TCP server program sends the concatenated strings to the client.
‘Ans. TCP client program
include enetdb.h>
include estdio.h>
include estdlib.ho
include estringh>
dicluute
4idefine MAX 80
lHdefine PORT 8080
Hidefine SA struct sockaddr
void functint sockfd)
{
char buff{MAXl:
int
for (:)f
brerolbuff, sizeof( buf);
printf("F tor the string -")
n=0;
while ((bufffr+t] = getchar()) = "\n')
\write(sockfd, buff, sizeoflbutf);
brerolbutff, sizeof{buffl);
read{sockfa, buff, sizeof(butf));
printf("From Server :%", buff);
if (strncmplbuff, "exit", 4)} == 0) {
printf("Client Exit..\n");
break;
Int main)
(
Ignou Study Helper-Sunil Poonia Page 1}
‘Output:
nousite.com
int sockd, conntas
struct sockaddr_in servaddr, cli;
J socket create and verification
sockfd = socket(AF_INET, SOCK_STREAM, 0};
if (sockfd == -1) {
printfi"socket creation failed...\n"): 4p S™*
exit ‘eh
hes
oe
printf("Socket successfully created..\n");
bzerol&servaddr, sizeof(servaddr));
1/ assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr’
servaddr.sin_port = htons(PORT);
{connect the client socket to server socket
if (Connect(socktd, (SA*}&servaddr, sizeot(servaddr)
printf("connection with the server failed...\n');
exit(O);
on
else
printf("connected to the server..\n"}
{/ function for chat
tune(sockta);
// close the socket
close(sockfd);
Sarees ere
ges Touou Stony Heese
Nest? SUMIL POONA
Ignou Study Helper-Sunil Poonia
Page 2aww ignousite.com
‘TCP server program
Winclude
include
include
Winclude
include estring.h>
include
include
fidefine MAX 80
fidefine PORT 8080
lidefine SA struct sockaddr
// Function designed for chat between client and server.
aaa ‘et
{finfinite loop for chat
for (i)
‘a 25 fs Towa Srypy Heupee
Ey SUNIL POONIA
}
brerolbuff, MAX};
// read the message from client and copy it in buffer
read{connfd, buff, sizeof(buf))
// print butter which contains the dlient contents
printf("From client: %s\t To client ", buff);
brero{buff, MAX);
net
Ui copy server message in the buffer
while ((buffin+t] = getchar()) I= "\n')
// and send that buffer to client
write(connfd, buff, sizeof(buff)};
//if msg contains "Exit" then server exit and chat ended
iF (stencmp("exit", buff, 4) == 0) {
printf("Server Exit..\n");
break;
1/ river function
Ignou Study Helper-Sunil Poonia Page 3waww ignousite.com
int main()
(
int sockfd, conntd, len:
struct sockaddr_in servaddr, cli
[/ socket create and verification
sockfd = socket(AF_INET. SOCK STREAM. 0}:
if (sockfe == -1){
printf("socket creation falled..\n");
tO);
, exit fen ft
on EY
else
printfi"Socket successfully created. oi
bzero(&servaddr, sizeof(servadar));
Jf assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonI(INADDR_ANY);
servaddr.sin_port = htons(PORT);
{Binding newly created socket to given IP and verification
if ((bind(sockfd, (SA*)&servaddr, sizeof{servadd))) = 0) {
printf("socket bind falled...\n");
exit(O);
else
printf("Socket successfully binded..\n");
// Now setver is ready to listen and verification
if ({isten(sockt, 5)) = 0) ¢
printf["Listen failed...\n");
exit(0};
t
alse
printf("Server istening.An");,
len = sizeof{cl);
// Necopt the data packet from client and varifeation
connfd = accept|sockid, (SA*)&cti, Rien);
if (connid <0) {
printf("server accept failed...\n");
exit(0};
t
else
Zee Tout Stevens
eS SUNIL POONIA
Ignou Study Helper-Sunil Poonia
Page 4Heys Touoy Stuvy Here
www ignousite.com Nam? SUMIL POONA
printf("server accept the client...\n*);
// Function for chatting between client and server
fune{conntd);
/{ After chatting close the socket
closelsockfa):
} sve
_ (eh
Sen ree
Seats)
Sees
Gene ne
ae
eens
caer
Q2. Configure the DHCP server on the Linux operating system. Write all the steps involved in configuration. Sort each column
of the table and show the result.
‘Ans. Dynamic Host Configuration Protocol (DHCP) is a communication protocol that computers use to automatically assign IP
addresses to devices connected to a local network or over the internet. Prior to the invention of the DHCP protocol, adding any
ew computer toa network required manual action to add its Mac address to an P address. Managing the network was
complicated in case of a large number of hosts over 9 network. However, with the DHCP installed and configured on a local
network, any computer that is accepted in the network automatically gets an IP address associated with its Mac address.
A Domain Name Server (DNS) manages the generation of IP addresses, and a OHCP server automaticaly distributes the
configuration among the hosts.
DHCP Server Installation: Standard DHCP server implementation available in various Linuxdistributions is an Open source
version maintained by ISC (Internet System Consortium).
Use the appropriate Linux command below to install DHCP with your Linux distro’s package manager.
To install DHCP on Ubuntu, Debian, and Linux Mint:
$ sudo apt install isc-dhcp-server
To install DHCP on CentOS, Fedora, AlmaLinux, and Red Hat:
$ sudo dnf install dhep
Basic DHCP Configuration: By default, DHCP server configuration does not include any subnets on which DHCP server should
lease IP addresses. Therefore, depending on your Linux system you may get the following error message when you attempt to
start DHCP with the default dhepd.conf configuration file,
Ignou Study Helper-Sunil Poonia Page SEf ete Too Stuvy HeLree
‘www.ignousite.com & =? SUNIL POOWIA
Starting ISC DHCP server: dhepdcheck syslog tor diagnostics... failed!
Examining log files such as /var/log/syslog reveals more details:
No subnet declaration for eth0 (some IP address)
Your server may be connected to multiple network subnets. To start DHCF server, at least one subnet must be defined within
the DHCP configuration file /etc/dhep/dhepd.cont..
Below is the simplest example of DHCP configuration file:
subnet 10.1.1.0 netmask 255.255.255.0 {
range 10.1.1.3 10.1.1.254
}
‘subnet 192.168,0,0 netmask 255,255.0.0 {
}
Use nano ar your favor
text editor to make this change below/ll the commented lines, ier
pre eee
Co ets eT mes
,
PremeCR Ue resent
Se Fed Eke Sa Es
tee eee Eee eke Sao
This configuration file instructs DHCP server to listen for DHCP dlient requests on subnet 10.1.1.0 with netmask 255.255.2550
Furthermore, itil assign IP addresses in range 10.1.1.3~10.1.1.254. It also defines an empty definition for subnet with
network ID 192.168.0.0.
Alter the above code with your subnet and insert i
into /ete/ahcp/dhepd.cont. When ready, restart your DHCP server with (
restart command may vary |
$ sudo systemetl restart ise-dhep-server
Ignou Study Helper-Sunil Poonia Page 6fests Toney Stupy Hecpee
www ignousite.com eS Sumit Poona
DHCP default and max lease time: At this point we can add a few additional settings to our DHCP configuration, namely the
default and max lease time expiry.
‘© default-lease-time is a value in seconds in which a leased IP address expiry will be set to if DHCP client does not ask for
any other specific expiry lease time
‘+ may-lease-time is 2 value in seconds which defines a maximum expiry time for an IP address leased by DHCP server
etal-heane-thane 600;
max-lease-time 7200;
subnet 10.110 netmask 255,255,255.0(
range 10.1.1.3101.1.254)
+
subnet 192.168.0.0 netmask 255.255.0.0{
:
Define DNS Server: Another configuration parameter possible to be set by DHCP server to its client isa definition of DNS server.
Hf you want your clients to use DNS server with an IP address 8.8.8.8 (Google DNS server] and 10.4.1.1 you can do it by including,
zn option domain-name-servers to DHCP's configuration file.
default-lease-time 600;
max-lease-time 7200;
subnet 10,1.1.0 netmask 255.255.2550 {
range 10.1.1.3 10.1.1.254;
‘ation domain-name-servers 10.1.1.1. 8.8.8.8:
}
subnet 192.168.0,0 netmask 255,255.0.0 {
)
subnet 10.1.1.0 netmask 255.255,255.0[
range 10.1.1.3 10.1.1.254)
option routers 10.1.1.1;
}
Set default gateway: DHCP also allows for a client’s gateway configuration. To set any client on the local network to use default
gateway 10.1.1.1, add line option routers 10.1.1.1 into dhepd.conf file as demonstrated below:
default-lease-time 600;
max-lease-time 7200;
subnet 10,1.1.0 netmask 255.255,255.0 {
range 10.1.1. 10.1.1.254:
Ignou Study Helper-Sunil Poonia Page 7Tandy Stupy HELPER
—_
www.ignousite.com ‘ SUNIL POONIA
option domain-name-servers 10.1.1.1, 8.8.8.8
option routers 10.1.1.1;
}
subnet 192.168.0.0 netmask 255.255.
}
‘subnet 10.1.1.0 netmask 255,255.255.0 {
range 10.1.1.3 10.1.1.254 Ah
option routers 10.1.1.1; Hy
)
GNU nano 4.8 7etc/dhcp/dhepd. cont
Poem e erat a.)
Poet neh acid
CeCe SRR e eee eet
irc (eis Bei (Sees ee irr
option domain-name-servers 19.1.1.1, 8.8.8.8;
Crete cies OSes ey
i
Siete RRM ei ela kes
+
subnet 19.1,1.0 neta Pont
circe (Sis met
Castro Ce
CRTs Ce aoe peastsy
for met Replace Pec Tm ee acest
DHCP will now set DHCP client with gateway 10.1.1.1,
Host specific configuration: There may he a need to Set static IP address to’ particular host on the network such as printer, web
server and ete In this ease itis pascihle to amenel DHCP server configuration to leace a chancen IP arideess ta a specific hast
defined by its MAC address.
default-lease-time 600;
maxclease-time 7200;
subnet 10,1.1.0 netmask 255.255,255.0{
Ignou Study Helper-Sunil Poonia Page 8fees ys Toney Styny Hetpee
nw ignousite.com Nam? SUMIL POONA
range 10.1.1.3 10.1.1.254
option domain-name-servers 10.1.1.1, 8.8.8.8;
option routers 10.1.1.1;
}
subnet 192.168.0.0 netmask 255.255.0.0 {
?
host printer {
hardware ethernet 00:16:03:b7:81:86;
fixed-address 10.1.1.100;
) "
Load
host web server ens
hardware ethernet 00:17:04:c2:44:22;
fixed-address 10.1.1.200;
}
The above DHCP configuration file will permanently assign the IP address 10.1.1.100 to a host “printer” with a MAC
address 00:16:43:b7:8f:86 and P address 10.1,1.200 to host “web-server” with MAC address 00:17:a4:c2:44:22.
Q3. (a) Run the following Linux commands @n your machine and show the output:
* df-h
* psag.
+ ping
+ cat
* tall-f
Ans,
dfsh To display information about disk drives in human-readable format (kilobytes, megabytes, gigabytes and so on), invoke
the df command with the -h option:
$ai-h
est)
FE eet
Ignou Study Helper-Sunil Poonia Page 9Ef ete Too Stuvy HeLree
‘www.ignousite.com & =f SUNIL POONIA
pag The ps Lux command creates a snapshot of curentiy running processes. Unlike the oer commands on this
list, ps presents the cutout as a static lst, not updated in real time.
‘The ps command uses the following syntax:
ps options]
output
(eer
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 adress 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. Ping uses ICMP|internet Control Message Protocal) to send an ICMP echo message to the specified hostf that host
is available (hen it sends ICMP ieply message. Pit nilisecord every modern operating Syatenn has this
ping pre-installed
Now let see the PING command
Command:
sudo ping -v
is generally measured i
Output:
Peer
Sore
Surmetccmerace)
Cat Cat{concatenate) command is very frequently used in Linux. Itreads data from the file and gives their content as output. It
helps us to create, view, concatenate files, So let us see some frequently used cat commands.
Command:
$cat filename
Output:
e-will show content of given tiiename
Tall 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 ofa 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
Ignou Study Helper-Sunil Poonia Page 10“¢ ete Too Stony HELPEe
www ignousite.com & =? SUNIL POONIA
Use the lnterupt key to abort this command In general the appiatons wie error messages to og fs, You can use the
fF option to check for the error messages as and when they appear in the log file.
$ tail -f
Output:
load average: 0.17, 0.05, 0.01
Pe eg
kB
Tenaga a
Tea werae lies Cee)
A
(b) Write and run commands in Linux for the following tasks:
- Add new users
Display the list of users whe belong to different groups
- Display the list of users who are currently logged on,
- List all the processes which are €urrently running in the systems.
- List all file names that have a letter @ as Sth character in their name
‘Ans. Add new users: Steps to add new user on Linux:
1. Launch a terminal application,
2. Run adduser command with a username as argument,
sudo adduser username
3, Enter password for current user if necessary.
{sudo] password for user:
4, adduser will add the user along with other details
Ignou Study Helper-Sunil Poonia Page 11Heys Touoy Stuvy Here
www ignousite.com Nam? SUMIL POONA
Adding user ‘username’
‘Adding new group ‘username’ (1001)
‘Adding new user ‘username’ (1002) with group ‘username
Creating home directory ‘/nome/username' ..
Copying files from ‘/ete/ske!
5. Enter desired password for the user followed by [ENTER] twice.
New password:
Retype new passwords
passwd: password updated successfully
6. Enter some informational values (0
nal) or just press {ENTER] to keep blank and proceed.
Changing the user information for username
Enter the new value, or press ENTER for the default
Full Name []:
Room Number fl:
Work Phone [}:
Home Phone |]
other fi:
7. Press ¥ and [ENTER] to confirm everything |s Correct for adduser to really create the user.
Is the information correct? [Y/n] y
Output:
lusernane:
Ceara}
Seca
Baca
Seta sae Rie
Display the list of users who belong to different groups:
Use the grep command as follows:
$ arep ‘grpup-name-here’ /ete/group
$ grep ‘ftponly' /ete/group
$ grep -i--color'tponly’ /etc/groupty ete Too Stuy HELPER
www ignousite.com se =f SUNIL POONIA
eee eer ro ae
er NC eee
‘who command — Shows information about users who are currently longed on.
who command works on all Unix like operating systems:
who
root pts/O 2023-03-12 15:10 (10.13.17)
Pass the -a option to who command:
fiwho-a
outputs: fe ea
yatem boot 2013 03 02 04:20
runlevel 3 2013-03-02 04:10
ONES Me OE SE SOTICEey
ey
LOGIN
oy
ey
(a
ron 2013-03-02 0 7961
SRC BERT AI)
Soc
Type the following ps command to display all running process:
Hps-aux | less
oR
fi psaux | less
Where,
Ignou Study Helper-Sunil Poonia Page 13“¢ ete Too Stony HELPEe
www ignousite.com & =? SUNIL POOWIA
= AzSelect all processes
u: Select all processes on a terminal, including those of other users
x :Select processes without controlling ttys
roe D ‘COMMAND
ery were sy
an aes)
oe) ee Mia Sy
rey eens]
4 R ae]
itergstcyL3|
[reu_sched]
ati
ey
Sti icyEs
Secegstev esl
cmc ee ts)
ico
iestietat
Sacegstt
eee
enon
Peers ewer]
5 een
PICS|
ie oy
eeeegraie 3]
lene
tess Cra]
eerste ts]
Cer rt nt]
ei)
ieveiessed
terete ya
Cee eiat ie]
fon
ec
iteraaae
fi
lan)
ete
oo
vee sseECy|
[ksoftirad/10}
creer wre
Ignou Study Helper-Sunil Poonia Page 14fe “1 Toney Stupy HeLPee
nw ignousite.com Nam? SUMIL POONA
List all file names that have a letter e as Sth character in their name:
In regex * is start symbol ,/ means starts with /.[e][5} means followed by 5 characters, \. means . where \is escape
character \.cpp$ means ends with .cpp
If file name contains numbers instead of [e]. So it will look like this:
find . -regextype posix-erren -repex '*./IeHS)\.cop$*
output:
Ceara
Peru es cory
eo
earth
co
Cod
Ignou Study Helper-Sunil Poonia Page 15