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();
}