22013570039
22013570039
Q1. Implement TCP Echo client and TCP Echo server (Iterative).
Echo server:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main() {
int opt = 1;
perror("socket failed");
exit(EXIT_FAILURE);
}
// Attaching socket to the port
sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
< 0) {
perror("bind failed");
exit(EXIT_FAILURE);
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
while (1) {
(socklen_t*)&addrlen);
if (new_socket < 0) {
perror("accept");
exit(EXIT_FAILURE);
return 0;
Echo client:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main() {
int sock = 0;
// Creating socket
return-1;
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
return-1;
sizeof(serv_addr)) < 0) {
return-1;
// Send a message
close(sock);
return 0;
Q2. Implement TCP Echo client and TCP Echo server (Concurrent)
Echo client:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main() {
int sock = 0;
return-1;
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
return-1;
sizeof(serv_addr)) < 0) {
return-1;
while (1) {
if (strcmp(buffer, "exit") == 0) {
break;
close(sock);
return 0;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <pthread.h>
char buffer[BUFFER_SIZE];
int n;
close(sock);
free(client_socket);
return NULL;
int main() {
int opt = 1;
perror("socket failed");
exit(EXIT_FAILURE);
sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
< 0) {
perror("bind failed");
exit(EXIT_FAILURE);
if (listen(server_fd, 3) < 0) {
perror("listen failed");
exit(EXIT_FAILURE);
while (1) {
new_socket = malloc(sizeof(int));
perror("accept failed");
free(new_socket);
continue;
pthread_t thread_id;
pthread_create(&thread_id, NULL, handle_client, new_socket);
pthread_detach(thread_id);
return 0;
Q3. Implement TCP daytime client and TCP daytime server (Iterative).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main() {
int sock = 0;
// Create socket
return-1;
}
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
return-1;
sizeof(serv_addr)) < 0) {
return-1;
printf("%s", buffer);
close(sock);
return 0;
Tcp DaytimeServer(Iterative):
Q4.Implement TCP daytime client and TCP daytime server (concurrent).
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
int sockfd;
char recvline[MAXLINE];
if (argc != 2) {
exit(1);
if (sockfd < 0) {
exit(1);
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(8080);
perror("inet_pton error");
exit(1);
exit(1);
int n;
recvline[n] = '\0';
fputs(recvline, stdout);
if (n < 0) {
perror("Read error");
close(sockfd);
return 0;
Server code:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include <signal.h>
char buff[MAXLINE];
time_t ticks;
ticks = time(NULL);
close(connfd);
exit(0);
int main() {
signal(SIGCHLD, SIG_IGN);
if (listenfd < 0) {
exit(1);
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(8080);
perror("Bind error");
exit(1);
perror("Listen error");
exit(1);
for ( ; ; ) {
if (connfd < 0) {
perror("Accept error");
continue;
}
if (fork() == 0) {
close(listenfd);
handle_client(connfd);
close(connfd);
return 0;
Client code:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
int sockfd;
exit(1);
if (sockfd < 0) {
exit(1);
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(12345);
perror("inet_pton error");
exit(1);
sizeof(servaddr)) < 0) {
perror("Send error");
exit(1);
if (n < 0) {
perror("Receive error");
exit(1);
buffer[n] = '\0';
printf("Received from server: %s\n", buffer);
close(sockfd);
return 0;
Server code:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
int main() {
int sockfd;
char buffer[MAXLINE];
socklen_t len;
if (sockfd < 0) {
exit(1);
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(12345);
perror("Bind error");
exit(1);
}
printf("UDP Echo Server running on port 12345...\n");
while (1) {
len = sizeof(cliaddr);
if (n < 0) {
perror("Receive error");
exit(1);
buffer[n] = '\0';
perror("Send error");
exit(1);
close(sockfd);
return 0;
Client code:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
int sockfd;
char buffer[MAXLINE];
if (argc != 2) {
exit(1);
if (sockfd < 0) {
exit(1);
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(12345);
perror("inet_pton error");
exit(1);
sizeof(servaddr)) < 0) {
perror("Send error");
exit(1);
}
if (n < 0) {
perror("Receive error");
exit(1);
buffer[n] = '\0';
close(sockfd);
return 0;
server code:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
int main() {
int sockfd;
char buffer[MAXLINE];
socklen_t len;
time_t ticks;
if (sockfd < 0) {
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(12345);
perror("Bind error");
exit(1);
while (1) {
len = sizeof(cliaddr);
if (n < 0) {
perror("Receive error");
exit(1);
ticks = time(NULL);
perror("Send error");
exit(1);
close(sockfd);
return 0;
}
7.Implement TCP client and server (concurrent) where client gets input from the user and sends it to
server. Server displays it on the screen. Server then gets another input from the user and sends it to
client. Client displays it on the screen. The process continues till server or client sends “bye” to the
other party.
Client code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int sockfd;
char buffer[MAXLINE];
int n;
if (argc != 2) {
exit(1);
exit(1);
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
exit(1);
perror("Connection failed");
exit(1);
while (1) {
printf("Client: ");
bzero(buffer, sizeof(buffer));
if (strncmp(buffer, "bye", 3) == 0) {
break;
bzero(buffer, sizeof(buffer));
if (n <= 0) {
break;
break;
close(sockfd);
return 0;
Server code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <arpa/inet.h>
int main() {
socklen_t clilen;
pthread_t tid;
exit(1);
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
if (bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
perror("Bind failed");
exit(1);
perror("Listen failed");
exit(1);
while (1) {
clilen = sizeof(cliaddr);
perror("Accept failed");
continue;
exit(1);
pthread_detach(tid);
return 0;
char buffer[MAXLINE];
int n;
while (1) {
bzero(buffer, sizeof(buffer));
if (n <= 0) {
break;
if (strncmp(buffer, "bye", 3) == 0) {
break;
printf("Server: ");
bzero(buffer, sizeof(buffer));
if (strncmp(buffer, "bye", 3) == 0) {
break;
close(connfd);
return NULL;
}
8.Implement TCP client and server(concurrent) where client requests server to transfer a file.
Assume file is smaller than 1K size. If the file is present on the server, it is sent to the client otherwise
an error message is sent to client. Client copies the file on the hard disk and disconnects.
Client code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main() {
int sock;
char file_name[BUFFER_SIZE];
char buffer[BUFFER_SIZE];
FILE *file;
exit(1);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
perror("Connection failed");
exit(1);
}
scanf("%s", file_name);
if (file == NULL) {
close(sock);
exit(1);
ssize_t n;
fwrite(buffer, 1, n, file);
fclose(file);
close(sock);
return 0;
Server code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <pthread.h>
#define MAX_CLIENTS 10
char file_name[BUFFER_SIZE];
char buffer[BUFFER_SIZE];
if (file == NULL) {
} else {
strcpy(buffer, "FOUND");
size_t n;
fclose(file);
close(sock);
free(client_socket);
pthread_exit(NULL);
int main() {
pthread_t thread_id;
exit(1);
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
perror("Bind failed");
exit(1);
perror("Listen failed");
exit(1);
while (1) {
new_sock = malloc(sizeof(int));
if (*new_sock < 0) {
perror("Accept failed");
free(new_sock);
continue;
printf("Connection accepted\n");
close(*new_sock);
free(new_sock);
}
return 0;
9.Implement UDP client and UDP server where server displays the IP address and port number of the
client sending the datagram. Client sends a datagram (size 64 bytes) three times to the same server.
server sends the message back to client. Client reports the time elapsed in sending and receiving of
the message. Use connected UDP sockets.
Client code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <time.h>
#define MAXLINE 64
int sockfd;
char buffer[MAXLINE];
struct timespec start, end;
if (argc != 2) {
exit(1);
exit(1);
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
exit(1);
perror("Connect failed");
exit(1);
clock_gettime(CLOCK_MONOTONIC, &start);
buffer[n] = '\0';
clock_gettime(CLOCK_MONOTONIC, &end);
close(sockfd);
return 0;
Server code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#define MAXLINE 64
int main() {
int sockfd;
char buffer[MAXLINE];
exit(1);
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
perror("Bind failed");
exit(1);
while (1) {
int n = recvfrom(sockfd, (char *)buffer, MAXLINE, 0, (struct sockaddr *)&cliaddr, &len);
buffer[n] = '\0';
char client_ip[INET_ADDRSTRLEN];
len);
close(sockfd);
return 0;
10.write a program to a) display name of the host b) all IP address of the host. c) Check whether FTP
and HTTP services are running on the system. d) Display the name of the service running on port
number specified by user.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#define MAX_IPS 10
void display_host_name() {
char hostname[MAX_HOSTNAME_LEN];
if (gethostname(hostname, sizeof(hostname)) == 0) {
} else {
void display_ip_addresses() {
char **ip_addresses;
char hostname[MAX_HOSTNAME_LEN];
int i = 0;
if (gethostname(hostname, sizeof(hostname)) == 0) {
host_info = gethostbyname(hostname);
if (host_info == NULL) {
return;
ip_addresses = host_info->h_addr_list;
printf("IP Addresses:\n");
ip_addresses++;
i++;
} else {
void check_services_running() {
if (service != NULL) {
} else {
if (service != NULL) {
} else {
if (service != NULL) {
int main() {
int port;
display_host_name();
display_ip_addresses();
check_services_running();
scanf("%d", &port);
display_service_by_port(port);
return 0;