KEMBAR78
Fixed CPP Programs | PDF | Computer Programming | C++
0% found this document useful (0 votes)
4 views3 pages

Fixed CPP Programs

The document contains corrected C++ programs demonstrating multithreading and socket communication. It includes two examples of threading: one with two threads counting to 100 and another with five threads printing 'Hello World'. Additionally, it provides server and client socket communication code for sending and receiving messages over TCP.

Uploaded by

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

Fixed CPP Programs

The document contains corrected C++ programs demonstrating multithreading and socket communication. It includes two examples of threading: one with two threads counting to 100 and another with five threads printing 'Hello World'. Additionally, it provides server and client socket communication code for sending and receiving messages over TCP.

Uploaded by

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

Fixed and Corrected C++ Programs

11.a - Two Threads Counting to 100


#include <iostream>
#include <pthread.h>

void* countTo100(void* arg) {


int threadNum = *((int*)arg);
delete (int*)arg;
for (int i = 1; i <= 100; ++i) {
std::cout << "Thread " << threadNum << ": Count " << i << std::endl;
}
pthread_exit(nullptr);
}

int main() {
const int numThreads = 2;
pthread_t threads[numThreads];
for (int i = 0; i < numThreads; ++i) {
int* threadNum = new int(i);
pthread_create(&threads[i], nullptr, countTo100, threadNum);
}
for (int i = 0; i < numThreads; ++i) {
pthread_join(threads[i], nullptr);
}
return 0;
}

11.b - 5 Threads Printing Hello World


#include <iostream>
#include <pthread.h>

void* printHello(void* arg) {


int threadNum = *((int*)arg);
delete (int*)arg;
std::cout << "Hello World from Thread " << threadNum << std::endl;
pthread_exit(nullptr);
}

int main() {
const int numThreads = 5;
pthread_t threads[numThreads];
for (int i = 0; i < numThreads; ++i) {
int* threadNum = new int(i + 1);
pthread_create(&threads[i], nullptr, printHello, threadNum);
}
for (int i = 0; i < numThreads; ++i) {
pthread_join(threads[i], nullptr);
}
return 0;
}

12.a - server.cpp (Socket Communication)


#include <iostream>
#include <cstring>
#include <unistd.h>
#include <arpa/inet.h>

int main() {
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket == -1) {
std::cerr << "Error creating socket." << std::endl;
return -1;
}

sockaddr_in serverAddress{};
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = INADDR_ANY;
serverAddress.sin_port = htons(8080);

if (bind(serverSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) ==


-1) {
std::cerr << "Error binding socket." << std::endl;
close(serverSocket);
return -1;
}

if (listen(serverSocket, 5) == -1) {
std::cerr << "Error listening for connections." << std::endl;
close(serverSocket);
return -1;
}

std::cout << " Server listening on port 8080..." << std::endl;

sockaddr_in clientAddress{};
socklen_t clientAddrSize = sizeof(clientAddress);
int clientSocket = accept(serverSocket, (struct sockaddr*)&clientAddress,
&clientAddrSize);
if (clientSocket == -1) {
std::cerr << "Error accepting connection." << std::endl;
close(serverSocket);
return -1;
}

char buffer[1024] = {0};


ssize_t bytesRead = recv(clientSocket, buffer, sizeof(buffer) - 1, 0);
if (bytesRead == -1) {
std::cerr << "Error receiving data." << std::endl;
close(clientSocket);
close(serverSocket);
return -1;
}

buffer[bytesRead] = '\0';
std::cout << " Received from client: " << buffer << std::endl;

close(clientSocket);
close(serverSocket);
return 0;
}

12.b - client.cpp (Socket Communication)


#include <iostream>
#include <cstring>
#include <unistd.h>
#include <arpa/inet.h>

int main() {
int clientSocket = socket(AF_INET, SOCK_STREAM, 0);
if (clientSocket == -1) {
std::cerr << "Error creating socket." << std::endl;
return -1;
}

sockaddr_in serverAddress{};
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(8080);
if (inet_pton(AF_INET, "127.0.0.1", &serverAddress.sin_addr) <= 0) {
std::cerr << "Invalid address or not supported." << std::endl;
close(clientSocket);
return -1;
}

if (connect(clientSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress))


== -1) {
std::cerr << "Connection failed." << std::endl;
close(clientSocket);
return -1;
}

std::cout << " Connected to the server. Sending data..." << std::endl;
const char* message = "Hello from the client!";
if (send(clientSocket, message, strlen(message), 0) == -1) {
std::cerr << "Error sending data." << std::endl;
close(clientSocket);
return -1;
}

close(clientSocket);
return 0;
}

You might also like