0% found this document useful (0 votes)
296 views27 pages

Mtech Network Programming Lab Manual MSCSL207

The document outlines the syllabus for the Network Programming Laboratory course at Impact College of Engineering & Applied Sciences, detailing various programming exercises using C and Java. Key tasks include implementing TCP client/server programs for time retrieval and arithmetic operations, as well as socket option exploration and chat applications. The document is compiled by faculty members and includes code examples and instructions for students.

Uploaded by

faize ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
296 views27 pages

Mtech Network Programming Lab Manual MSCSL207

The document outlines the syllabus for the Network Programming Laboratory course at Impact College of Engineering & Applied Sciences, detailing various programming exercises using C and Java. Key tasks include implementing TCP client/server programs for time retrieval and arithmetic operations, as well as socket option exploration and chat applications. The document is compiled by faculty members and includes code examples and instructions for students.

Uploaded by

faize ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

IMPACT COLLEGE OF ENGINEERING &

APPLIED SCIENCES
Kodigehalli Post, Bangalore – 560 092

NETWORK PROGRAMMING LABORATORY


(MSCSL207)

(As per Visvesvaraya Technological University Syllabus)

Compiled by:

Prof. VIJENDRA SN Dr. DHANANJAYA V


Asst. Professor Prof & Head of Department
Dept. of CS & E Dept. of CS & E

NAME :
USN :

Department of CompUter SCIenCe anD enGIneerInG

2025
NETWORK PROGRAMMING LABORATORY MSCSL207

SL PAGE
TITLE
NO NO.
Write a C program to implement daytime client/server program using
1. 3
TCP sockets.

Write a TCP client/server program in which client sends three numbers


to the server in a single message. Server returns sum, difference and
2.
product as a result single message. Client program should print the
results appropriately.

Write a C program that prints the IP layer and TCP layer socket options
3.
in a separate file.

4. Exercises on Socket Programming using C and Java.

Exercises using OPNET Network Simulator


1. Setting up of various network topologies
2. Implementation of various MAC protocols
5. 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.

6. Comparison of TCP/IP, Socket, Pipes. Analyse which is the best.

Vijendra SN, Dept. of CS&E, ICEAS 2


NETWORK PROGRAMMING LABORATORY MSCSL207

1. Write a C program to implement daytime client/server program using TCP sockets.

SERVER SIDE PROGRAM:

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<time.h>

#define BACKLOG 10

int main (int argc, char **argv)


{
if(argc!=2)
{
printf("Usage:%s <post> \n",argv[0]);
exit(0);
}

int port = atoi(argv[1]);


printf("Port:%d\n",port);

int n_client = 0;
int sockfd = socket(AF_INET, SOCK_STREAM,0);
struct sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = INADDR_ANY;
serverAddress.sin_port = htons(port);

bind(sockfd,(struct sockaddr *)&serverAddress,sizeof(serverAddress));

printf("[+]Bind\n");

listen(sockfd,BACKLOG);
printf("[+]Listening for the client\n");

Vijendra SN, Dept. of CS&E, ICEAS 3


NETWORK PROGRAMMING LABORATORY MSCSL207

int i=1;
while(i)
{
int client_socket = accept(sockfd,NULL,NULL);
n_client++;
time_t currentTime;
time(&currentTime);
printf("Client %d requested for time at %s",n_client,ctime(&currentTime));
send(client_socket, ctime(&currentTime),40,0);
}
return 0;

CLIENT SIDE PROGRAM:

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<time.h>

int main (int argc, char **argv)


{
if(argc!=2)
{
printf("Usage:%s <post> \n",argv[0]);
exit(0);
}

int port = atoi(argv[1]);


printf("Port:%d\n",port);

int sockfd = socket(AF_INET, SOCK_STREAM,0);


char response[30];

Vijendra SN, Dept. of CS&E, ICEAS 4


NETWORK PROGRAMMING LABORATORY MSCSL207

struct sockaddr_in serverAddress;


serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = INADDR_ANY;
serverAddress.sin_port = htons(port);

connect(sockfd,(struct sockaddr*)&serverAddress,sizeof(serverAddress));

printf("[+] Connected to the server\n");

recv(sockfd,response,29,0);

printf("Time from server: %s",response);

return 0;

Vijendra SN, Dept. of CS&E, ICEAS 5


NETWORK PROGRAMMING LABORATORY MSCSL207

OUTPUT: SERVER SIDE

OUTPUT: CLIENT SIDE

Vijendra SN, Dept. of CS&E, ICEAS 6


NETWORK PROGRAMMING LABORATORY MSCSL207

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 Side Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.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] = {0};

// Create socket file descriptor


if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}

