0% found this document useful (0 votes)
3 views

deva recd

The document outlines two experiments from a networking lab, focusing on network commands and system calls in Linux. It details various networking commands such as ifconfig, netstat, ping, and telnet, explaining their usage and significance in network configuration and troubleshooting. Additionally, it introduces fundamental system calls for network programming, including socket creation and connection handling.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

deva recd

The document outlines two experiments from a networking lab, focusing on network commands and system calls in Linux. It details various networking commands such as ifconfig, netstat, ping, and telnet, explaining their usage and significance in network configuration and troubleshooting. Additionally, it introduces fundamental system calls for network programming, including socket creation and connection handling.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 46

CSL 332 NETWORKING LAB

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

wlp0s20f3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500


inet 192.168.77.53 netmask 255.255.255.0 broadcast 192.168.77.255
inet6 2409:40f3:100f:b642:85d9:b342:6ac:1149 prefixlen 64 scopeid 0x0<global>
inet6 2409:40f3:100f:b642:77e:f234:cf70:eb11 prefixlen 64 scopeid 0x0<global>

DEPT. OF CSE CEK 1


CSL 332 NETWORKING LAB

inet6 fe80::15c1:3a8e:f863:431c prefixlen 64 scopeid 0x20<link>


ether 28:11:a8:00:79:db txqueuelen 1000 (Ethernet)
RX packets 40 bytes 6068 (6.0 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 126 bytes 18768 (18.7 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
NETSTAT
netstat is a versatile command-line utility designed to provide insights into network activity
and configurations. It displays active network connections, routing tables, and network
interface statistics, making it a valuable tool for network troubleshooting and performance
monitoring. Common parameters include:

● -a: Shows all active TCP connections and listening ports.

● -e: Displays Ethernet statistics, such as sent and received bytes and packets.

● -f: Displays fully qualified domain names for foreign addresses.

● -i: (Linux/Unix) Displays network interfaces and their statistics.

● -n: Shows active TCP connections with numerical addresses and ports.

● -o: (Windows) Shows active TCP connections with process IDs (PIDs).

● -p: (Linux) Shows which processes are using which sockets.

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:

DEPT. OF CSE CEK 2


CSL 332 NETWORKING LAB

Pinging 192.168.86.45 with 32 bytes of data:


Reply from 192.168.86.45: bytes=32 time=290ms TTL=128
Reply from 192.168.86.45: bytes=32 time=3ms TTL=128
Reply from 192.168.86.45: bytes=32 time=176ms TTL=128Reply from 192.168.86.45:
bytes=32 time=3ms TTL=128

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

DEPT. OF CSE CEK 3


CSL 332 NETWORKING LAB

Trying 192.168.86.45...
Connected to 192.168.86.45.
Escape character is '^]'.

Welcome to the remote server!

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

DEPT. OF CSE CEK 4


CSL 332 NETWORKING LAB

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

DEPT. OF CSE CEK 5


CSL 332 NETWORKING LAB

The following results may also be obtained via:


https://whois.arin.net/rest/nets;q=216.58.206.46?
showDetails=true&showARIN=false&showNo nArinTopLevelNet=false&ext=netref2
NetRange: 216.58.192.0 - 216.58.223.255
CIDR: 216.58.192.0/19
NetName: GOOGLE
NetHandle: NET-216-58-192-0-1
Parent: NET216 (NET-216-0-0-0-0)
NetType: Direct Allocation
OriginAS: AS15169
Organization: Google LLC (GOGL)
RegDate: 2012-01-27
Updated: 2012-01-27
Ref: https://whois.arin.net/rest/net/NET-216-58-192-0-1
OrgName: Google LLC
OrgId: GOGL
Address: 1600 Amphitheatre Parkway
City: Mountain View
StateProv: CA
PostalCode: 94043
Country: US
RegDate: 2000-03-30
Updated: 2017-12-21

RESULT
Get familiarised with various network commands.

DEPT. OF CSE CEK 6


CSL 332 NETWORKING LAB

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.

2.1 COMMUNICATION IMPLEMENTATION USING TCP

Server-side system calls


1. Socket Creation
int sockfd = socket(domain, type, protocol)

● sockfd: Socket descriptor, an integer (like a file handle).

● domain: Specifies the communication domain.

DEPT. OF CSE CEK 7


CSL 332 NETWORKING LAB

AF_INET: For IPv4 communication between different hosts.


AF_INET6: For IPv6 communication between different hosts.

● type: Communication type.

● SOCK_DGRAM: UDP (unreliable, connectionless).

● 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.

int listen(int sockfd, int backlog);


sockfd: Socket descriptor.
backlog: Number of pending connections allowed in the queue.
5. Accept

DEPT. OF CSE CEK 8


CSL 332 NETWORKING LAB

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)

● sockfd:The socket descriptor (created using socket()).

● buffer: A pointer to the data that needs to be sent.

● length:The size of the data in bytes.

● flags : Special options (usually set to 0 for normal operation).


7. recv()
The recv() function receives data on a socket with descriptor socket and stores it in a
buffer.
recv(int sockfd, void *buffer, size_t length, int flags)

● sockfd (int) :The socket file descriptor (created using socket()).

● *buffer (void ) : A pointer to the memory where received data is stored.

● length (size_t) : The maximum number of bytes to receive.

● 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.

DEPT. OF CSE CEK 9


CSL 332 NETWORKING LAB

Client – side system calls


1. Socket connection: Exactly the same as that of server’s socket creation.
2. Connect: The connect() system call connects the socket referred to by the file
descriptor sockfd to the address specified by addr. Server’s address and port is
specified in addr.
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
3. send() and recv():Exactly same as server.
4. close():Exactly same as server.

2.2 COMMUNICATION IMPLEMENTATION USING UDP

Server-side system calls


1. Socket Creation
int sockfd = socket(domain, type, protocol)

● sockfd: Socket descriptor, an integer (like a file handle).

● domain: Specifies the communication domain.


AF_INET: For IPv4 communication between different hosts.
AF_INET6: For IPv6 communication between different hosts.
● type: Communication type.

● SOCK_DGRAM: UDP (unreliable, connectionless).

● 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.

DEPT. OF CSE CEK 10


CSL 332 NETWORKING LAB

int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);


