`sending data to client but getting segmentation fault on receiver. Here i am attaching my code. Please look into it.
To handle up to 10 clients simultaneously and save each received video file with a unique name, we’ll use multithreading on the server-side. Each client connection will be handled by a separate thread, and the server will create a unique filename for every received file.
Below is the updated server code to handle multiple clients concurrently using threads and generate unique filenames for each received file:
#define PORT 9999
#define BUFFER_SIZE 1024
using namespace std;
int main() {
int sock = 0;
struct sockaddr_in serv_addr;
char buffer[BUFFER_SIZE] = {0};
// Creating socket file descriptor
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
cout << "Socket creation error" << endl;
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 address from text to binary form
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
cout << "Invalid address/ Address not supported" << endl;
return -1;
}
// Connect to the server
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
cout << "Connection Failed" << endl;
return -1;
}
// Open the video file to send
ifstream videoFile("file.mp4", ios::binary);
if (!videoFile.is_open()) {
cerr << "Error opening video file." << endl;
return -1;
}
cout << "Sending video file to server..." << endl;
// Read from file and send in chunks
while (!videoFile.eof()) {
videoFile.read(buffer, BUFFER_SIZE);
int bytesRead = videoFile.gcount();
send(sock, buffer, bytesRead, 0);
}
cout << "File sent successfully!" << endl;
// Close file and socket
videoFile.close();
close(sock);
return 0;
}
#define PORT 9999
#define BUFFER_SIZE 1024
#define MAX_CLIENTS 5
using namespace std;
mutex fileMutex;
static int clientCount = 0;
// Function to handle each client connection
void handleClient(int clientSocket, int clientId) {
char buffer[BUFFER_SIZE] = {0};
int bytesReceived;
// Generate a unique filename for each client
string fileName = "received_video_client_" + to_string(clientId) + ".mp4";
// Locking file writing to avoid race conditions
{
lock_guard<mutex> guard(fileMutex);
cout << "Saving video file as: " << fileName << endl;
}
// Open the file to write the received video
ofstream videoFile(fileName, ios::binary);
if (!videoFile.is_open()) {
cerr << "Error opening file to write." << endl;
close(clientSocket);
return;
}
// Receive the file in chunks and write to the file
while ((bytesReceived = read(clientSocket, buffer, BUFFER_SIZE)) > 0) {
videoFile.write(buffer, bytesReceived);
}
if (bytesReceived < 0) {
cerr << "Error receiving file from client " << clientId << endl;
} else {
cout << "File received successfully from client " << clientId << "!" << endl;
}
// Close the file and socket
videoFile.close();
close(clientSocket);
// Decrease the client count after handling the client
// clientCount--;
}
int main() {
int server_fd, new_socket;
struct sockaddr_in address;
int addrlen = sizeof(address);
// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
// Bind the socket to the address and port
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
// Start listening for incoming connections
if (listen(server_fd, MAX_CLIENTS) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
cout << "Waiting for clients to connect..." << endl;
while (true) {
// Accept incoming connection
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
// Check if the server can handle more clients
if (clientCount < MAX_CLIENTS) {
clientCount++;
int clientId = clientCount; // Assign a unique ID to each client
// Create a new thread to handle the client connection
thread clientThread(handleClient, new_socket, clientId);
clientThread.detach(); // Detach the thread to let it run independently
} else {
cout << "Maximum client limit reached. Cannot accept more connections." << endl;
close(new_socket);
}
}
// Close the server socket
close(server_fd);
return 0;
}
I tried using gdb but not able to find out the solution`
Ajay Kumar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1