Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using Visual Studio Ultimate 2013.

I have developed a simple client and server application using winsock API in C++ where the client sends hello message to the server. But, I want to send multiple images to server at once from the client socket. I don't know how to send image files using socket programming. Please help me with sending of multiple image files at once using winsock.

My code is as under:

What I have tried:

Client:
#include <iostream>
#include <winsock2.h>

using namespace std;

#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")

static void ClientApp()
{
    WSADATA WSAData;
    SOCKET server;
    SOCKADDR_IN addr;

    WSAStartup(MAKEWORD(2, 0), &WSAData);
    server = socket(AF_INET, SOCK_STREAM, 0);

    addr.sin_addr.s_addr = inet_addr("192.168.100.4");
    addr.sin_family = AF_INET;
    addr.sin_port = htons(5555);

    connect(server, (SOCKADDR *)&addr, sizeof(addr));
    cout << "Connected to server!" << endl;

    char buffer[1024] = { 'h', 'e', 'l', 'l', 'o', '.' };
    send(server, buffer, sizeof(buffer), 0);
    cout << "Message sent!" << endl;

    closesocket(server);
    WSACleanup();
    cout << "Socket closed." << endl << endl;
}

int main()
{
    ClientApp();
}



Server:
#include <iostream>
#include <winsock2.h>

using namespace std;

#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")

static void ServerApp()
{
    WSADATA WSAData;

    SOCKET server, client;

    SOCKADDR_IN serverAddr, clientAddr;

    WSAStartup(MAKEWORD(2, 0), &WSAData);
    server = socket(AF_INET, SOCK_STREAM, 0);

    serverAddr.sin_addr.s_addr = INADDR_ANY;
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(5555);

    bind(server, (SOCKADDR *)&serverAddr, sizeof(serverAddr));
    listen(server, 0);

    cout << "Listening for incoming connections..." << endl;

    char buffer[1024];
    int clientAddrSize = sizeof(clientAddr);
    if ((client = accept(server, (SOCKADDR *)&clientAddr, &clientAddrSize)) != INVALID_SOCKET)
    {
        cout << "Client connected!" << endl;
        recv(client, buffer, sizeof(buffer), 0);
        cout << "Client says: " << buffer << endl;
        memset(buffer, 0, sizeof(buffer));

        closesocket(client);
        cout << "Client disconnected." << endl;
    }
}

int main()
{
    ServerApp();
}
Posted
Updated 8-Sep-17 22:50pm

You send image files the same way you send any data. You just need to ensure that each endpoint can understand when a new file starts and finishes.
 
Share this answer
 
You have to define a protocol (or use an existing one). You can for example create a structure that contains the additional information required by the receiver to save the images as files:
C++
#include <stdint.h>

struct myheader {
    uint32_t id; // an ID to identify the header
    uint32_t hdr_len; // the total length of this header
    uint32_t file_num; // the index for this file
    uint32_t file_count; // the total number of files
    uint32_t file_len; // the length of the file
    uint32_t name_len; // the length of the file name
    wchar_t file_name[1]; // the name of the file without path
};

The sender must now create a buffer for that structure that has additional space for the file name, and for each file fill the members, send the buffer, and send the image file content:
C++
uint8_t buf[sizeof(myheader) + sizeof(wchar_t) * MAX_FILE_NAME_LEN];
myheader *pHeader = reinterpret_cast<myheader*>(buf);
// initialise pHeader
pHeader->id = MY_HEADER_ID;
pHeader->file_num = 0;
pHeader->file_count = 1; // or more
pHeader->name_len = sizeof(wchar_t) * (1 + wcslen(file_name));
wcscpy(pHeader->file_name, file_name);
pHeader->hdr_len = sizeof(myheader) - sizeof(wchar_t) + pHeader->name_len;
pHeader->file_len = file_len;

// send header using pointer buf and size pHeader->hdr_len
// sende image file content
// increment pHeader->file_num and process next file


The receiver has to do it similar:
C++
uint8_t buf[sizeof(myheader) + sizeof(wchar_t) * MAX_FILE_NAME_LEN];
myheader *pHeader = reinterpret_cast<myheader*>(buf);
// Read into buf with sizeof(myheader)
// Read into pHeader->file_name+1 with pHeader->hdr_len - sizeof(myheader)
// Allocate buffer for image content (full size pHeader->file_len or partial size for writing to file)
// Read image file content into buffer and process data (e.g. write to file)
// Repeat until pHeader->file_num == pHeader->file_count - 1
 
Share this answer
 
Comments
raushanaj5 9-Sep-17 5:04am    
In which part of the code should I make these changes?
Jochen Arndt 9-Sep-17 6:04am    
When sending and receiving. It is just an example how it might be implemented.

You still have to add the code to pass image file names and read them into memory for sending, and process the received data (write files).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900