0% found this document useful (0 votes)
12 views18 pages

Network Labprogram

Vtu Mtech scs nlp programs

Uploaded by

veenanaik
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)
12 views18 pages

Network Labprogram

Vtu Mtech scs nlp programs

Uploaded by

veenanaik
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

[Link] a C program to implement daytime client/server program using TCP sockets.

//Server.c
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <time.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <string.h>
int main() {
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd < 0) {
perror("Socket creation failed");
return -1;
}
struct sockaddr_in addr = {AF_INET, htons(1313), INADDR_ANY};
if (bind(server_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
perror("Bind failed");
return -1;
}
listen(server_fd, 5);
printf("Daytime server listening on port 13...\n");
while(1) {
int client = accept(server_fd, NULL, NULL);
if (client < 0) continue;

1
time_t t = time(NULL);
char *timestr = ctime(&t);
send(client, timestr, strlen(timestr), 0);
close(client);
printf("Time sent to client: %s", timestr);
}
close(server_fd);
return 0;
}

2
//Client.c
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

#include <string.h>
int main() {
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror("Socket creation failed");
return -1;
}
struct sockaddr_in addr = {AF_INET, htons(1313), inet_addr("[Link]")};
if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
perror("Connection failed");
return -1;
}
char buffer[100];
int bytes = recv(sock, buffer, sizeof(buffer)-1, 0);
buffer[bytes] = '\0';
printf("Current server time: %s", buffer);
close(sock);
return 0;
}}

3
Output:

unix2024@DESKTOP-37GBF05:~$ gcc prog1s.c -o prog1


unix2024@DESKTOP-37GBF05:~$ ./prog1
Daytime server listening on port 13...
Time sent to client: Tue Aug 12 [Link] 2025

unix2024@DESKTOP-37GBF05:~$ gcc prog1c.c -o prog1cr


unix2024@DESKTOP-37GBF05:~$ ./prog1cr
Current server time: Tue Aug 12 [Link] 2025

4
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.c
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include <unistd.h>
int main() {
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr = {AF_INET, htons(8080), INADDR_ANY};
int opt = 1;
setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
bind(server_fd, (struct sockaddr*)&addr, sizeof(addr));
listen(server_fd, 5);
printf("Calculator server listening on port 8080...\n");
while(1) {
int client = accept(server_fd, NULL, NULL);
if (client < 0) continue;
int nums[3], results[3];
recv(client, nums, sizeof(nums), 0);
printf("Received numbers: %d, %d, %d\n", nums[0], nums[1], nums[2]);
results[0] = nums[0] + nums[1] + nums[2]; // sum
results[1] = nums[0] - nums[1] - nums[2]; // difference
results[2] = nums[0] * nums[1] * nums[2]; // product
send(client, results, sizeof(results), 0);
printf("Results sent: Sum=%d, Diff=%d, Product=%d\n",

5
results[0], results[1], results[2]);
close(client);
}
close(server_fd);
return 0;
}

6
//Client.c
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
int main() {
int sock = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr = {AF_INET, htons(8080), inet_addr("[Link]")};
if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
perror("Connection failed");
return -1;
}
int nums[3], results[3];
printf("Enter three numbers: ");
scanf("%d %d %d", &nums[0], &nums[1], &nums[2]);
send(sock, nums, sizeof(nums), 0);
recv(sock, results, sizeof(results), 0);
printf("\nResults from server:\n");
printf("Sum: %d\n", results[0]);
printf("Difference: %d\n", results[1]);
printf("Product: %d\n", results[2]);
close(sock);
return 0;
}

7
Output:

unix2024@DESKTOP-37GBF05:~$ gcc prog2s.c -o prog2

unix2024@DESKTOP-37GBF05:~$ ./prog2

Calculator server listening on port 8080...

Received numbers: 21, 27, 108

Results sent: Sum=156, Diff=-114, Product=61236

unix2024@DESKTOP-37GBF05:~$ gcc prog2c.c -o prog2cr