sockfd: Socket descriptor.
server_addr: Server address structure (contains IP & port).
sizeof(server_addr): Size of the address structure.
3. recvfrom()
The recvfrom() function shall receive a message from a connection-mode or
connectionless-mode socket. It is normally used with connectionless-mode sockets
because it permits the application to retrieve the source address of received data.
recvfrom(int sockfd, void *buffer, size_t length, int flags, struct sockaddr *src_addr,
socklen_t *addrlen)

● sockfd (int) : The socket file descriptor.

● *buffer (void ) :Pointer to the buffer where the received data will be stored.

● length (size_t) : Maximum number of bytes to receive.

● flags (int) : Extra options (0 for normal behavior).

● *src_addr (struct sockaddr ) : Stores the sender's address.

● *addrlen (socklen_t ) : The size of src_addr, updated with actual sender


address size.
4. sendto()
The sendto() function is used to send data over UDP (Datagram) sockets to a specific
destination without establishing a connection.
sendto(int sockfd, const void *buffer, size_t length, int flags, const struct sockaddr
*dest_addr, socklen_t addrlen)

● sockfd (int) : The socket file descriptor.

● *buffer (const void ) :Pointer to the data to be sent.

● length (size_t) : Size of the data in bytes.

● flags (int) : Extra options (0 for normal behavior).

● *dest_addr (const struct sockaddr ) :Address of the recipient.

● addrlen (socklen_t) : Size of dest_addr structure.


5. 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);

DEPT. OF CSE CEK 11


CSL 332 NETWORKING LAB

sockfd: Socket descriptor.

Client -side system calls


1. Socket connection: Exactly the same as that of server’s socket creation
2. send() and recv():Exactly same as server.
3. close():Exactly same as server.

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.

