Network Programming Laboratory
Program 1
Write a C program to implement daytime client/server program using TCP
sockets
Server Code (daytime_server.c)
// File: daytime_server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 8080
#define BUFFER_SIZE 1024
int main() {
int server_fd, new_socket;
struct sockaddr_in address;
int addrlen = sizeof(address);
char buffer[BUFFER_SIZE];
time_t current_time;
// Creating socket file descriptor
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd == 0) {
perror("Socket failed");
exit(EXIT_FAILURE);
Dept of CSE, SIET 1
Network Programming Laboratory
// Define server address
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY; // Bind to all interfaces
address.sin_port = htons(PORT);
// Bind the socket
if (bind(server_fd, (struct sockaddr*)&address, sizeof(address)) < 0) {
perror("Bind failed");
close(server_fd);
exit(EXIT_FAILURE);
}
// Listen for incoming connections
if (listen(server_fd, 3) < 0) {
perror("Listen failed");
close(server_fd);
exit(EXIT_FAILURE);
}
printf("Daytime server is running on port %d...\n", PORT);
// Accept a client connection
new_socket = accept(server_fd, (struct sockaddr*)&address, (socklen_t*)&addrlen);
if (new_socket < 0) {
perror("Accept failed");
close(server_fd);
exit(EXIT_FAILURE);
Dept of CSE, SIET 2
Network Programming Laboratory
// Get current time
current_time = time(NULL);
snprintf(buffer, BUFFER_SIZE, "Current Date and Time: %s", ctime(¤t_time));
// Send the time to the client
send(new_socket, buffer, strlen(buffer), 0);
printf("Time sent to client: %s", buffer);
// Close sockets
close(new_socket);
close(server_fd);
return 0;
Client Code (daytime_client.c)
// File: daytime_client.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define SERVER_IP "127.0.0.1"
#define PORT 8080
#define BUFFER_SIZE 1024
Dept of CSE, SIET 3
Network Programming Laboratory
int main() {
int sock = 0;
struct sockaddr_in serv_addr;
char buffer[BUFFER_SIZE] = {0};
// Create socket
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror("Socket creation error");
return -1;
}
// Set server address
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 address from text to binary
if (inet_pton(AF_INET, SERVER_IP, &serv_addr.sin_addr) <= 0) {
printf("Invalid address/Address not supported\n");
return -1;
// Connect to server
if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
perror("Connection Failed");
return -1;
Dept of CSE, SIET 4
Network Programming Laboratory
// Read server response
read(sock, buffer, BUFFER_SIZE);
printf("Server Response: %s\n", buffer);
// Close socket
close(sock);
return 0;
Compile & Run:
gcc daytime_server.c -o server
gcc daytime_client.c -o client
./server
Output:
Daytime server is running on port 8080...
./client
Output:
Server Response: Current Date and Time: Tue Jul 1 19:30:42 2025
Dept of CSE, SIET 5
Network Programming Laboratory
Program 2
Write a TCP client/server program in which client sends three numbers to
the server in a single message. Server returns sum, difference and product as
a result single message. Client program should print the results
appropriately
Server Code: tcp_server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <unistd.h>
#define PORT 8080
int main() {
int server_fd, new_socket;
struct sockaddr_in address;
int addrlen = sizeof(address);
int numbers[3], result[3]; // sum, difference, product
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd == 0) {
perror("Socket failed");
exit(EXIT_FAILURE);
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
Dept of CSE, SIET 6
Network Programming Laboratory
bind(server_fd, (struct sockaddr *)&address, sizeof(address));
listen(server_fd, 3);
printf("Server is listening...\n");
new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen);
read(new_socket, numbers, sizeof(numbers));
printf("Received numbers: %d, %d, %d\n", numbers[0], numbers[1], numbers[2]);
result[0] = numbers[0] + numbers[1] + numbers[2];
result[1] = numbers[0] - numbers[1] - numbers[2];
result[2] = numbers[0] * numbers[1] * numbers[2];
write(new_socket, result, sizeof(result));
printf("Results sent back to client.\n");
close(new_socket);
close(server_fd);
return 0;
Client Code: tcp_client.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <unistd.h>
Dept of CSE, SIET 7
Network Programming Laboratory
#define PORT 8080
int main() {
int sock = 0;
struct sockaddr_in serv_addr;
int numbers[3], result[3];
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
printf("\nSocket creation error\n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
serv_addr.sin_addr.s_addr = INADDR_ANY;
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
printf("\nConnection Failed\n");
return -1;
printf("Enter three integers: ");
scanf("%d %d %d", &numbers[0], &numbers[1], &numbers[2]);
write(sock, numbers, sizeof(numbers));
read(sock, result, sizeof(result));
Dept of CSE, SIET 8
Network Programming Laboratory
printf("Sum = %d\n", result[0]);
printf("Difference = %d\n", result[1]);
printf("Product = %d\n", result[2]);
close(sock);
return 0;
Compile and Run:
gcc tcp_server.c -o server
gcc tcp_client.c -o client
./server
Output:
Server is listening...
Received numbers: 5, 3, 2
Results sent back to client.
./client
Output:
Enter three integers: 5 3 2
Sum = 10
Difference = 0
Product = 30
Dept of CSE, SIET 9
Network Programming Laboratory
Program 3
Write a C program that prints the IP layer and TCP layer socket options in
a separate file
Code: socket_options.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
int main() {
int sockfd;
int optval;
socklen_t optlen = sizeof(optval);
FILE *fp = fopen("socket_options.txt", "w");
if (fp == NULL) {
perror("File open error");
exit(EXIT_FAILURE);
// Create a TCP socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
Dept of CSE, SIET 10
Network Programming Laboratory
fprintf(fp, "---- Socket Options ----\n");
// IP Layer Options
if (getsockopt(sockfd, IPPROTO_IP, IP_TTL, &optval, &optlen) == 0) {
fprintf(fp, "IP_TTL: %d\n", optval);
if (getsockopt(sockfd, IPPROTO_IP, IP_HDRINCL, &optval, &optlen) == 0) {
fprintf(fp, "IP_HDRINCL: %d\n", optval);
}
// TCP Layer Options
if (getsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &optval, &optlen) == 0) {
fprintf(fp, "TCP_NODELAY: %d\n", optval);
if (getsockopt(sockfd, IPPROTO_TCP, TCP_MAXSEG, &optval, &optlen) == 0) {
fprintf(fp, "TCP_MAXSEG: %d\n", optval);
fclose(fp);
close(sockfd);
printf("Socket options written to socket_options.txt\n");
return 0;
Dept of CSE, SIET 11
Network Programming Laboratory
Compile and Run:
gcc socket_options.c -o socket_options
./socket_options
Output:
---- Socket Options ----
IP_TTL: 64
IP_HDRINCL: 0
TCP_NODELAY: 0
TCP_MAXSEG: 536
Dept of CSE, SIET 12
Network Programming Laboratory
Program 4
Exercises on Socket Programming using C and Java
tcp_server.c (Echo Server)
#include <stdio.h>
#include <string.h>
#include <netinet/in.h>
#include <unistd.h>
#define PORT 8080
int main() {
int server_fd, new_socket;
char buffer[1024];
struct sockaddr_in address;
int addrlen = sizeof(address);
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 listening...\n");
new_socket = accept(server_fd, (struct sockaddr*)&address, (socklen_t*)&addrlen);
read(new_socket, buffer, 1024);
printf("Received: %s\n", buffer);
Dept of CSE, SIET 13
Network Programming Laboratory
send(new_socket, buffer, strlen(buffer), 0);
printf("Echoed back to client.\n");
close(new_socket);
close(server_fd);
return 0;
tcp_client.c (Client)
#include <stdio.h>
#include <string.h>
#include <netinet/in.h>
#include <unistd.h>
#define PORT 8080
int main() {
int sock;
struct sockaddr_in serv_addr;
char buffer[1024];
sock = socket(AF_INET, SOCK_STREAM, 0);
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
serv_addr.sin_addr.s_addr = INADDR_ANY;
connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
printf("Enter message: ");
fgets(buffer, sizeof(buffer), stdin);
Dept of CSE, SIET 14
Network Programming Laboratory
send(sock, buffer, strlen(buffer), 0);
memset(buffer, 0, sizeof(buffer));
read(sock, buffer, sizeof(buffer));
printf("Server replied: %s\n", buffer);
close(sock);
return 0;
Output:
Client:
Enter message: Hello
Server replied: Hello
Server:
Server listening...
Received: Hello
Echoed back to client.
TCPServer.java
import java.io.*;
import java.net.*;
public class TCPServer {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(8080);
System.out.println("Server is running...");
Socket s = ss.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
Dept of CSE, SIET 15
Network Programming Laboratory
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
String msg = in.readLine();
System.out.println("Received: " + msg);
out.println(msg);
ss.close();
TCPClient.java
import java.io.*;
import java.net.*;
public class TCPClient {
public static void main(String[] args) throws IOException {
Socket s = new Socket("localhost", 8080);
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
System.out.print("Enter message: ");
String msg = userInput.readLine();
out.println(msg);
String response = in.readLine();
System.out.println("Server replied: " + response);
s.close();
Dept of CSE, SIET 16
Network Programming Laboratory
Output:
Client:
Enter message: Hello Java
Server replied: Hello Java
Server:
Server is running...
Received: Hello Java
Dept of CSE, SIET 17
Network Programming Laboratory
Program 5
Exercises using OPNET Network Simulator
1. Setting up of various network topologies
2. Implementation of various MAC protocols
3. Measurement of routing protocols
4. Analysis of TCP/IP protocol under various mechanisms
5. Setting up of network that carries various application protocols and analyzing
the performances
1. Setting up of various network topologies
#include <stdio.h>
#include <netinet/in.h>
#include <unistd.h>
int main() {
int s = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in a = {AF_INET, htons(1234), INADDR_ANY};
bind(s, (void*)&a, sizeof a); listen(s, 1);
int c = accept(s, 0, 0); char b[20];
read(c, b, 20); printf("Got: %s\n", b);
write(c, "Hi Client", 10); close(c); close(s);
}
Output:
Got: Hello Server
2. Implementation of various MAC protocols
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int i, attempt = 0;
srand(time(0));
while (attempt < 10) {
int channel = rand() % 2; // 0: idle, 1: busy
if (channel == 0) {
Dept of CSE, SIET 18
Network Programming Laboratory
printf("Attempt %d: Channel idle. Data sent.\n", attempt + 1);
break;
} else {
printf("Attempt %d: Channel busy. Backing off.\n", attempt + 1);
int wait = rand() % 5;
attempt++;
}
}
if (attempt == 10)
printf("Transmission failed after 10 attempts.\n");
return 0;
}
Output:
Attempt 1: Channel busy. Backing off.
Attempt 2: Channel idle. Data sent.
3. Measurement of routing protocols
#include <stdio.h>
#include <string.h>
struct Route {
char dest[20];
char nextHop[20];
};
int main() {
struct Route table[3] = {
{"192.168.1.0", "Router A"},
{"10.0.0.0", "Router B"},
{"172.16.0.0", "Router C"}
};
char packetDest[20];
Dept of CSE, SIET 19
Network Programming Laboratory
printf("Enter packet destination IP: ");
scanf("%s", packetDest);
int found = 0;
for (int i = 0; i < 3; i++) {
if (strncmp(packetDest, table[i].dest, strlen(table[i].dest)) == 0) {
printf("Forward packet via %s\n", table[i].nextHop);
found = 1;
break;
if (!found)
printf("No route found. Dropping packet.\n");
return 0;
Output:
Enter packet destination IP: 10.0.0.12
Forward packet via Router B
3. Analysis of TCP/IP protocol under various mechanisms
#include <stdio.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
int main() {
int sock = socket(AF_INET, SOCK_STREAM, 0);
Dept of CSE, SIET 20
Network Programming Laboratory
struct sockaddr_in addr = {AF_INET, htons(1234), INADDR_ANY};
bind(sock, (struct sockaddr*)&addr, sizeof(addr));
listen(sock, 1);
int client = accept(sock, NULL, NULL);
char msg[100];
read(client, msg, sizeof(msg));
printf("Server received: %s\n", msg);
write(client, "ACK", 4);
close(client);
close(sock);
return 0;
Compile and Run:
gcc tcp_server.c -o server
./server
Output:
Server received: Hello TCP
5. Setting up of network that carries various application protocols and analyzing
the performances
#include <stdio.h>
#include <string.h>
#include <netinet/in.h>
#include <unistd.h>
int main() {
int s = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr = {AF_INET, htons(8080), INADDR_ANY};
Dept of CSE, SIET 21
Network Programming Laboratory
bind(s, (struct sockaddr*)&addr, sizeof(addr));
listen(s, 1);
int c = accept(s, NULL, NULL);
char req[1024];
read(c, req, sizeof(req));
printf("Received request:\n%s\n", req);
char resp[] = "HTTP/1.1 200 OK\nContent-Length: 13\n\nHello, Client!";
write(c, resp, strlen(resp));
close(c);
close(s);
return 0;
Compile and Run:
gcc http_server.c -o server
./server
Output:
Received request:
GET / HTTP/1.1
Host: localhost
Dept of CSE, SIET 22
Network Programming Laboratory
Program 6
Comparison of TCP/IP, Socket, Pipes. Analyse which is the best
A. TCP/IP (Socket over Network)
tcp_server.c
#include <stdio.h>
#include <string.h>
#include <netinet/in.h>
#include <unistd.h>
#define PORT 8080
int main() {
int sockfd, newsock;
struct sockaddr_in addr;
char buffer[1024] = {0};
sockfd = socket(AF_INET, SOCK_STREAM, 0);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(PORT);
bind(sockfd, (struct sockaddr*)&addr, sizeof(addr));
listen(sockfd, 3);
printf("TCP Server waiting...\n");
newsock = accept(sockfd, NULL, NULL);
read(newsock, buffer, sizeof(buffer));
printf("Server received: %s\n", buffer);
send(newsock, buffer, strlen(buffer), 0);
close(newsock);
Dept of CSE, SIET 23
Network Programming Laboratory
close(sockfd);
return 0;
Output
Server: Server received: Hello TCP
B. Socket
server.c
#include <stdio.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
int main() {
int s = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in server = {AF_INET, htons(1234), INADDR_ANY};
bind(s, (struct sockaddr*)&server, sizeof(server));
listen(s, 1);
int c = accept(s, NULL, NULL);
char msg[100];
read(c, msg, sizeof(msg));
printf("Server got: %s\n", msg);
write(c, "Hi from server", 15);
close(c);
close(s);
return 0;
}
Dept of CSE, SIET 24
Network Programming Laboratory
Compile and Run
gcc server.c -o server
./server
Output:
Server got: Hello server
C. Pipe
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main() {
int fd[2];
char write_msg[] = "Hello from parent";
char read_msg[100];
pipe(fd);
if (fork() == 0) {
// Child
close(fd[1]); // Close write end
read(fd[0], read_msg, sizeof(read_msg));
printf("Child received: %s\n", read_msg);
} else {
// Parent
close(fd[0]); // Close read end
write(fd[1], write_msg, strlen(write_msg)+1);
return 0;
}
Output:
Child received: Hello from parent
Dept of CSE, SIET 25