CN Assignment 20105034 Saksham Garg
CN Assignment 20105034 Saksham Garg
CN Assignment 20105034 Saksham Garg
Ques1: Write and explain the basic commands to edit, compile and execute ‘C’
programs in Linux Environment?
Ans 1:
To Edit: Use a text editor to create or edit your C program. For example, using
Vim: This will open the Vim editor with a new or existing file named saksham.c.
Command: vim saksham.c
To Compile: The GNU Compiler Collection (GCC) is a commonly used compiler for
C programs on Linux.
In the terminal, we use the following command to compile your C program:
Command: gcc -o output_file saksham.c
To Execute: Once the compilation is successful, we can run the compiled program
using the following command:
Command: . /output_file
The program will now execute, and we'll see the output in the terminal.
Ques 2: Write basic code with brief explanation of the basics of sockets
programming using ‘C’
Programming Language in Linux based environment for follwing
a. Creating and destroying sockets – socket() and close() functions
b. Specifying addresses – sockaddr and sockaddr_in structures
c. Understanding the syntax, purpose and use of various functions used in sockets
programing in ‘C’ programming language – connect(), send(), recv(), bind(),
listen(), accept(), sendto(), recvfrom() etc. functions
Ans 2:
a) Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
int main() {
int server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
perror("Error creating socket");
exit(EXIT_FAILURE);
}
b)
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main() {
int server_fd, new_socket;
struct sockaddr_in server_addr, client_addr;
socklen_t addr_len = sizeof(client_addr);
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
close(new_socket);
close(server_fd);
return 0;
}
Explanation:
socket(AF_INET, SOCK_STREAM, 0): Creates a new socket using IPv4 and TCP.
bind(server_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)):
Associates the socket with the specified address and port.
listen(server_fd, MAX_CONNECTIONS): Listens for incoming connections with
a maximum queue size.
accept(server_fd, (struct sockaddr*)&client_addr, &addr_len): Accepts an
incoming connection and returns a new socket for data exchange.
The struct sockaddr_in structure is used to represent the server and client
addresses.
c)
Code:
Server.c(server side)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main() {
int server_fd, new_socket;
struct sockaddr_in server_addr, client_addr;
int opt = 1;
int addrlen = sizeof(server_addr);
char buffer[BUFFER_SIZE] = {0};
if (listen(server_fd, 3) < 0)
{ perror("Listen failed");
exit(EXIT_FAILURE);
}
close(new_socket);
close(server_fd);
return 0;
}
Client.c(client side)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main() {
int sock = 0;
struct sockaddr_in server_addr;
char buffer[BUFFER_SIZE] = {0};
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
close(sock);
return 0;
}
Explanation:
connect(): This function is used to connect a socket to a server. It takes the socket file descriptor, the
server address, and the length of the server address as arguments.
send(): This function is used to send data over the socket. It takes the socket file descriptor, the message
to be sent, the length of the message, and flags as arguments.
recv(): This function is used to receive data from the socket. It takes the socket file descriptor, the buffer
to store the received data, the maximum length of the data to be received, and flags as arguments.
bind(): This function is used to bind a socket to a particular address and port. It takes the socket file
descriptor, the address to bind to, and the length of the address as arguments.
listen(): This function is used to put the socket in listening mode. It takes the socket file descriptor and
the maximum number of pending connections as arguments.
accept(): This function is used to accept a connection request from a client. It takes the socket file
descriptor, the address of the client, and the length of the address as arguments.
sendto(): This function is used to send data to a specific address. It takes the socket file descriptor, the
message to be sent, the length of the message, flags, the address to send to, and the length of the
address as arguments.
recvfrom(): This function is used to receive data from a specific address. It takes the socket file
descriptor, the buffer to store the received data, the maximum length of the data to be received, flags,
the address from which to receive, and the length of the address as arguments.
Ques3: TCP/IP based Echo Client program using ‘C’ Programming Language in
Linux based environment
Ans3:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main() {
int client_socket;
struct sockaddr_in server_addr;
char buffer[MAX_BUFFER_SIZE];
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
while (1) {
printf("Enter message to send (type 'exit' to quit): ");
fgets(buffer, MAX_BUFFER_SIZE, stdin);
if (strncmp(buffer, "exit", 4) == 0)
break;
close(client_socket);
return 0;
}
Ques4: TCP/IP based Echo server program using ‘C’ Programming Language in
Linux based environment
Ans4:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main() {
int server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
char buffer[MAX_BUFFER_SIZE];
if (listen(server_socket, 3) == -1)
{ perror("Listen failed");
exit(EXIT_FAILURE);
}
while (1) {
ssize_t received_bytes = recv(client_socket, buffer, MAX_BUFFER_SIZE, 0);
if (received_bytes <= 0) {
printf("Client disconnected\n");
break;
}
close(client_socket);
close(server_socket);
return 0;
}
Ques 5: UDP/IP based Echo Client Program using ‘C’ Programming Language in
Linux based environment
Ans 5:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main() {
int clientSocket;
struct sockaddr_in serverAddr;
char buffer[BUFFER_SIZE];
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(SERVER_PORT);
inet_pton(AF_INET, SERVER_IP, &serverAddr.sin_addr);
while (1) {
fgets(buffer, BUFFER_SIZE, stdin);
sendto(clientSocket, buffer, strlen(buffer), 0, (struct sockaddr*)&serverAddr,
sizeof(serverAddr));
if (strncmp(buffer, "exit", 4) == 0)
break;
if (recvBytes == -1)
{ perror("Error receiving
data"); exit(EXIT_FAILURE);
}
buffer[recvBytes] = '\0';
printf("Server response: %s", buffer);
}
close(clientSocket);
return 0;
}
Ques6: UDP/IP based Echo Server Program using ‘C’ Programming Language in
Linux based environment
Ans 6:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main() {
int serverSocket;
struct sockaddr_in serverAddr, clientAddr;
socklen_t clientAddrLen = sizeof(clientAddr);
char buffer[BUFFER_SIZE];
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons(SERVER_PORT);
while (1) {
ssize_t recvBytes = recvfrom(serverSocket, buffer, BUFFER_SIZE, 0, (struct
sockaddr*)&clientAddr, &clientAddrLen);
if (recvBytes == -1)
{ perror("Error receiving
data"); exit(EXIT_FAILURE);
}
buffer[recvBytes] = '\0';
printf("Received from %s:%d: %s", inet_ntoa(clientAddr.sin_addr),
ntohs(clientAddr.sin_port), buffer);
close(serverSocket);
return 0;
}