unix2024@DESKTOP-37GBF05:~$ ./prog2cr

Enter three numbers: 21 27 108

Results from server:

Sum: 156

Difference: -114

Product: 61236

8
3. Write a C program that prints the IP layer and TCP layer socket options in a separate
file.
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <unistd.h>
int main() {
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror("Socket creation failed");
return -1;
}
FILE *fp = fopen("socket_options.txt", "w");
if (!fp) {
perror("File open failed");
return -1;
}
int val;
socklen_t len = sizeof(val);
fprintf(fp, "=== SOCKET OPTIONS REPORT ===\n\n");
// IP Layer Options
fprintf(fp, "IP LAYER OPTIONS:\n");
fprintf(fp, "-----------------\n");
if (getsockopt(sock, IPPROTO_IP, IP_TTL, &val, &len) == 0)
fprintf(fp, "IP_TTL: %d\n", val);
if (getsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, &len) == 0)

9
fprintf(fp, "SO_REUSEADDR: %d\n", val);
if (getsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &val, &len) == 0)
fprintf(fp, "SO_KEEPALIVE: %d\n", val);
if (getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &val, &len) == 0)
fprintf(fp, "SO_RCVBUF: %d bytes\n", val);
if (getsockopt(sock, SOL_SOCKET, SO_SNDBUF, &val, &len) == 0)
fprintf(fp, "SO_SNDBUF: %d bytes\n", val);
// TCP Layer Options
fprintf(fp, "\nTCP LAYER OPTIONS:\n");
fprintf(fp, "------------------\n");
if (getsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &val, &len) == 0)
fprintf(fp, "TCP_NODELAY: %d\n", val);
if (getsockopt(sock, IPPROTO_TCP, TCP_MAXSEG, &val, &len) == 0)
fprintf(fp, "TCP_MAXSEG: %d bytes\n", val);
fprintf(fp, "\n=== END OF REPORT ===\n");
fclose(fp);
close(sock);
printf("Socket options have been written to 'socket_options.txt'\n");
printf("Check the file for detailed information.\n");
return 0;
}

10
Output

unix2024@DESKTOP-37GBF05:~$ gcc prog3.c -o prog3s

unix2024@DESKTOP-37GBF05:~$ ./prog3s

Socket options have been written to 'socket_options.txt'

Check the file for detailed information.

unix2024@DESKTOP-37GBF05:~$ cat socket_options.txt

=== SOCKET OPTIONS REPORT ===

IP LAYER OPTIONS:

-----------------

IP_TTL: 64

SO_REUSEADDR: 0

SO_KEEPALIVE: 0

SO_RCVBUF: 131072 bytes

SO_SNDBUF: 16384 bytes

TCP LAYER OPTIONS:

------------------

TCP_NODELAY: 0

TCP_MAXSEG: 536 bytes

=== END OF REPORT ===

11
4. Exercises on Socket Programming using C and Java

Server. java

import [Link].*;
import [Link].*;
public class Server {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(9999);

[Link]("Server listening on port 9999...");


while (true) {
Socket clientSocket = [Link]();
[Link]("Client connected: " +
[Link]());
BufferedReader in = new BufferedReader(
new InputStreamReader([Link]()));
PrintWriter out = new PrintWriter(
[Link](), true);
String inputLine = [Link]();

[Link]("Received: " + inputLine);


[Link]("Echo: " + inputLine);
[Link]();
}
} catch (IOException e) {
[Link]("Server error: " + [Link]());
}
}
}

12
[Link]

import [Link].*;

import [Link].*;

import [Link];

public class Client {

public static void main(String[] args) {

try {

Socket socket = new Socket("localhost", 9999);

PrintWriter out = new PrintWriter(

[Link](), true);

BufferedReader in = new BufferedReader(

new InputStreamReader([Link]()));

Scanner scanner = new Scanner([Link]);

[Link]("Enter message to send: ");

String message = [Link]();

[Link](message);

String response = [Link]();

[Link]("Server response: " +

response);

[Link]();

[Link]();

} catch (IOException e) {

[Link]("Client error: " +[Link]());}}}

13
Output:

unix2024@DESKTOP-37GBF05:~$ javac [Link]


unix2024@DESKTOP-37GBF05:~$ java prog4s
Server listening on port 9999...
Client connected: /[Link]
Received: Welcome to network programming

unix2024@DESKTOP-37GBF05:~$ javac [Link]


unix2024@DESKTOP-37GBF05:~$ java prog4s
Server listening on port 9999...
Client connected: /[Link]
Received: Welcome to network programming

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

#include <stdio.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#define TEST_DATA "Performance test data for comparison"
#define ITERATIONS 1000
long measure_tcp_performance() {
struct timeval start, end;
int sockfd[2];
gettimeofday(&start, NULL);
for (int i = 0; i < ITERATIONS; i++) {
socketpair(AF_UNIX, SOCK_STREAM, 0, sockfd);
write(sockfd[0], TEST_DATA, strlen(TEST_DATA));
char buffer[100];
read(sockfd[1], buffer, sizeof(buffer));
close(sockfd[0]);
close(sockfd[1]);
}
gettimeofday(&end, NULL);
return ((end.tv_sec - start.tv_sec) * 1000000 +
(end.tv_usec - start.tv_usec));
}
long measure_pipe_performance() {

15
struct timeval start, end;
int pipefd[2];
gettimeofday(&start, NULL);
for (int i = 0; i < ITERATIONS; i++) {
pipe(pipefd);
write(pipefd[1], TEST_DATA, strlen(TEST_DATA));
char buffer[100];
read(pipefd[0], buffer, sizeof(buffer));
close(pipefd[0]);
close(pipefd[1]);
}
gettimeofday(&end, NULL);
return ((end.tv_sec - start.tv_sec) * 1000000 +
(end.tv_usec - start.tv_usec));
}
int main() {
printf("PROTOCOL PERFORMANCE COMPARISON\n");
printf("================================\n");
printf("Test iterations: %d\n", ITERATIONS);
printf("Test data size: %lu bytes\n\n", strlen(TEST_DATA));
long tcp_time = measure_tcp_performance();
long pipe_time = measure_pipe_performance();
printf("Results:\n");
printf("--------\n");
printf("TCP Sockets: %ld microseconds\n", tcp_time);
printf("Named Pipes: %ld microseconds\n", pipe_time);
printf("\nPerformance Ratio: %.2f\n",

16
(double)tcp_time / pipe_time);
printf("\nAnalysis:\n");
printf("---------\n");
if (pipe_time < tcp_time) {
printf("Winner: PIPES\n");
printf("Reason: Lower overhead, no network stack\n");
printf("Best for: Local IPC communication\n");
} else {
printf("Winner: TCP SOCKETS\n");
printf("Reason: Better for network communication\n");
printf("Best for: Distributed systems\n");
}
printf("\nRecommendations:\n");
printf("----------------\n");
printf("• Use PIPES for local inter-process communication\n");
printf("• Use TCP SOCKETS for network communication\n");
printf("• Consider UDP for low-latency applications\n");
return 0;
}

17
Output:

unix2024@DESKTOP-37GBF05:~$ gcc prog6.c -o prog6s

unix2024@DESKTOP-37GBF05:~$ ./prog6s

PROTOCOL PERFORMANCE COMPARISON

================================

Test iterations: 1000

Test data size: 36 bytes

Results:

--------

TCP Sockets: 5257 microseconds

Named Pipes: 1569 microseconds

Performance Ratio: 3.35

Analysis:

---------

Winner: PIPES

Reason: Lower overhead, no network stack

Best for: Local IPC communication

Recommendations:

----------------

• Use PIPES for local inter-process communication

• Use TCP SOCKETS for network communication

• Consider UDP for low-latency applications

18

You might also like