address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);

// Bind the socket to the specified IP and port


if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}

// Listen for incoming connections


if (listen(server_fd, 3) < 0) {

Vijendra SN, Dept. of CS&E, ICEAS 7


NETWORK PROGRAMMING LABORATORY MSCSL207

perror("listen");
exit(EXIT_FAILURE);
}

printf("Server listening on port %d\n", PORT);

// Accept a client connection


if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}

printf("Client connected.\n");

// Read numbers from client


read(new_socket, buffer, BUFFER_SIZE);
int num1, num2, num3;
sscanf(buffer, "%d %d %d", &num1, &num2, &num3);

// Calculate sum, difference, and product


int sum = num1 + num2 + num3;
int difference = num1 - num2 - num3; // Example: num1 - (num2 + num3)
int product = num1 * num2 * num3;

// Prepare response message


sprintf(buffer, "Sum: %d, Difference: %d, Product: %d", sum, difference, product);

// Send results back to client


send(new_socket, buffer, strlen(buffer), 0);
printf("Results sent to client.\n");

// Close sockets
close(new_socket);
close(server_fd);

return 0;
}

Vijendra SN, Dept. of CS&E, ICEAS 8


NETWORK PROGRAMMING LABORATORY MSCSL207

Client Side Program:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

#define PORT 8080


#define BUFFER_SIZE 1024

int main() {
int sock = 0;
struct sockaddr_in serv_addr;
char buffer[BUFFER_SIZE] = {0};
int num1, num2, num3;

// Create socket file descriptor


if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("Socket creation error");
exit(EXIT_FAILURE);
}

serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);

// Convert IPv4 and IPv6 addresses from text to binary form


if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
perror("Invalid address/ Address not supported");
exit(EXIT_FAILURE);
}

// Connect to the server


if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
perror("Connection Failed");
exit(EXIT_FAILURE);
}

Vijendra SN, Dept. of CS&E, ICEAS 9


NETWORK PROGRAMMING LABORATORY MSCSL207

printf("Enter three numbers (space-separated): ");


scanf("%d %d %d", &num1, &num2, &num3);

// Send numbers to server


sprintf(buffer, "%d %d %d", num1, num2, num3);
send(sock, buffer, strlen(buffer), 0);
printf("Numbers sent to server.\n");

// Read response from server


read(sock, buffer, BUFFER_SIZE);
printf("Received from server: %s\n", buffer);

// Close socket
close(sock);

return 0;
}

Vijendra SN, Dept. of CS&E, ICEAS 10


NETWORK PROGRAMMING LABORATORY MSCSL207

OUTPUT: SERVER SIDE

OUTPUT: CLIENT SIDE

Vijendra SN, Dept. of CS&E, ICEAS 11


NETWORK PROGRAMMING LABORATORY MSCSL207

3. Write a C program that prints the IP layer and TCP layer socket options in a separate file.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h> // For TCP_NODELAY, TCP_KEEPALIVE, etc.
#include <errno.h>

void print_socket_option(FILE *fp, int sockfd, int level, int optname, const char *optname_str)
{
int optval;
socklen_t optlen = sizeof(optval);

if (getsockopt(sockfd, level, optname, &optval, &optlen) == 0) {


fprintf(fp, " %s: %d\n", optname_str, optval);
} else {
fprintf(fp, " Error getting %s: %s\n", optname_str, strerror(errno));
}
}

int main() {
int sockfd;
FILE *output_file;

// Create a TCP socket


sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("Error creating socket");
return 1;
}

// Open a file to write the options


output_file = fopen("socket_options.txt", "w");

Vijendra SN, Dept. of CS&E, ICEAS 12


NETWORK PROGRAMMING LABORATORY MSCSL207

if (output_file == NULL) {
perror("Error opening output file");
close(sockfd);
return 1;
}

fprintf(output_file, "--- IP Layer Socket Options ---\n");


print_socket_option(output_file, sockfd, IPPROTO_IP, IP_TOS, "IP_TOS");
print_socket_option(output_file, sockfd, IPPROTO_IP, IP_TTL, "IP_TTL");
// Add more IP options as needed

