I have a simple server in C++ that receives an image file. The server first receives the "File Size" and then the "Data" of an image. Please delete this post if it doesn't belong in this forum. It would be wonderful if someone could point me in the right direction on how to receive the sent file into in to its LiveCode equivalent.
My C++ code is rather simple,
Code: Select all
#pragma warning(disable : 4996)
#include <WinSock2.h>
#include <Windows.h>
#include <stdio.h>
using namespace std;
#pragma comment(lib, "Ws2_32.lib")
#define Port 81
SOCKET Socket, Sub;
WSADATA Winsock;
sockaddr_in Addr;
sockaddr_in IncomingAddress;
int AddressLen = sizeof(IncomingAddress);
void FirstStage() {
WSAStartup(MAKEWORD(2, 2), &Winsock); // Start Winsock
if (LOBYTE(Winsock.wVersion) != 2 || HIBYTE(Winsock.wVersion) != 2) // Check version
{
WSACleanup();
printf("ERROR");
Sleep(5000);
exit(0);
}
Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
ZeroMemory(&Addr, sizeof(Addr));
Addr.sin_family = AF_INET;
Addr.sin_port = htons(Port);
bind(Socket, (sockaddr*)&Addr, sizeof(Addr));
if (listen(Socket, 1) == SOCKET_ERROR)
{
printf("listening error\n");
}
else
{
printf("listening ok\n");
}
if ((Sub = accept(Socket, (sockaddr*)&IncomingAddress, &AddressLen)))
{
char* ClientIP = inet_ntoa(IncomingAddress.sin_addr);
int ClientPort = ntohs(IncomingAddress.sin_port);
printf("Client conncted!\n");
printf("IP: %s:%d\n", ClientIP, ClientPort);
printf("Receiving file .. \n");
int Size = 0;
char* Filesize = new char[1024];
if (recv(Sub, Filesize, 1024, 0)) // File size
{
Size = atoi(Filesize);
printf("File size: %d\n", Size);
}
char* Buffer = new char[Size];
int Offset = 0;
while (Size > Offset)
{
int Amount = recv(Sub, Buffer + Offset, Size - Offset, 0);
if (Amount <= 0)
{
break;
}
else
{
Offset += Amount;
printf("Received %d\n", Offset);
}
}
FILE* File = fopen("C://Users//h5n1s//Desktop//RECEIVED.jpeg", "wb");
fwrite(Buffer, 1, Size, File);
fclose(File);
}
ShellExecute(0, L"open", L"C://Users//h5n1s//Desktop//RECEIVED.jpeg", NULL, NULL, SW_NORMAL);
Sleep(3000);
exit(0);
}
int main()
{
CreateMutexA(0, FALSE, "lop"); // try to create a named mutex
if (GetLastError() == ERROR_ALREADY_EXISTS) // did the mutex already exist?
exit(0);
else {
}
Sleep(3000);
HWND Stealth;
AllocConsole();
Stealth = FindWindowA("ConsoleWindowClass", NULL);
ShowWindow(Stealth, 0);
FirstStage();
}
Many Thanks,
Googie.
P.S. Im really really sorry if this post is not supposed to be on this forum!!