I am getting segmentation fault on sender and reciever socket code

`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`

New contributor

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật