0% found this document useful (0 votes)
30 views9 pages

L1F21BSCS0463 Lab6

Uploaded by

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

L1F21BSCS0463 Lab6

Uploaded by

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

Lab 6

Bakhtawar Shahid
L1F21BSCS0463
F3

(Task 1)

Server code :
// Server side implementation of TCP client-server model
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define PORT 8080


#define MAXLINE 1024

// Driver code
int main() {
int sockfd, newfd;
char buffer[MAXLINE];
char *hello = "Hello from server";
struct sockaddr_in servaddr, cliaddr;

// Creating socket file descriptor


if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket creation failed");
exit(EXIT_FAILURE);
}
memset(&servaddr, 0, sizeof(servaddr));
memset(&cliaddr, 0, sizeof(cliaddr));

// Filling server information


servaddr.sin_family = AF_INET; // IPv4
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(PORT);

// Bind the socket with the server address


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

// Listen for incoming connections


if (listen(sockfd, 5) < 0) {
perror("listen failed");
exit(EXIT_FAILURE);
}

int len = sizeof(cliaddr), n;


while (1) {
// Accept a new connection
if ((newfd = accept(sockfd, (struct sockaddr *)&cliaddr, (socklen_t *)&len)) < 0) {
perror("accept failed");
exit(EXIT_FAILURE);
}

// Receive data from client


n = recv(newfd, buffer, MAXLINE, 0);
buffer[n] = '\0';
printf("Client : %s\n", buffer);

// Send response to client


send(newfd, hello, strlen(hello), 0);
printf("Hello message sent.\n");

// Close the socket for this client


close(newfd);
}
return 0;
}

Client Code :

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

#define PORT 8080


#define MAXLINE 1024

int main() {
int sockfd;
char buffer[MAXLINE];
char *hello = "Hello from client";
struct sockaddr_in servaddr;

// Creating socket file descriptor


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

memset(&servaddr, 0, sizeof(servaddr));

// Filling server information


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

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


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

// Connect to the server


if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
perror("Connection failed");
exit(EXIT_FAILURE);
}

// Send data to server


send(sockfd, hello, strlen(hello), 0);
printf("Hello message sent.\n");

// Receive response from server


int n = recv(sockfd, buffer, MAXLINE, 0);
buffer[n] = '\0';
printf("Server : %s\n", buffer);

close(sockfd);
return 0;
}

(Task 2)
Code for Client :

// Client side implementation of TCP client-server model


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>

#define PORT 8080


#define MAXLINE 1024

void encryptData(char *data) {


while (*data) {
if (islower(*data))
*data = (*data - 'a' + 3) % 26 + 'a';
else if (isupper(*data))
*data = (*data - 'A' + 2) % 26 + 'A';
else if (isdigit(*data))
*data = (*data - '0' + 1) % 10 + '0';
data++;
}
}

int main() {
int sockfd;
char buffer[MAXLINE];
struct sockaddr_in servaddr;

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {


perror("socket creation failed");
exit(EXIT_FAILURE);
}

memset(&servaddr, 0, sizeof(servaddr));

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

if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {


perror("connection failed");
exit(EXIT_FAILURE);
}

FILE *file = fopen("fileData.txt", "r");


if (file == NULL) {
perror("file open failed");
exit(EXIT_FAILURE);
}

char data[MAXLINE];
fgets(data, MAXLINE, file);
fclose(file);

encryptData(data);

send(sockfd, data, strlen(data), 0);


printf("Encrypted data sent to server.\n");

int n = recv(sockfd, buffer, MAXLINE, 0);


buffer[n] = '\0';

printf("Decrypted data received from server: %s\n", buffer);

close(sockfd);
return 0;
}

Code for Server :


// Server side implementation of TCP client-server model
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <ctype.h>

#define PORT 8080


#define MAXLINE 1024

void decryptData(char *data) {


while (*data) {
if (islower(*data))
*data = (*data - 'a' - 3 + 26) % 26 + 'a';
else if (isupper(*data))
*data = (*data - 'A' - 2 + 26) % 26 + 'A';
else if (isdigit(*data))
*data = (*data - '0' - 1 + 10) % 10 + '0';
data++;
}
}

int main() {
int sockfd, newfd;
char buffer[MAXLINE];
struct sockaddr_in servaddr, cliaddr;

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {


perror("socket creation failed");
exit(EXIT_FAILURE);
}

memset(&servaddr, 0, sizeof(servaddr));

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

if (bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {


perror("bind failed");
exit(EXIT_FAILURE);
}

if (listen(sockfd, 5) < 0) {
perror("listen failed");
exit(EXIT_FAILURE);
}

int len = sizeof(cliaddr), n;


while (1) {

if ((newfd = accept(sockfd, (struct sockaddr *)&cliaddr, (socklen_t *)&len)) < 0) {


perror("accept failed");
exit(EXIT_FAILURE);
}

n = recv(newfd, buffer, MAXLINE, 0);


buffer[n] = '\0';

decryptData(buffer);

send(newfd, buffer, strlen(buffer), 0);


printf("Decrypted data sent back to client.\n");

close(newfd);
}
return 0;
}

(Task – 3)
Code for Server

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

#define PORT 8080


#define MAXLINE 1024

int main() {
int sockfd, newfd;
struct sockaddr_in servaddr, cliaddr;
char buffer[MAXLINE];
char *file_name = "example.txt";

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {


perror("socket creation failed");
exit(EXIT_FAILURE);
}

memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(PORT);

if (bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) {


perror("bind failed");
exit(EXIT_FAILURE);
}

if (listen(sockfd, 5) == -1) {
perror("listen failed");
exit(EXIT_FAILURE);
}

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

while (1) {
int len = sizeof(cliaddr);

if ((newfd = accept(sockfd, (struct sockaddr *)&cliaddr, (socklen_t *)&len)) == -1) {


perror("accept failed");
exit(EXIT_FAILURE);
}

printf("Client connected from port %d, process ID: %d\n", ntohs(cliaddr.sin_port), getpid());

recv(newfd, buffer, MAXLINE, 0);


printf("File name received from client: %s\n", buffer);

FILE *file = fopen(file_name, "r");


if (file == NULL) {
perror("file open failed");
exit(EXIT_FAILURE);
}

while (fgets(buffer, MAXLINE, file) != NULL) {


send(newfd, buffer, strlen(buffer), 0);
}

fclose(file);
printf("File sent to client.\n");

close(newfd);
printf("Connection with client closed.\n");
}

close(sockfd);
return 0;
}

Code for Client

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

#define PORT 8080


#define MAXLINE 1024

int main() {
int sockfd;
struct sockaddr_in servaddr;
char buffer[MAXLINE];
char *file_name = "received_file.txt";

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {


perror("socket creation failed");
exit(EXIT_FAILURE);
}

memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); // Assuming server is running on localhost

if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) {


perror("connection failed");
exit(EXIT_FAILURE);
}

printf("Connected to server on port %d.\n", PORT);

char *file_request = "example.txt";


send(sockfd, file_request, strlen(file_request), 0);
printf("File request sent to server.\n");

FILE *file = fopen(file_name, "w");


if (file == NULL) {
perror("file open failed");
exit(EXIT_FAILURE);
}

while (1) {
int n = recv(sockfd, buffer, MAXLINE, 0);
if (n <= 0) break;
fwrite(buffer, sizeof(char), n, file);
}

fclose(file);
printf("File received and saved as %s.\n", file_name);

close(sockfd);
printf("Connection with server closed.\n");

return 0;
}

You might also like