deva recd
deva recd
Date: 20/12/2024
EXPERIMENT NO: 01
NETWORK COMMANDS
AIM:Getting started with the basics of network configuration files and networking
commands in Linux.
PROGRAM OBJECTIVE: Understanding and using of commands like ifconfig, netstat,
ping, arp, telnet, ftp, finger, traceroute, who is
PROGRAM DESCRIPTION: UNIX utilities are commands that, generally, perform a
single task. It may be as simple as printing the date and time, or a complex as finding files
that match many criteria throughout a directory hierarchy.
IFCONFIG
Linux (ifconfig): ifconfig is a command-line tool used to configure network interfaces. It
allows users to set IP addresses, netmasks, and enable or disable interfaces. The ifconfig -a
command is particularly useful for displaying all network interfaces, including those that are
inactive. This makes it a fundamental tool for network administration and troubleshooting in
Linux environments.
Windows (ipconfig): ipconfig serves as the Windows equivalent, providing information
about the computer's TCP/IP network configuration. By using commands like ipconfig /all,
users can view detailed information about network adapters, IP addresses, DNS servers, and
other network-related settings. This tool is crucial for diagnosing network connectivity issues
and understanding the network configuration of a Windows system.
ifconfig
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 131 bytes 11159 (11.1 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 131 bytes 11159 (11.1 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
● -e: Displays Ethernet statistics, such as sent and received bytes and packets.
● -n: Shows active TCP connections with numerical addresses and ports.
● -o: (Windows) Shows active TCP connections with process IDs (PIDs).
These parameters allow users to analyze network traffic, identify potential issues, and
measure network performance.
PING
ping is a command-line network utility used to test the reachability of a host on an Internet
Protocol (IP) network. It functions by sending Internet Control Message Protocol (ICMP)
echo request packets to the target host and then listening for ICMP echo response replies.
This process allows users to determine if a connection exists between their device and the
target host. While often used to estimate round-trip time, ping primarily focuses on verifying
connectivity and reporting packet loss. It provides a statistical summary of the test,
highlighting any lost packets and indicating the stability of the connection. Beyond basic
connectivity checks, ping can also be employed to self-test a computer's network interface
card, ensuring it is functioning correctly. It's a fundamental tool for network troubleshooting,
allowing users to quickly identify potential network issues and confirm IP-based device
connections.
ping 192.168.86.45
The ping command establishes a connection with the other device on the network and should
show a result like this:
ARP
In computer networking, the Address Resolution Protocol (ARP) is the method for finding
a host's link layer (hardware) address when only its Internet Layer (IP) or some other
Network Layer address is known. ARP has been implemented in many types of networks; it
is not an IP-only or Ethernet-only protocol. It can be used to resolve many different network
layer protocol addresses to interface hardware addresses, although, due to the overwhelming
prevalence of IPv4 and Ethernet, ARP is primarily used to translate IP addresses to Ethernet
MAC addresses.
Use the following ARP command to get a list that shows the MAC address of the device you
pinged:
arp -a
The results may look something like this but probably with many other entries:
Interface: 192.168.86.38 --- 0x3
Internet Address Physical Address Type
192.168.86.1 70-3a-cb-14-11-7a dynamic
192.168.86.45 98-90-96-B9-9D-61 dynamic
192.168.86.255 ff-ff-ff-ff-ff-ff static
224.0.0.22 01-00-5e-00-00-16 static
224.0.0.251 01-00-5e-00-00-fb static
Find the device's IP address in the list. The MAC address is shown right next to it. In this
example, the IP address is 192.168.86.45, and its MAC address is 98-90-96-B9-9D-61.
TELNET
Telnet (Telecommunication network) is a network protocol used on the Internet or local
area network (LAN) connections. In Linux, the telnet command is used to create a remote
connection with a system over a TCP/IP network. Typically, telnet provides access to a
command-line interface on a remote machine. The term telnet also refers to software which
implements the client part of the protocol. Telnet clients are available for virtually all
platforms. Protocol details: Telnet is a client-server protocol, based on a reliable connection-
oriented transport. Typically this protocol is used to establish a connection to TCP port 23.
Telnet 192.168.86.45
Trying 192.168.86.45...
Connected to 192.168.86.45.
Escape character is '^]'.
FTP
File Transfer Protocol (FTP): FTP is a network protocol used to transfer data from one
computer to another through a network such as the Internet.FTP is a file transfer protocol for
exchanging and manipulating files over a TCP computer network. An FTP client may
connect to an FTP server to manipulate files on that server.FTP runs over TCP. It defaults to
listen on port 21 for incoming connections from FTP clients. A connection to this port from
the FTP Client forms the control stream on which commands are passed from the FTP client
to the FTP server and on occasion from the FTP server to the FTP client. FTP uses out-of-
band control, which means it uses a separate connection for control and data. Thus, for the
actual file transfer to take place, a different connection is required which is called the data
stream. To establish an FTP connection to a remote system, use the ftp command with the
remote system's IP address:
ftp [IP]
For instance, connecting to a remote server with the IP address 192.168.100.9:
ftp 192.168.100.9
FINGER
In computer networking, the Name/Finger protocol and the Finger user information
protocol are simple network protocols for the exchange of human-oriented status and user
information.
finger -p ch
Display information about the user ch. Output appears similar to the following:
Login name: admin
In real life: Computer Hope
On since Feb 11 23:37:16 on pts/7 from domain.computerhope.com
28 seconds Idle Time
Unread mail since Mon Feb 12 00:22:52 2001
TRACEROUTE
traceroute is a computer network tool used to determine the route taken by packets across an
IP network. An IPv6 variant, traceroute6, is also widely available. Traceroute is often used
for network troubleshooting. By showing a list of routers traversed, it allows the user to
identify the path taken to reach a particular destination on the network. This can help identify
routing problems or firewalls that may be blocking access to a site. Traceroute is also used by
penetration testers to gather information about network infrastructure and IP ranges around a
given host. It can also be used when downloading data, and if there are multiple mirrors
available for the same piece of data, one can trace each mirror to get a good idea of which
mirror would be the fastest to use.
The traceroute command in Windows is tracert. On a Linux system, the command is
traceroute. A typical tracert on a Windows machine would look like the following.
tracert www.google.com
Tracing route to www.google.com [74.125.227.179] over a maximum of 30 hops: 1 1 ms <1
ms 1 ms 192.168.1.1
2 7 ms 6 ms 6 ms 10.10.1.2
3 7 ms 8 ms 7 ms 10.10.1.45
4 9 ms 8 ms 8 ms 10.10.25.45
5 9 ms 10 ms 9 ms 10.10.85.99
6 11 ms 51 ms 10 ms 10.10.64.2
7 11 ms 10 ms 10 ms 10.10.5.88
8 11 ms 10 ms 11 ms 216.239.46.248
9 12 ms 12 ms 12 ms 72.14.236.98
10 18 ms 18 ms 18 ms 66.249.95.231
11 25 ms 24 ms 24 ms 216.239.48.4
12 48 ms 46 ms 46 ms 72.14.237.213
13 50 ms 50 ms 50 ms 72.14.237.214
14 48 ms 48 ms 48 ms 64.233.174.137
15 47 ms 47 ms 46 ms dfw06s32-in-f19.1e100.net [74.125.227.179] Trace
complete.
WHO IS
WHOIS (pronounced "who is"; not an acronym) is a query/response protocol which is widely
used for querying an official database in order to determine the owner of a domain name, an
IP address, or an autonomous system number on the Internet. WHOIS lookups were
traditionally made using a command line interface, but a number of simplified web-based
tools now exist for looking up domain ownership details from different databases. WHOIS
normally runs on TCP port 43.
The WHOIS system originated as a method that system administrators could use to look up
information to contact other IP address or domain name administrators (almost like "white
pages").
whois 216.58.206.46
RESULT
Get familiarised with various network commands.
Date: 20/12/2024
EXPERIMENT NO: 02
SYSTEM CALLS
AIM: To familiarize and understand the use and functioning of system calls used for
network programming in Linux.
PROGRAM OBJECTIVE: To implement and understand fundamental Linux system calls
for network programming, including socket creation, connection handling, and data transfer.
PROGRAM DEFINITION: This program explores core Linux system calls for network
communication, demonstrating socket creation, connection establishment, and data transfer.
● protocol: Protocol value for Internet Protocol (IP), usually 0 (default protocol
for the given domain and type).
2. Setsockopt
This helps in manipulating options for the socket referred by the file descriptor
sockfd. This is completely optional, but it helps in reuse of address and port. Prevents
error such as: “address already in use”.
int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t
optlen);
sockfd: Socket descriptor.
server_addr: Server address structure (contains IP & port).
sizeof(server_addr): Size of the address structure.
Returns: positive integer on success, -1 on failure.
3. Bind
After the creation of the socket, the bind function binds the socket to the address and
port number specified in addr(custom data structure). In the example code, we bind
the server to the localhost, hence we use INADDR_ANY to specify the IP address.
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
sockfd: Socket descriptor.
backlog: Number of pending connections allowed in the queue.
4. Listen
It puts the server socket in a passive mode, where it waits for the client to approach
the server to make a connection. The backlog, defines the maximum length to which
the queue of pending connections for sockfd may grow. If a connection request
arrives when the queue is full, the client may receive an error with an indication of
ECONNREFUSED.
It extracts the first connection request on the queue of pending connections for the
listening socket, sockfd, creates a new connected socket, and returns a new file descriptor
referring to that socket. At this point, the connection is established between client and
server, and they are ready to transfer data.
int new_socket= accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
sockfd: Socket descriptor.
client_addr: Structure to store the client's address.
addr_len: Size of the client's address structure.
6. send()
The send() function in C is used in socket programming to transmit data from a sender
(client or server) to a receiver over a network.
send(int sockfd, const void *buffer, size_t length, int flags)
● flags (int) :Extra options for receiving (set to 0 for normal operation)
8. close()
The close() function is used to close a socket after communication is completed. It
releases system resources associated with the socket.
int close(int sockfd);
sockfd: Socket descriptor.
● protocol: Protocol value for Internet Protocol (IP), usually 0 (default protocol
for the given domain and type).
2. Bind
After the creation of the socket, the bind function binds the socket to the address and port
number specified in addr(custom data structure). In the example code, we bind the server
to the localhost, hence we use INADDR_ANY to specify the IP address.
● *buffer (void ) :Pointer to the buffer where the received data will be stored.
RESULT
Familiarised with the system calls.
Date: 10/01/2025
EXPERIMENT NO:03
SOCKET PROGRAMMING USING TCP
AIM: Implement client-server communication using socket programming and TCP as
transport layer protocol.
PROGRAM OBJECTIVE: To understand TCP-based client-server communication.
PROGRAM DEFINITION: A C program demonstrating TCP socket communication where
the client sends a message, and the server receives and displays it.
ALGORITHM
Client
1. Start
2. Create socket for client.
3. Create socket for server.
4. Connect client to server.
5. Read a message from user and store it in a character array.
6. Send it to server.
7. Close the socket for client.
8. Stop
Server
1. Start
2. Create socket for server.
3. Create address for server and client.
4. Bind the server socket to server address.
5. Create listen for server.
6. Create a temporary socket and accept client socket to server socket.
PROGRAM
Client
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<unistd.h>
int main(){
char buf[100];
int k;
int sock_desc;
struct sockaddr_in client;
sock_desc=socket(AF_INET,SOCK_STREAM,0);
if(sock_desc==-1)
printf("Error in socket");
client.sin_family=AF_INET;
client.sin_addr.s_addr==INADDR_ANY;
client.sin_port=3003;
k=connect(sock_desc,(struct sockaddr*)&client,sizeof(client));
if(k==-1)
printf("Error in connecting to server\n");
printf("\nEnter the data: ");
fgets(buf,100,stdin);
k=send(sock_desc,buf,100,0);
printf("sending...");
close(sock_desc);
return 0;
Server
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<unistd.h>
int main(){
char buf[100];
int k;
socklen_t len;
int sock_desc,temp_sock_desc;
struct sockaddr_in server,client;
sock_desc=socket(AF_INET,SOCK_STREAM,0);
if(sock_desc==-1)
printf("Error in socketcreation ");
server.sin_family=AF_INET;
server.sin_addr.s_addr=INADDR_ANY;
server.sin_port=3003;
client.sin_family=AF_INET;
client.sin_addr.s_addr=INADDR_ANY;
client.sin_port=3003;
k=bind(sock_desc,(struct sockaddr*)&server,sizeof(server));
if(k==-1)
printf("Error in binding");
k=listen(sock_desc,5);
if(k==-1)
printf("Error in listening");
len=sizeof(client);
temp_sock_desc=accept(sock_desc,(struct sockaddr*)&client,&len);
if(temp_sock_desc==-1)
printf("Error in temporary socket creation");
k=recv(temp_sock_desc,buf,100,0);
if(k==-1)
printf("ERROR in receiving");
printf("Message got from client is : %s",buf);
close(temp_sock_desc);
return 0;
}
OUTPUT
RESULT
The program has been executed successfully and output is obtained.
Date: 17/01/2025
EXPERIMENT NO:04
SOCKET PROGRAMMING USING UDP
AIM: Implement client-server communication using socket programming and UDP as
transport layer protocol.
PROBLEM OBJECTIVE: To understand UDP-based client-server communication.
PROBLEM DEFINITION: A C program that demonstrates UDP socket communication
where the client sends two integers, and the server calculates and displays their sum.
ALGORITHM
Client
1. Start
2. Create a socket for client.
3. Create address for server.
4. Read a message from user and store it in a character array.
5. Send the client socket to server.
6. Stop
Server
1. Start
2. Create a socket for server.
3. Create address for server.
4. Bind the server socket to server.
5. Receive value from client and save it to character array.
6. Print the result.
7. Stop
PROGRAM
Client
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
int main(int argc,char* argv[])
{
struct sockaddr_in server,client;
if(argc!=3)
printf("Input format not correct");
int sockfd=socket(AF_INET,SOCK_DGRAM,0);
if(sockfd==-1)
printf("Error in socket():");
server.sin_family=AF_INET;
server.sin_addr.s_addr=INADDR_ANY;
server.sin_port=htons(atoi(argv[2]));
char buffer[100];
printf("Enter a message to be send to the server:");
fgets(buffer,100,stdin);
if(sendto(sockfd,buffer,sizeof(buffer),0,(struct sockaddr*)&server,sizeof(server))<0)
return 0;
}
Server
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
OUTPUT
RESULT
Date: 14/02/2025
EXPERIMENT NO:05
FILE TRANSFER PROTOCOL
AIM: Implement File Transfer Protocol.
ALGORITHM
Client
1. Start
2. Create a TCP socket for client.
3. Create address for server.
4. Connect client with server.
5. Read the source filename.
6. Read the destination filename.
7. Send the source file to the server.
8. Repeat step 9-5 while the content received from server not equal to zero and store file
content to character array rcvg.
9. Compare the rcvg with error. If the file content in rcvg is equal to end, then print “File
is not available”.
10. Compare the rcvg with end. If the file content in rcvg is equal to end, then go to step
11, otherwise go to step 14.
PROGRAM
Client
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<netdb.h>
#include<netinet/in.h>
int main()
{
FILE *fp;
int csd,n,ser,s,cli,cport,newsd;
char name[100],rcvmsg[100],rcvg[100],fname[100];
struct sockaddr_in servaddr;
printf("Enter the port\n");
scanf("%d",&cport);
csd=socket(AF_INET,SOCK_STREAM,0);
if(csd<0)
{
printf("error....\n");
exit(0);
}
else
printf("socket is created\n");
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(cport);
if(connect(csd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
printf("error in connection\n");
else
printf("connected\n");
printf("enter the existing file name\t");
scanf("%s",name);
printf(" enter the new file name\t");
scanf("%s",fname);
fp=fopen(fname,"w");
send(csd,name,sizeof(name),0);
while(n=recv(csd,rcvg,100,0)!=0)
{
if(strcmp(rcvg,"error")==0)
printf("file is not available\n");
if(strcmp(rcvg,"end")==0)
{
printf("file is transferred....\n");
fclose(fp);
close(csd);
exit(0);
}
else
fputs(rcvg,stdout);
fprintf(fp,"%s",rcvg);
}
return 0;
}
Server
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<netdb.h>
#include<netinet/in.h>
int main()
{
FILE *fp;
int sd,n,ser,a,cli,newsd,pid,bd,port,clilen;
char name[100],fileread[100],ch,file[100],rcv[100],fname[100];
struct sockaddr_in servaddr,cliaddr;
printf("Enter the port address\n");
scanf("%d",&port);
sd=socket(AF_INET,SOCK_STREAM,0);
if(sd<0)
printf("Can't create\n");
else
printf("socket is created\n");
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(port);
a=sizeof(servaddr);
bd=bind(sd,(struct sockaddr*)&servaddr,a);
if(bd<0)
printf("Can't bind\n");
else
printf("Binded");
listen(sd,5);
clilen=sizeof(cliaddr);
newsd=accept(sd,(struct sockaddr*)&cliaddr,&clilen);
if(newsd<0)
{
printf("Can't accept\n");
}
else
printf("Accepted");
n=recv(newsd,rcv,100,0);
if(n==-1)
printf("Error in receiving\n");
n=sizeof(rcv);
rcv[n]='\0';
fp=fopen(rcv,"r");
if(fp==NULL)
{
send(newsd,"error",5,0);
close(newsd);
}
else
{
while(fgets(fileread,sizeof(fileread),fp))
{
if(send(newsd,fileread,sizeof(fileread),0)<0)
{
printf("Can't send file content\n");
}
printf("file contents %s",fileread);
sleep(1);
}
if(fgets(fileread,sizeof(fileread),fp))
{
send(newsd,"complete",999999999,0);
}
}
return 0;
}
OUTPUT
RESULT
The program has been executed successfully and output is obtained.
Date: 20/02/2025
EXPERIMENT NO:06
SIMPLE MAIL TRANSFER PROTOCOL
AIM: Implement a simple SMTP (Simple Mail Transfer Protocol) simulation using socket
programming in C.
OBJECTIVE: To understand a simple SMTP (Simple Mail Transfer Protocol) simulation
using socket programming in C.
DEFINITION: A C program that demonstrates SMTP-based client-server communication,
where the client sends email data, and the server processes and responds accordingly.
ALGORITHM
Client
1. Start
2. Create a TCP socket from client.
3. Create address for client.
4. Connect client with server.
5. Send H1 to server.
6. Receive response for H1.
7. Sending HELLO to server.
8. Receive response for HELLO.
9. Read FROM ADDRESS.
10. Copy MAIL FROM to sendbuff variable.
11. Concatenate FROM ADDRESS to sendbuff.
12. Send sendbuff to server.
13. Receive OK from server in rec to buff.
14. Compare the recbuff to 250.If match is found ,print recbuff otherwise print “OK NOT
RECEIVED”.
15. Read TO ADDRESS.
Server
1. Start
2. Create socket for the server.
3. Create address for the server.
4. Bind server socket to server address.
5. Create listen for the server,listen.
6. Create temporary socket and accept client socket to server socket.
7. Receive HI from client.
8. Copy 220 192.108.9.138 to sendbuff and sendbuff to client.
9. Receive HELLO from client and store in recbuff.
10. Check whether recbuff contains HELLO. If yes print recbuff otherwise print
“ERROR”.
11. Copy 250 OK to sendbuff and send it to client.
12. Receive FROM ADDRESS from client.
13. if FROM ADRESS received successfully print FROM ADDRESS otherwise print
“FROM ADDRESS EROR”.
14. Copy 250 OK to sendbuff and send sendbuff to client.
15. Receive TO ADDRESS from client.
16. If TO ADDRESS received successfully print TO ADDRESS otherwise print “TO
ADDRESS ERROR”.
17. Copy 250 OK to sendbuff and send it to client.
PROGRAM
Client
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
void main()
{
int cid,con,size,csend,crec,i;
char sendbuff[500],recbuff[500],fromaddress[500],toaddress[500],mailbody[1000];
cid=socket(AF_INET,SOCK_STREAM,0);
check("Socket creation",cid);
csend=send(cid,sendbuff,500,0);
check("SENDING",csend);
printf("Waiting ok from server\n");
crec=recv(cid,recbuff,500,0);
check("RECEIVE",crec);
if(strncmp(recbuff,"250",3))
printf("Ok not received\n");
else
printf("Message from server:%s\n",recbuff);
printf("Enter to address: ");
scanf("%s",toaddress);
strcpy(sendbuff,"MAIL TO:");
strcat(sendbuff,toaddress);
csend=send(cid,sendbuff,500,0);
check("SENDING",csend);
printf("Waiting ok from server\n");
crec=recv(cid,recbuff,500,0);
check("RECEIVE",crec);
if(strncmp(recbuff,"250",3))
printf("Ok received\n");
else
printf("Message from server: %s\n",recbuff);
printf("Sending data to the server...\n");
strcpy(sendbuff,"DATA");
csend=send(cid,sendbuff,500,0);
check("SENDING",csend);
printf("Waiting ok from server\n");
crec=recv(cid,recbuff,500,0);
check("RECEIVE",crec);
if(strncmp(recbuff,"354",3))
printf("Ok not received\n");
else
printf("Message from server: %s\n",recbuff);
printf("Enter mail body\n");
for(i=0;;i++)
{
fgets(mailbody,sizeof(mailbody),stdin);
csend=send(cid,mailbody,1000,0);
if(strncmp(mailbody,"$",1)==0)
break;
check("SENDING",csend);
}
printf("Sending mailboy to server\n");
printf("Waiting ok from server\n");
crec=recv(cid,recbuff,500,0);
check("RECEIVE",crec);
if(strncmp(recbuff,"221",3))
printf("Ok not received\n");
else
printf("Message from server: %s\n",recbuff);
strcpy(sendbuff,"QUIT");
csend=send(cid,sendbuff,1000,0);
printf("Sending %s...\n",sendbuff);
strcpy(recbuff,"");
crec=recv(cid,recbuff,500,0);
if(strncmp(recbuff,"221 OK",6)==0)
{
printf("Exiting...\n");
}
printf("Connection Closed\n");
close(cid);
}
Server
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
void main()
{
int sid,sbind,size,slisten,saccept,srec,ssend,i,n=5;
char recbuff[500],sendbuff[500],fromaddress[500],toaddress[500],mailbody[1000];
sid=socket(AF_INET,SOCK_STREAM,0);
check("SOCKET CREATION",sid);
struct sockaddr_in saddr;
saddr.sin_family=AF_INET;
saddr.sin_port=htons(8082);
saddr.sin_addr.s_addr=INADDR_ANY;
size=sizeof(saddr);
sbind=bind(sid,(struct sockaddr*)&saddr,size);
check("BINDING",sbind);
slisten=listen(sid,5);
check("LISTEN",slisten);
saccept=accept(sid,(struct sockaddr*)&saddr,&size);
check("ACCEPT",saccept);
printf("\n.....\n");
srec=recv(saccept,recbuff,500,0);
check("RECEIVE",srec);
printf("Message from client:%s\n",recbuff);
printf("Sending ok to To address\n");
strcpy(sendbuff,"250 OK");
ssend=send(saccept,sendbuff,500,0);
check("SENDING",ssend);
printf("Waiting for data\n");
srec=recv(saccept,recbuff,500,0);
check("RECEIVE",srec);
if(strncmp(recbuff,"DATA",4))
printf("Data not received\n");
else
printf("Message from client:%s\n",recbuff);
printf("Sending ok to client\n");
strcpy(sendbuff,"354 GO AHEAD");
ssend=send(saccept,sendbuff,500,0);
check("SENDING",ssend);
printf("Mail body:\n");
for(i=0;;i++)
{
srec=recv(saccept,mailbody,1000,0);
check("MAIL BODY RECEIVE",srec);
if(strncmp(mailbody,"$",1)==0)
break;
else
printf("%s",mailbody);
}
strcpy(sendbuff,"221 OK");
ssend=send(saccept,sendbuff,500,0);
printf("Sending ok to client\n");
check("SENDING",ssend);
srec=recv(saccept,mailbody,1000,0);
if(strncmp(mailbody,"QUIT",4)==0)
{
strcpy(sendbuff,"221 OK");
ssend=send(saccept,sendbuff,500,0);
printf("Sending 221 ok...\n");
}
printf("Connection closed\n");
close(saccept);
close(sid);
}
OUTPUT
RESULT
The program has been executed successfully and output is obtained.
Date: 28/02/2025
EXPERIMENT NO:07
LEAKY BUCKET ALGORITHM
AIM:To implement the Leaky Bucket algorithm for network traffic management.
OBJECTIVE: To understand the Leaky Bucket algorithm to regulate data flow and control
congestion by managing incoming and outgoing packet rates.
DEFINITION: A C program that demonstrates the Leaky Bucket algorithm, managing
packet flow by tracking buffer size, incoming packets, and outgoing rates to prevent data loss
due to buffer overflow.
ALGORITHM
1. Start
2. Read bucket size,number of processes,outgoing flow and store it in bsize,n,out.
3. Initialize storage=0.
4. For i from 0 to n , repeat step 5-16.
5. Read packet size from user and store in psize.
6. Calculate the size left in bucket as, size_left=bsize-storage.
7. If psize is less than or equal to size_left then go to step 8. Otherwise go to step 9.
8. Set storage=storage+psize. Go to step 10.
9. Print the number of packets dropped as psize_(bsize-storage).
10. Set storage=bsize.
11. Print storage and bsize.
12. If storage_out less than zero then go to step 13. Otherwise go to step 14.
13. Set storage=0. Go to step 15.
14. Set storage=storage_out
15. Print to out storage.
16. Set i=i+1.
17. Stop.
PROGRAM
#include <stdio.h>
int main()
{
int n,storage=0,out,psize,bsize,size_left;
printf("enter the no.of inputs:");
scanf("%d",&n);
printf("enter the bucket size:");
scanf("%d",&bsize);
printf("enter the outgoing rate:");
scanf("%d",&out);
for(int i=0;i<n;i++)
{
printf("enter incoming packet size:");
scanf("%d",&psize);
size_left=bsize-storage;
if(psize<=size_left)
{
storage+=psize;
}
else
{
printf("dropped %d no.of packets\n",psize-(bsize-storage));
storage=bsize;
}
printf("\nBucket buffer size %d out of %d",storage,bsize);
if(storage-out<0)
storage=0;
else
storage-=out;
printf("\nAfter outgoing %d packets %d packets left in buffer\
n",out,storage);
}
return 0;
}
OUTPUT
RESULT
The program has been executed successfully and output is obtained.
Date: 28/02/2025
EXPERIMENT NO:08
DISTANCE VECTOR ROUTING
AIM: To implement the distance vector routing algorithm for network communication.
DEFINITION: A C program that uses the Distance Vector Routing algorithm to compute
and display the shortest paths and next-hop details for each router in a given network
topology.
ALGORITHM
1. Start
2. Read the no. of nodes and store the value in nodes.
3. Repeat step 4 to 10 for i=0 to nodes.
4. Repeat step 5 to 9 for j=0 to nodes.
5. Read distance between i &j nodes and store in cost[i][j].
6. Set cosmat[i][j] = 0.
PROGRAM
#include<stdio.h>
struct node{
unsigned dist[20];
unsigned from[20];
}rt[10];
int main()
int cosmat[20][20];
scanf("%d",&nodes);
for(i=0;i<nodes;i++)
for(j=0;j<nodes;j++)
scanf("%d",&cosmat[i][j]);
cosmat[i][i]=0;
rt[i].dist[j]=cosmat[i][j];
rt[i].from[j]=j;
do
count=0;
for(i=0;i<nodes;i++)
for(j=0;j<nodes;j++)
for(k=0;k<nodes;k++)
if(rt[i].dist[j]>cosmat[i][k]+rt[k].dist[j])
rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j];
rt[i].from[j]=k;
count++;
while(count!=0);
for(i=0;i<nodes;i++)
for(j=0;j<nodes;j++)
printf("\n\n");
return 0;
OUTPUT
RESULT
Date: 07/03/2025
EXPERIMENT NO:09
STOP AND WAIT
AIM: Implement a Stop-and-Wait ARQ (Automatic Repeat reQuest) protocol using socket
programming in C.
ALGORITHM
Client
1. Start
2. Set packet[3][100]={“I am batman”,”I am the saviour of Gotham” ,”I am Walter
White”}
3. Create a TCP socket for client
4. Create address for client
5. Connect client with server
6. Set i=0
7. Repeat step 8 to step 12 while i<3
8. Send packet[i] to server
9. Receive acknowledge from server and store it in ack
10. If acknowledge, ack equals 1, go to step 12. Otherwise go to step 11
11. Send packet[i] to the server. Go to step 9
12. Set i= i+1
13. Close the socket connection
14. Stop
Server
1. Start
2. Create a TCP socket for server
3. Create address for server
4. Create address for client
5. Bind server socket to server address
6. Create listen for the server socket k
PROGRAM
Client
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
int sock_desc;
void sendPacket(char packet[],int i)
{
int k,m;
char ack[100];
k=send(sock_desc,packet,100,0);
if(k==-1)
printf("Error in sending.");
else
printf("Sending Packet %d\n",i+1);
for(;;)
{
m=recv(sock_desc,ack,100,0);
printf("%s\n",ack);
if(strcmp(ack,"1")==0)
break;
else
{
k=send(sock_desc,packet,100,0);
printf("Resending Packet %d\n",i+1);
}
}
}
int main()
{
char buf[100];
int k;
struct sockaddr_in client;
char packet[3][100]={" iam batman", "iam the saviour of the gotham","iam walter
white"};
sock_desc=socket(AF_INET,SOCK_STREAM,0);
if(sock_desc==-1)
printf("Error in socket creation.");
client.sin_family=AF_INET;
client.sin_addr.s_addr=INADDR_ANY;
client.sin_port=5651;
k=connect(sock_desc,(struct sockaddr*)&client,sizeof(client));
if(k==-1)
printf("Error in connecting to server\n");
for(int i=0;i<3;i++)
{
sendPacket(packet[i],i);
}
printf("Packets transmitted successfully\n");
close(sock_desc);
return 0;
}
Server
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdio.h>
#include<string.h>
#include<strings.h>
#include<stdlib.h>
#include<unistd.h>
int sock_desc,temp_sock_desc;
void recvPacket(int i)
{
char buff[100],ack[100];
int k;
k=recv(temp_sock_desc,buff,100,0);
if(k==-1)
printf("Error in receiving packet %d\n",i+1);
for(;;)
{
printf("Packet %d recived or not: ",i+1);
scanf("%s",ack);
ack[strlen(ack)]='\0';
k=send(temp_sock_desc,ack,100,0);
if(strcmp(ack,"1")==0)
{
break;
}
else
{
k=recv(temp_sock_desc,buff,100,0);
}
}
}
int main()
{
char buf[100];
char ack[100];
int k,m;
socklen_t len;
struct sockaddr_in server,client;
sock_desc=socket(AF_INET,SOCK_STREAM,0);
if(sock_desc==-1)
printf("Error in socket creation.");
server.sin_family=AF_INET;
server.sin_addr.s_addr=INADDR_ANY;
server.sin_port=5651;
client.sin_family=AF_INET;
client.sin_addr.s_addr=INADDR_ANY;
client.sin_port=5651;
k=bind(sock_desc,(struct sockaddr*)&server,sizeof(server));
if(k==-1)
printf("Error in binding.");
k=listen(sock_desc,5);
if(k==-1)
printf("Error in listening.");
len=sizeof(client);
temp_sock_desc=accept(sock_desc,(struct sockaddr*)&client,&len);
if(temp_sock_desc==-1)
printf("Error in temporary socket creation");
for(int i=0;i<3;i++)
{
recvPacket(i);
}
close(temp_sock_desc);
return 0;
}
OUTPUT
RESULT