0% found this document useful (0 votes)
40 views6 pages

L1F21BSCS0463 Lab8

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)
40 views6 pages

L1F21BSCS0463 Lab8

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/ 6

Bakhtawar Shahid

L1F21BSCS0463

Task1:
#include<pthread.h>
#include<stdio.h>
#include<unistd.h>
void *printdetails(void *arg)
{
printf("Name: Bakahtawar Shahid\n");
printf("Registration Number:L1F21BSCS0463\n");
pthread_exit(NULL);

}
int main()
{
pthread_t thread;
pthread_create(&thread,NULL,&printdetails,NULL);
pthread_join(thread,NULL);
return 0;
}

TASK2:
#include <stdio.h>
#include <pthread.h>

void *printNameAndRollNo(void *args)


{
char *name = ((char **)args)[0];
char *rollNo = ((char **)args)[1];

printf("Name: %s\n", name);


printf("Roll No: %s\n", rollNo);

return NULL;
}

int main()
{
char *name = “Bakhtawar Shahid";
char *rollNo = "L1F21BSCS0463";
pthread_t t1;
void *args[2] = {name, rollNo};

pthread_create(&t1, NULL, printNameAndRollNo, args);


pthread_join(t1, NULL);

return 0;
}

TASK3:
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 <pthread.h>
#include <netinet/in.h>

#define PORT 5010


#define MAXLINE 1024

void *send_handler(void *socket_fd) {


int sockfd = *((int *)socket_fd);
char buffer[MAXLINE];

while (1) {
memset(buffer, 0, sizeof(buffer));
fgets(buffer, MAXLINE, stdin);
send(sockfd, buffer, strlen(buffer), 0);

if (strcmp(buffer, "exit\n") == 0) {
break;
}
}

return NULL;
}

void *receive_handler(void *socket_fd) {


int sockfd = *((int *)socket_fd);
char buffer[MAXLINE];

while (1) {
memset(buffer, 0, sizeof(buffer));
int n = recv(sockfd, buffer, sizeof(buffer), 0);

if (n <= 0) {
break;
}

printf("%s", buffer);
}

return NULL;
}

int main() {
int sockfd;
char buffer[MAXLINE];
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);
servaddr.sin_addr.s_addr = INADDR_ANY;

connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));

pthread_t send_tid, receive_tid;

pthread_create(&send_tid, NULL, send_handler, (void *)&sockfd);


pthread_create(&receive_tid, NULL, receive_handler, (void *)&sockfd);
pthread_join(send_tid, NULL);
pthread_join(receive_tid, NULL);

close(sockfd);
return 0;
}
Server 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 <pthread.h>
#include <netinet/in.h>

#define PORT 5010


#define MAXLINE 1024
#define MAX_CLIENTS 50

char* wait_new_client = "\nWaiting for Client\n";


char* files_list = "File1.txt\nFile2.txt\nFile3.txt\n"; // Sample file list

struct Client {
int socket_fd;
struct sockaddr_in address;
};

struct Client client_list[MAX_CLIENTS];


int num_clients = 0;

void send_message(int sockfd, const char* message) {


send(sockfd, message, strlen(message), 0);
}

void* handle_client(void* arg) {


struct Client* client = (struct Client*)arg;
int sockfd = client->socket_fd;

char buffer[MAXLINE];
// Send client IP and port
char client_info[MAXLINE];
sprintf(client_info, "Client IP: %s, Port: %d\n", inet_ntoa(client-
>address.sin_addr), ntohs(client->address.sin_port));
send_message(sockfd, client_info);

// Send list of files


send_message(sockfd, files_list);

// Receive file name


recv(sockfd, buffer, MAXLINE, 0);

// Assuming client requests a file here and it's sent


// For simplicity, let's assume the file content is "Sample content"
send_message(sockfd, "Sample content\n");

// Ask if client needs more files


send_message(sockfd, "Do you need more files? (Yes/No)\n");

// Receive response
recv(sockfd, buffer, MAXLINE, 0);
if (strcmp(buffer, "Yes\n") == 0) {
// Repeat the process
} else {
// Close connection
close(sockfd);
printf("Connection with client %s closed.\n", inet_ntoa(client-
>address.sin_addr));
client->socket_fd = -1; // Mark client slot as available
}

return NULL;
}

int main() {
int sockfd;
struct sockaddr_in servaddr, cliaddr;
pthread_t tid;

// 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(sockfd, 5); // set a backlog for server

while (1) {
int len, new_sockfd;
write(1, wait_new_client, strlen(wait_new_client));
int clientLength = sizeof(cliaddr);
new_sockfd = accept(sockfd, (struct sockaddr*)&cliaddr,
&clientLength); // accept request from TCP client

// Store client information


client_list[num_clients].socket_fd = new_sockfd;
client_list[num_clients].address = cliaddr;
num_clients++;

pthread_create(&tid, NULL, handle_client,


(void*)&client_list[num_clients - 1]);
pthread_detach(tid); // Detach thread to avoid memory leaks
}

close(sockfd);
return 0;
}

You might also like