DEPT. OF CSE CEK 12


CSL 332 NETWORKING LAB

7. Receive the message from client and store it.


8. Display the message.
9. Close the temporary socket.
10. Stop

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);

DEPT. OF CSE CEK 13


CSL 332 NETWORKING LAB

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)

DEPT. OF CSE CEK 14


CSL 332 NETWORKING LAB

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.

DEPT. OF CSE CEK 15


CSL 332 NETWORKING LAB

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

DEPT. OF CSE CEK 16


CSL 332 NETWORKING LAB

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>

DEPT. OF CSE CEK 17


CSL 332 NETWORKING LAB

#include<netinet/in.h>
#include<netdb.h>

int main(int argc,char* argv[])


{
struct sockaddr_in server,client;
if(argc!=2)
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[1]));
if(bind(sockfd,(struct sockaddr*)&server,sizeof(server))<0)
printf("Error is binding");
char buffer[100];
socklen_t server_len=sizeof(server);
printf("Server waiting.......");
if(recvfrom(sockfd,buffer,100,0,(struct sockaddr*)&server,&server_len)<0)
printf("Error is recvfrom()!");
printf("Got a datagram:%s",buffer);
return 0;
}

OUTPUT

RESULT

DEPT. OF CSE CEK 18


CSL 332 NETWORKING LAB

The program has been executed successfully and output is obtained.

Date: 14/02/2025

EXPERIMENT NO:05
FILE TRANSFER PROTOCOL
AIM: Implement File Transfer Protocol.

OBJECTIVE: To understand TCP-based file transfer system using socket programming in


C.

DEFINITION: A C program demonstrating TCP socket communication where the client


requests a file, and the server transfers its contents for storage in a new file.

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.

DEPT. OF CSE CEK 19


CSL 332 NETWORKING LAB

11. Print “File is transferred”.


12. Close the socket of client.
13. Then exit from the program.
14. Write the file content in rcvg to the destination file.
15. Print the content in rcvg.
16. Stop.
Server
1. Start
2. Create a TCP socket for the server.
3. Create address for the server.
4. Print socket to server address.
5. Set listen for the server socket.
6. Using accept(), point connection is established between client and server.
7. Receive file from client.
8. Open the received file from client in read mode and store in fp.
9. If fp is NULL, go to step 10, otherwise go to step 11.
10. Send error message to client.
11. Repeat step 12-13 while file content not equal to end of file.
12. Send file content to client.
13. Display the file content.
14. If file content equal to end, then send completed message to client.
15. Stop.

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");

DEPT. OF CSE CEK 20


CSL 332 NETWORKING LAB

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);

DEPT. OF CSE CEK 21


CSL 332 NETWORKING LAB

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");

DEPT. OF CSE CEK 22


CSL 332 NETWORKING LAB

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
{

DEPT. OF CSE CEK 23


CSL 332 NETWORKING LAB

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.

DEPT. OF CSE CEK 24


CSL 332 NETWORKING LAB

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.

DEPT. OF CSE CEK 25


CSL 332 NETWORKING LAB

16. Copy MAIL TO to sendbuff.


17. Concatenate TO ADDRESS to sendbuff.
18. Send sendbuff to server.
19. Receive OK from server in recbuff.
20. Compare the recbuff to 250 .If match is found print recbuff ,otherwise print “OR NOT
RECEIVED”.
21. Copy DATA to sendbuff.
22. Send sendbuff to server.
23. Receive response from server and store in recbuff.
24. Compare recbuff to 354.If match found ,print recbuff .Otherwise print “OK NOT
RECEIVED”.
25. Sender mail body.
26. Read content from user and stores it in mailbody.
27. Send mailbody to server.
28. Repeat step 26-27. To step read content user type and store in mailbody and send
mailbody to server.If mailbody is matched with and stop reading from user.
29. Receive OK response from server and store in recbuff .
30. Compare recbuff with 221.If match is found print recbuff.Otherwise print “OK NOT
RECEIVED”.
31. Copy Quit to sendbuff.
32. Send sendbuff to server.
33. Copy “ ” to recbuff.
34. Receive OK response from server and store in recbuff.
35. Compare 221 OK with recbuff.If match is not found print “Exiting…”
36. Close the connection and socket.
37. Stop

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.

DEPT. OF CSE CEK 26


CSL 332 NETWORKING LAB

18. Receive DATA from client and store it in recbuff.


19. Compare recbuff with DATA.If match is found print recbuff otherwise print “DATA
NOT RECEIVED”.
20. Copy 254 GO AHEAD to sendbuff and send it to client.
21. Receive mailbody content from client and store it in mailbody and print the mailbody.
22. Repeat steps 21 until mailbody equals to $.
23. If mailbody equals to $ go to step 24.
24. Copy 221 OK to sendbuff.
25. Send sendbuff to client.
26. Receive QUIT from client and store it in mailbody.
27. If mailbody doesn’t contain QUIT , copy 221 OK to sendbuff and send sendbuff to
client.
28. Close the connection.
29. Stop.

PROGRAM
Client
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>

void check(char strr[],int value)


{
if(value<0)
printf("%s FAILED\n",strr);
}

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);