fprintf(output_file, "\n--- TCP Layer Socket Options ---\n");


print_socket_option(output_file, sockfd, IPPROTO_TCP, TCP_NODELAY, "TCP_NODELAY");
print_socket_option(output_file, sockfd, IPPROTO_TCP, TCP_KEEPALIVE, "TCP_KEEPALIVE");
// Add more TCP options as needed

fclose(output_file);
close(sockfd);
printf("Socket options written to socket_options.txt\n");
return 0;
}

Vijendra SN, Dept. of CS&E, ICEAS 13


NETWORK PROGRAMMING LABORATORY MSCSL207

OUTPUT:

Vijendra SN, Dept. of CS&E, ICEAS 14


NETWORK PROGRAMMING LABORATORY MSCSL207

4. Exercises on Socket Programming using C and Java. (Simple Chat Application using C)
SERVER SIDE PROGRAM
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>

#define PORT 8080


#define BUFFER_SIZE 1024

int main() {
int server_fd, new_socket;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[BUFFER_SIZE] = {0};

// Create socket file descriptor


if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}

// Attach socket to the port 8080


if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);

// Bind the socket to the specified IP and port


if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);

Vijendra SN, Dept. of CS&E, ICEAS 15


NETWORK PROGRAMMING LABORATORY MSCSL207

// Listen for incoming connections


if (listen(server_fd, 3) < 0) { // Max 3 pending connections
perror("listen");
exit(EXIT_FAILURE);
}

printf("Server listening on port %d...\n", PORT);

// Accept a client connection


if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}

printf("Client connected.\n");

while (1) {
// Read message from client
int valread = read(new_socket, buffer, BUFFER_SIZE);
if (valread <= 0) {
printf("Client disconnected or error.\n");
break;
}
printf("Client: %s\n", buffer);
memset(buffer, 0, BUFFER_SIZE); // Clear buffer

// Send message to client


printf("Server: ");
fgets(buffer, BUFFER_SIZE, stdin);
send(new_socket, buffer, strlen(buffer), 0);
memset(buffer, 0, BUFFER_SIZE); // Clear buffer
}
close(new_socket);
close(server_fd);
return 0;
}

Vijendra SN, Dept. of CS&E, ICEAS 16


NETWORK PROGRAMMING LABORATORY MSCSL207

CLIENT SIDE PROGRAM


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>

#define PORT 8080


#define BUFFER_SIZE 1024

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


int sock = 0;
struct sockaddr_in serv_addr;
char buffer[BUFFER_SIZE] = {0};

// Create socket file descriptor


if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\n Socket creation error \n");
return -1;
}

serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);

// Convert IPv4 and IPv6 addresses from text to binary form


if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) { // Connect to localhost
printf("\nInvalid address/ Address not supported \n");
return -1;
}

// Connect to the server


if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
printf("\nConnection Failed \n");
return -1;
}

printf("Connected to server.\n");

Vijendra SN, Dept. of CS&E, ICEAS 17


NETWORK PROGRAMMING LABORATORY MSCSL207

while (1) {
// Send message to server
printf("Client: ");
fgets(buffer, BUFFER_SIZE, stdin);
send(sock, buffer, strlen(buffer), 0);
memset(buffer, 0, BUFFER_SIZE); // Clear buffer

// Read message from server


int valread = read(sock, buffer, BUFFER_SIZE);
if (valread <= 0) {
printf("Server disconnected or error.\n");
break;
}
printf("Server: %s\n", buffer);
memset(buffer, 0, BUFFER_SIZE); // Clear buffer
}

close(sock);
return 0;
}

Vijendra SN, Dept. of CS&E, ICEAS 18


NETWORK PROGRAMMING LABORATORY MSCSL207

SERVER SIDE OUTPUT:

CLIENT SIDE OUTPUT:

Vijendra SN, Dept. of CS&E, ICEAS 19


NETWORK PROGRAMMING LABORATORY MSCSL207

5. Exercises using OPNET Network Simulator

Step 1: Launch the Software

 Open: Start Menu → OPNET IT Guru Academic Edi on


 Click File → New Project

Step 2: Create a New Project

 Enter Project Name: e.g., LAN_Simulation


 Enter Scenario Name: e.g., Simple_LAN
 Click OK

When prompted:

 Choose Create Empty Scenario


 Choose Campus Network as network scale
 Click OK

Step 3: Set up the Network Topology

