Question 4.
Exercise 1: TCP Echo Server and Client
Server (tcp_echo_server.c)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#define PORT 8080
#define BUF_SIZE 1024
int main() {
int server_fd, new_socket;
struct sockaddr_in address;
char buffer[BUF_SIZE] = {0};
server_fd = socket(AF_INET, SOCK_STREAM, 0);
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
bind(server_fd, (struct sockaddr *)&address, sizeof(address));
listen(server_fd, 3);
int addrlen = sizeof(address);
new_socket = accept(server_fd, (struct sockaddr *)&address,
(socklen_t*)&addrlen);
read(new_socket, buffer, BUF_SIZE);
printf("Received: %s\n", buffer);
send(new_socket, buffer, strlen(buffer), 0);
close(new_socket);
close(server_fd);
return 0;
}
Client (tcp_echo_client.c)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#define PORT 8080
int main() {
int sock = 0;
struct sockaddr_in serv_addr;
char *msg = "Hello Server!";
char buffer[1024] = {0};
sock = socket(AF_INET, SOCK_STREAM, 0);
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr);
connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
send(sock, msg, strlen(msg), 0);
read(sock, buffer, 1024);
printf("Echo from server: %s\n", buffer);
close(sock);
return 0;
}
Question 5 : Exercises using NS2 Network Simulator
Exercise 1: Setting Up of Various Network TopologiesTCL
Script (Star Topology – Wired)
# Create simulator
set ns [new Simulator]
# Open files
set tracefile [open out.tr w]
$ns trace-all $tracefile
# Create nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
# Create links
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns duplex-link $n0 $n2 1Mb 10ms DropTail
$ns duplex-link $n0 $n3 1Mb 10ms DropTail
# Run simulation
$ns at 1.0 "finish"
proc finish {} {
global ns tracefile
$ns flush-trace
close $tracefile
exit 0
}
$ns run
Exercise 2: Implementation of Various MAC Protocols
set val(chan) Channel/WirelessChannel
set val(prop) Propagation/TwoRayGround
set val(mac) Mac/802_11
set val(ifq) Queue/DropTail/PriQueue
set val(ll) LL
set val(netif) Phy/WirelessPhy
set val(nn) 2
# Simulator and topology
set ns [new Simulator]
set topo [new Topography]
$topo load_flatgrid 500 500
# Create nodes
for {set i 0} {$i < $val(nn)} {incr i} {
set node_($i) [$ns node]
$node_($i) random-motion 0
}
# Link nodes and assign MAC protocol
$ns at 1.0 "$node_(0) setdest 250 250 10"
$ns at 2.0 "$node_(1) setdest 260 260 10"
Exercise 3: Measurement of Routing Protocols
How to Specify Routing Protocol in Wireless
set val(rp) AODV ;# You can change to DSDV or DSR
Exercise 4: Analysis of TCP/IP Protocol under Various
Mechanisms
Sample TCP Setup
set tcp [new Agent/TCP/Reno]
set sink [new Agent/TCPSink]
$ns attach-agent $n0 $tcp
$ns attach-agent $n1 $sink
$ns connect $tcp $sink
Exercise 5: Setup a Network with Application Protocols and
Analyze Performance
Application (FTP over TCP)
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns at 1.0 "$ftp start"
$ns at 5.0 "$ftp stop"
for CBR over UDP:
set udp [new Agent/UDP]
set null [new Agent/Null]
$ns attach-agent $n0 $udp
$ns attach-agent $n1 $null
$ns connect $udp $null
set cbr [new Application/Traffic/CBR]
$cbr set packetSize_ 1000
$cbr set rate_ 1Mb
$cbr attach-agent $udp
$ns at 1.0 "$cbr start"
Question 6: Comparison of TCP/IP, Socket, Pipes. Analyse
which is the best
TCP/IP Communication using Sockets (C)
TCP Server (tcp_server.c)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#define PORT 8080
int main() {
int server_fd, new_socket;
struct sockaddr_in address;
int addrlen = sizeof(address);
char buffer[1024] = {0};
server_fd = socket(AF_INET, SOCK_STREAM, 0);
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
bind(server_fd, (struct sockaddr*)&address, sizeof(address));
listen(server_fd, 3);
printf("Server is waiting for connection...\n");
new_socket = accept(server_fd, (struct sockaddr*)&address,
(socklen_t*)&addrlen);
read(new_socket, buffer, 1024);
printf("Received from client: %s\n", buffer);
send(new_socket, buffer, strlen(buffer), 0);
close(new_socket);
close(server_fd);
return 0;
}
TCP Client (tcp_client.c)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#define PORT 8080
int main() {
int sock = 0;
struct sockaddr_in serv_addr;
char *msg = "Hello from client!";
char buffer[1024] = {0};
sock = socket(AF_INET, SOCK_STREAM, 0);
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr);
connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
send(sock, msg, strlen(msg), 0);
read(sock, buffer, 1024);
printf("Echo from server: %s\n", buffer);
close(sock);
return 0;
}
2. Pipes (Anonymous IPC between Parent and Child)
pipe_example.c
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
int main() {
int pipefd[2];
pid_t pid;
char write_msg[] = "Message from parent";
char read_msg[100];
pipe(pipefd); // create pipe
pid = fork();
if (pid == 0) {
// Child process
close(pipefd[1]); // close write end
read(pipefd[0], read_msg, sizeof(read_msg));
printf("Child received: %s\n", read_msg);
close(pipefd[0]);
} else {
// Parent process
close(pipefd[0]); // close read end
write(pipefd[1], write_msg, strlen(write_msg)+1);
wait(NULL); // wait for child
close(pipefd[1]);
}
return 0;
}