Professional Documents
Culture Documents
SID : 20105131
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 anshul.c. Command: vim anshul.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 anshul.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’
i. connect(),
ii. send()
iii. recv()
iv. bind()
v. listen()
vi. accept(),
vii. sendto(),
viii. recvfrom()
Ans 2:
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);
}
Explanation:
o AF_INET: Address family (IPv4). o SOCK_STREAM: Type of socket (in this case,
a stream socket, which is TCP).
o 0: Protocol (0 specifies the default protocol for the given socket type).
• After creating the socket, it's important to check if the creation was successful. If not, an
error message is printed, and the program exits.
• At this point, you can perform various operations using the created socket.
• If all operations are successful, the program prints messages indicating that the socket
was created and closed successfully.
b)
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main() {
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);
exit(EXIT_FAILURE);
}
if (listen(server_fd, MAX_CONNECTIONS) == -1) {
perror("Listen failed");
exit(EXIT_FAILURE);
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.
• 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>
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>
}
char *message = "Hello from client!"; send(sock,
message, strlen(message), 0); printf("Message sent to
server\n");
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>
while (1) {
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>
#define PORT 8080
int main() {
-1) {
ntohs(client_addr.sin_port));
while (1) {
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>
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) {
if (recvBytes == -1) {
}
buffer[recvBytes] = '\0';
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>
#define SERVER_PORT 8080
while (1) {
ssize_t recvBytes = recvfrom(serverSocket, buffer, BUFFER_SIZE, 0, (struct
sockaddr*)&clientAddr, &clientAddrLen);
if (recvBytes == -1) {
buffer[recvBytes] = '\0';
close(serverSocket);
return 0;