Use the Object Palette (bottom right):

1. Add Nodes:
o Drag and drop 3 workstations (ethernet_wkstn) – these are client computers.
o Drag and drop 1 server (ethernet_server) – to act as a file/email/web server.
2. Add a Switch:
o From the same palette, drag ethernet_switch.
3. Connect Devices:
o Select Link Tool (100BaseT) from the top toolbar.
o Click each workstation and connect it to the switch.
o Connect the server to the switch.

Step 4: Configure Applications

1. From the top menu: Application Config → New


2. Create profiles for client/server behavior:
o e.g., HTTP, FTP, Email traffic
3. Assign:
o Workstations → client profile

Vijendra SN, Dept. of CS&E, ICEAS 20


NETWORK PROGRAMMING LABORATORY MSCSL207

o Server → server profile

Step 5: Configure Profiles

 Go to Profile Config:
o Define when and how frequently each client accesses the server
o Example: HTTP → Heavy Usage

Step 6: Set Simulation Parameters

 Click Simulation → Configure Simula on


 Set:
o Simulation time: e.g., 10 minutes
o Speed: Normal or Fast

Step 7: Run the Simulation

 Click the Run button (green play icon on top)

Step 8: View Results

 After the simulation completes:


o Click Results → View Results
o Choose parameters like:
 Throughput
 Delay
 Server response time
 Application traffic

Vijendra SN, Dept. of CS&E, ICEAS 21


NETWORK PROGRAMMING LABORATORY MSCSL207

6. Comparison of TCP/IP, Socket, Pipes. Analyse which is the best.

A. IPC Using Pipe (Parent-child communication)


// Communication using unnamed pipe
// pipe_ipc.c

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

int main() {

int fd[2];

pid_t pid;

char write_msg[] = "Message through PIPE";

char read_msg[100];

// Create pipe

if (pipe(fd) == -1) {

perror("Pipe failed");

return 1;

pid = fork();

if (pid < 0) {

perror("Fork failed");

return 1;

if (pid > 0) { // Parent Process

close(fd[0]); // Close reading end

write(fd[1], write_msg, strlen(write_msg) + 1);

Vijendra SN, Dept. of CS&E, ICEAS 22


NETWORK PROGRAMMING LABORATORY MSCSL207

close(fd[1]);

} else { // Child Process

close(fd[1]); // Close writing end

read(fd[0], read_msg, sizeof(read_msg));

printf("Child received from parent via PIPE: %s\n", read_msg);

close(fd[0]);

return 0;

OUTPUT:

Child received from parent via PIPE: Message through PIPE

Vijendra SN, Dept. of CS&E, ICEAS 23


NETWORK PROGRAMMING LABORATORY MSCSL207

B. IPC USING TCP SOCKET


SERVER SIDE PROGRAM:

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

#define PORT 9090

int main() {
int server_fd, new_socket;
struct sockaddr_in address;
socklen_t addrlen = sizeof(address);
char *message = "Message through TCP Socket";

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

bind(server_fd, (struct sockaddr *)&address, sizeof(address));


listen(server_fd, 3);

Vijendra SN, Dept. of CS&E, ICEAS 24


NETWORK PROGRAMMING LABORATORY MSCSL207

printf("Server waiting...\n");
new_socket = accept(server_fd, (struct sockaddr *)&address, &addrlen);
send(new_socket, message, strlen(message), 0);

close(new_socket);
close(server_fd);
return 0;
}
CLIENT SIDE PROGRAM:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#define PORT 9090
int main() {
int sock;
struct sockaddr_in serv_addr;
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));
read(sock, buffer, sizeof(buffer));
printf("Client received from server via SOCKET: %s\n", buffer);
close(sock);
return 0;
}

Vijendra SN, Dept. of CS&E, ICEAS 25


NETWORK PROGRAMMING LABORATORY MSCSL207

SERVER SIDE OUTPUT:

CLIENT SIDE OUTPUT:

Vijendra SN, Dept. of CS&E, ICEAS 26


NETWORK PROGRAMMING LABORATORY MSCSL207

COMPARISON & ANALYSIS:

FEATURE PIPE (IPC) TCP SOCKET

TYPE Local communication Can be local or remote

SPEED Faster (within system) Slightly slower

COMPLEXITY Simple (parent-child) More code & setup

USE CASE Internal IPC Networked communication

Vijendra SN, Dept. of CS&E, ICEAS 27

You might also like