Page 1 of 1

C++ Server App to LiveCode App

Posted: Thu Jun 04, 2020 10:29 am
by Googie85
Hi Guys,

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();
}
Any ideas on how to receive the image file "RECEIVED.jpeg" in LiveCode would be a gods send!!

Many Thanks,

Googie.

P.S. Im really really sorry if this post is not supposed to be on this forum!!

Re: C++ Server App to LiveCode App

Posted: Thu Jun 04, 2020 7:55 pm
by FourthWorld
Googie85 wrote:
Thu Jun 04, 2020 10:29 am
P.S. Im really really sorry if this post is not supposed to be on this forum!!
Does it have to do with Android? Sounds like something you want to run on a server, yes? If so, I can move it to the Server forum.

As for the code itself, it seems like a socket server, one not using HTTP. Is that correct?

You could write that in LC, but it may be helpful to learn more about the use case: Is this on a secured local network, or the open Internet? Does the server perform any other functions? What is being used for the client that sends the image?