DEPT. OF CSE CEK 27


CSL 332 NETWORKING LAB

struct sockaddr_in caddr;


caddr.sin_family=AF_INET;
caddr.sin_port=htons(8082);
caddr.sin_addr.s_addr=INADDR_ANY;
size=sizeof(caddr);
con=connect(cid,(struct sockaddr*)&caddr,size);
check("CONNECTION",con);
printf("\n<-->\n");
printf("Sending hi to server\n");
strcpy(sendbuff,"HI");
csend=send(cid,sendbuff,500,0);
check("SENDING",csend);
printf("Wating for server response...\n");
crec=recv(cid,recbuff,500,0);
check("RECEIVE",crec);
printf("Message from server:%s\n",recbuff);
printf("Sending hello to server\n");
strcpy(sendbuff,"HELLO");
csend=send(cid,sendbuff,500,0);
check("SENDING",csend);
printf("Waiting for ok message\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 the from address: ");
scanf("%s",fromaddress);
strcpy(sendbuff,"MAIL FROM:");
strcat(sendbuff,fromaddress);

DEPT. OF CSE CEK 28


CSL 332 NETWORKING LAB

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");

DEPT. OF CSE CEK 29


CSL 332 NETWORKING LAB

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);
}

DEPT. OF CSE CEK 30


CSL 332 NETWORKING LAB

Server
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>

void check(char strr[],int value)


{
if(value<0)
printf("%s FAILED\n",strr);
}

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);

DEPT. OF CSE CEK 31


CSL 332 NETWORKING LAB

printf("Sending response for hi..\n");


strcpy(sendbuff,"220 192.168.9.138");
ssend=send(saccept,sendbuff,500,0);
check("SENDING",ssend);
printf("Waiting for client response..\n");
srec=recv(saccept,recbuff,500,0);
check("RECEIVE",srec);
if(strncmp(recbuff,"HELLO",5))
printf("Error\n");
else
printf("Message from client:%s\n",recbuff);
printf("Sending response for hello..\n");
strcpy(sendbuff,"250 OK");
ssend=send(saccept,sendbuff,500,0);
check("SENDING",ssend);
printf("Waiting for from address\n");
srec=recv(saccept,fromaddress,500,0);
check("RECEIVE",srec);
if(strncmp(fromaddress,"MAIL FROM:",10))
printf("From address error\n");
else
printf("From address:%s\n",fromaddress);
printf("Sending ok to from address\n");
strcpy(sendbuff,"250 OK");
ssend=send(saccept,sendbuff,500,0);
check("SENDING",ssend);
printf("Waiting for to address\n");
srec=recv(saccept,toaddress,500,0);
check("RECEIVE",srec);
if(strncmp(toaddress,"MAIL TO:",8))
printf("To address error\n");
else
printf("To address:%s\n",toaddress);

