Network Programming Lab Manual (4CS4-23) Credit: 1.5 Max. Marks: 75 (IA: 45, ETE: 30) Prepared By: Dileep Kumar Agarwal
Network Programming Lab Manual (4CS4-23) Credit: 1.5 Max. Marks: 75 (IA: 45, ETE: 30) Prepared By: Dileep Kumar Agarwal
2. Study and Verification of standard Network topologies i.e. Star, Bus, Ring etc.
a. The server listens for, and accepts, a single TCP connection; it reads all
the data it can from that connection, and prints it to the screen; then it
b. The client connects to the server, sends the string “Hello, world!”, then
7. Write an Echo_Client and Echo_server using TCP to estimate the round trip
time from client to the server. The server should be such that it can accept
What is LAN?
A Local Area Network (LAN) is a group of computer and peripheral devices which are
connected in a limited area such as school, laboratory, home, and office building. It is a widely
useful network for sharing resources like files, printers, games, and other application. The
simplest type of LAN network is to connect computers and a printer in someone's home or
office. In general, LAN will be used as one type of transmission medium.
It is a network which consists of less than 5000 interconnected devices across several buildings.
Characteristics of LAN
Advantages of LAN
• Computer resources like hard-disks, DVD-ROM, and printers can share local area
networks. This significantly reduces the cost of hardware purchases.
• You can use the same software over the network instead of purchasing the licensed
software for each client in the network.
• Data of all network users can be stored on a single hard disk of the server computer.
• You can easily transfer data and messages over networked computers.
• It will be easy to manage data at only one place, which makes data more secure.
• Local Area Network offers the facility to share a single internet connection among all the
LAN users.
Disadvantages of LAN
• LAN will indeed save cost because of shared computer resources, but the initial cost of
installing Local Area Networks is quite high.
• The LAN admin can check personal data files of every LAN user, so it does not offer
good privacy.
• Unauthorized users can access critical data of an organization in case LAN admin is not
able to secure centralized data repository.
• Local Area Network requires a constant LAN administration as there are issues related to
software setup and hardware failures
• Hubs provide a central location for attaching wires to workstations. There are two types:
passive and active.
• Switches connect devices to host computers and allow large numbers of these devices to
share a limited number of ports.
• Routers are protocol-dependent devices that connect sub-networks or divide a very large
network into smaller sub-networks.
• Repeaters use regeneration and retiming to ensure that signals are transmitted clearly
through all network segments.
• Bridges are used to interconnect local or remote networks. They centralize network
administration.
• Gateways can interconnect networks with different, incompatible communications protocols.
• Multiplexers combine multiple signal inputs into one output.
• Transceivers connect nodes and send and receive signals. They are sometimes called
medium access units (MAU).
• Firewalls safeguard a network against unauthorized access.
Other network devices such as wireless access points (WAP) and modular platforms are also
available.
Network equipment may be designed or suitable for particular applications. For example,
hardened products are often used in telecommunications applications. Their casings provide
protection from weather-related conditions and can act as a heat sink, directing high temperatures
away from sensitive components.
2. Study and Verification of standard Network topologies: Star, Bus, Ring etc.
BUS Topology
Bus topology is a network type in which every computer and network device is connected to
single cable. When it has exactly two endpoints, then it is called Linear Bus topology.
Features of Bus Topology
1. It is cost effective.
2. Cable required is least compared to other network topology.
4. It is easy to understand.
RING Topology
It is called ring topology because it forms a ring as each computer is connected to another
computer, with the last one connected to the first. Exactly two neighbours for each device.
Features of Ring Topology
1. A number of repeaters are used for Ring topology with large number of nodes, because if
someone wants to send some data to the last node in the ring topology with 100 nodes, then the
data will have to pass through 99 nodes to reach the 100th node. Hence to prevent data loss
3. In Dual Ring Topology, two ring networks are formed, and data flow is in opposite direction in
them. Also, if one ring fails, the second ring can act as a backup, to keep the network up.
4. Data is transferred in a sequential manner that is bit by bit. Data transmitted, has to pass through
1. Transmitting network is not affected by high traffic or by adding more nodes, as only the nodes
having tokens can transmit data.
STAR Topology
In this type of topology all the computers are connected to a single hub through a cable. This hub
is the central node and all others nodes are connected to the central node.
3. Easy to troubleshoot.
5. Only that node is affected which has failed, rest of the nodes can work smoothly.
3. If the hub fails then the whole network is stopped because all the nodes depend on the hub.
MESH Topology
It is a point-to-point connection to other nodes or devices. All the network nodes are connected
to each other. Mesh has n(n-1)/2 physical channels to link n devices.
There are two techniques to transmit data over the Mesh topology, they are :
1. Routing
2. Flooding
1. Partial Mesh Topology : In this topology some of the systems are connected in the same fashion
as mesh topology but some devices are only connected to two or three devices.
2. Full Mesh Topology : Each and every nodes or devices are connected to each other.
1. Fully connected.
2. Robust.
3. Not flexible.
TREE Topology
It has a root node and all other nodes are connected to it forming a hierarchy. It is also called
hierarchical topology. It should at least have three levels to the hierarchy.
1. Heavily cabled.
2. Costly.
Step 2. In the LAN IP Address field, enter the IP address of the SPA3102 on the
local network.
Step 3. From the LAN Subnet Mask drop-down list, choose the subnet that
correlates to the LAN IP Address in Step 2.
Step 4. From the Enable DHCP Server drop-down list, choose Yes to enable the
dynamic host configuration protocol which will dynamically assign IP address to
devices on the LAN, or choose No to have devices given static IP addresses.
Note: If you choose No, skip to Step 8.
Step 5. In the DHCP Lease Time field, enter the number, in hours, that you wish an
IP address to be assigned to a device before being renewed.
Step 6. In the DHCP Client Starting IP Address field, enter the beginning IP address
of the client devices that will be assigned a dynamic IP address.
Step 7. In the Number of Client IP Addresses field, enter the number of client
devices that you wish to be able to connect to the local network.
Step 8. Click Save Settings to save the settings or click Cancel Settings to discard
the settings.
#include<stdio.h>
void main() {
int data[10];
int dataatrec[10],c,c1,c2,c3,i;
if(c==0) {
printf("\nNo error while transmission of data\n");
}
else {
printf("\nError on position %d",c);
for (i=0;i<7;i++) {
printf("%d",dataatrec[i]);
}
}
}
#include<conio.h>
#include<string.h>
void main()
int a[20],b[30],i,j,k,count,n;
scanf("%d",&n);
printf("Enter the frame in the form of 0 and 1 :");
scanf("%d",&a[i]);
i=0;
count=1;
j=0;
while(i<n)
if(a[i]==1)
b[j]=a[i];
j++;
b[j]=a[k];
count++;
if(count==5)
j++;
b[j]=0;
i=k;
else
{
b[j]=a[i];
i++;
j++;
printf("%d",b[i]);
getch();
Client Code
/****************** CLIENT CODE ****************/
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int main(){
int clientSocket;
char buffer[1024];
struct sockaddr_in serverAddr;
socklen_t addr_size;
serverAddr.sin_family = AF_INET;
/* Set port number, using htons function to use proper byte order */
serverAddr.sin_port = htons(7891);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
/*---- Connect the socket to the server using the address struct ----*/
/*---- Read the message from the server into the buffer ----*/
return 0;
}
Server Code
/****************** SERVER CODE ****************/
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int main(){
char buffer[1024];
socklen_t addr_size;
serverAddr.sin_family = AF_INET;
/* Set port number, using htons function to use proper byte order */
serverAddr.sin_port = htons(7891);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
/* Set all bits of the padding field to 0 */
/*---- Listen on the socket, with 5 max connection requests queued ----*/
if(listen(welcomeSocket,5)==0)
printf("Listening\n");
else
printf("Error\n");
/*---- Accept call creates a new socket for the incoming connection ----*/
strcpy(buffer,"Hello World\n");
send(newSocket,buffer,13,0);
return 0;
Echo Client
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include<string.h>
sockfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof servaddr);
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(22000);
inet_pton(AF_INET,"127.0.0.1",&(servaddr.sin_addr));
while(1)
{
bzero( sendline, 100);
bzero( recvline, 100);
fgets(sendline,100,stdin); /*stdin = 0 , for standard input */
write(sockfd,sendline,strlen(sendline)+1);
read(sockfd,recvline,100);
printf("%s",recvline);
}
Echo Server
Echo Server is a device which sends back whatever data it has received
/*Required Headers*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include<string.h>
int main()
char str[100];
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htons(INADDR_ANY);
servaddr.sin_port = htons(22000);
listen(listen_fd, 10);
while(1)
read(comm_fd,str,100);
printf("Echoing back - %s",str);
UDP_client.c
// UDP client program
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/socket.h>
#include <sys/types.h>
#define PORT 5000
#define MAXLINE 1024
int main()
{
int sockfd;
char buffer[MAXLINE];
char* message = "Hello Server";
struct sockaddr_in servaddr;
int n, len;
// Creating socket file descriptor
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
printf("socket creation failed");
exit(0);
}
memset(&servaddr, 0, sizeof(servaddr));
#include "unp.h"
void
str_cli(FILE *fp, int sockfd)
{
int maxfdp1;
fd_set rset;
char sendline[MAXLINE], recvline[MAXLINE];
FD_ZERO(&rset);
for ( ; ; ) {
FD_SET(fileno(fp), &rset);
FD_SET(sockfd, &rset);
maxfdp1 = max(fileno(fp), sockfd) + 1;
Select(maxfdp1, &rset, NULL, NULL, NULL);
#include <stdio.h>
#include <stdlib.h>
int main(void) {
//create graph
struct Graph *g = (struct Graph*)malloc(sizeof(struct Graph));
g->V = 4; //total vertices
g->E = 5; //total edges
//array of edges for graph
g->edge = (struct Edge*)malloc(g->E * sizeof(struct Edge));
//edge 0 --> 1
g->edge[0].u = 0;
g->edge[0].v = 1;
g->edge[0].w = 5;
//edge 0 --> 2
g->edge[1].u = 0;
g->edge[1].v = 2;
g->edge[1].w = 4;
//edge 1 --> 3
g->edge[2].u = 1;
g->edge[2].v = 3;
g->edge[2].w = 3;
//edge 2 --> 1
g->edge[3].u = 2;
g->edge[3].v = 1;
g->edge[3].w = -6;
//edge 3 --> 2
g->edge[4].u = 3;
g->edge[4].v = 2;
g->edge[4].w = 2;
return 0;
}
//distance array
//size equal to the number of vertices of the graph g
int d[tV];
//predecessor array
//size equal to the number of vertices of the graph g
int p[tV];