DEPT. OF CSE CEK 32


CSL 332 NETWORKING LAB

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");

DEPT. OF CSE CEK 33


CSL 332 NETWORKING LAB

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.

DEPT. OF CSE CEK 34


CSL 332 NETWORKING LAB

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.

DEPT. OF CSE CEK 35


CSL 332 NETWORKING LAB

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

DEPT. OF CSE CEK 36


CSL 332 NETWORKING LAB

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.

OBJECTIVE: To understand the functionality of the Distance Vector Routing algorithm in


determining the shortest path between network nodes.

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.

DEPT. OF CSE CEK 37


CSL 332 NETWORKING LAB

7. Set rt[i].dist[j] = cosmat[i][j].


8. Set rt[i].from[j] = j.
9. Set j = j+1.
10. Set i = i+1.
11. Set count = 0.
12. For i = 0 to nodes repeat step 13 to 21.
13. For j = 0 to nodes repeat step 14 to 20.
14. For k = 0 to nodes repeat step 15 to 19.
15. If rt[i].dist[j] > cosmat [i][k] + rt[k].dist[j] then go to step 16. Otherwise go to step 19.
16. Set rt[i].dist[j] = rt[i].dist[k] + rt[k].dist[j].
17. Set rt[i].from[j] = k.
18. Set count = count+1.
19. Set k = k+1.
20. Set j = j+1.
21. Set i = i+1.
22. Repeat step 11 to step 21 until count not equal to zero
23. For i = 0 to nodes repeat step 24 to step 27.
24. For j = 0 to nodes repeat step 25 to step 26.
25. Print rt[i].from[j] , rt[i].dist[j] for j+1 node.
26. Set j = j+1.
27. Set i = i+1.
28. Stop.

PROGRAM

#include<stdio.h>

struct node{

unsigned dist[20];

unsigned from[20];

}rt[10];

int main()

int cosmat[20][20];

int nodes, i,j,k,count=0;

DEPT. OF CSE CEK 38


CSL 332 NETWORKING LAB

printf("enter the no of nodes: \n");

scanf("%d",&nodes);

printf("enter the cosmat matrix: \n");

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++;

DEPT. OF CSE CEK 39


CSL 332 NETWORKING LAB

while(count!=0);

for(i=0;i<nodes;i++)

printf("\n\n For Router %d\n",i+1);

for(j=0;j<nodes;j++)

printf("\t\nNode %d via %d distance %d",j+1,rt[i].from[j]+1,rt[i].dist[j]);

printf("\n\n");

return 0;

OUTPUT

DEPT. OF CSE CEK 40


CSL 332 NETWORKING LAB

RESULT

The program has been executed successfully and output is obtained.

DEPT. OF CSE CEK 41


CSL 332 NETWORKING LAB

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.

PROGRAM OBJECTIVE: To understand a Stop-and-Wait ARQ (Automatic Repeat


request) protocol using socket programming in C.

PROGRAM DEFINITION: A C program that demonstrates reliable data transmission using


the Stop-and-Wait ARQ protocol in a client-server model, ensuring correct packet delivery
with acknowledgment handling.

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

DEPT. OF CSE CEK 42


CSL 332 NETWORKING LAB

7. Create a temporary socket and accept client socket to server socket


8. Set i=0
9. Repeat step 10 to while i<3
10. Receivepacket[i] from client and store it in buf
11. Read acknowledge from user and store it in ack
12. Send ack to client
13. Check whether ack equals 1. If yes go to step 16 otherwise go to step 14
14. Receive packet[i] from client and store it in buf
15. Go to step 11
16. Set i=i+1
17. Close connection
18. Stop

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
{

DEPT. OF CSE CEK 43


CSL 332 NETWORKING LAB

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)

DEPT. OF CSE CEK 44


CSL 332 NETWORKING LAB

{
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)

DEPT. OF CSE CEK 45


CSL 332 NETWORKING LAB

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

The program has been executed successfully and output is obtained.

DEPT. OF CSE CEK 46

You might also like