Отправка буферизованного изображения по UDP в C ++

Я адаптировал этот блок кода из ответного сообщения здесь, которые сохраняют изображение в буфер. Теперь я хочу отправить этот буфер через пакет UDP. Я не понимаю, как использовать функцию sendto () в сокете. Я признателен за любую помощь, которую вы можете предоставить.

{
    IStream *pStream = NULL;
    LARGE_INTEGER liZero = {};
    ULARGE_INTEGER pos = {};
    STATSTG stg = {};
    ULONG bytesRead=0;
    HRESULT hrRet=S_OK;

    BYTE* buffer = NULL;  // this is your buffer that will hold the jpeg bytes
    DWORD dwBufferSize = 0;  // this is the size of that buffer;


    hrRet = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
    //hrRet = pScreenShot->Save(pStream, &imageCLSID, &encoderParams) == 0 ? S_OK : E_FAIL;
    bitmap.Save(pStream, &clsid, &encoderParameters);
    hrRet = pStream->Seek(liZero, STREAM_SEEK_SET, &pos);
    hrRet = pStream->Stat(&stg, STATFLAG_NONAME);

    // allocate a byte buffer big enough to hold the jpeg stream in memory
    buffer = new BYTE[stg.cbSize.LowPart];
    hrRet = (buffer == NULL) ? E_OUTOFMEMORY : S_OK;
    dwBufferSize = stg.cbSize.LowPart;

    // copy the stream into memory
    hrRet = pStream->Read(buffer, stg.cbSize.LowPart, &bytesRead);

    // now go save "buffer" and "dwBufferSize" off somewhere.  This is the jpeg buffer
    // don't forget to free it when you are done

    // After success or if any of the above calls fail, don't forget to release the stream
    if (pStream)
    {
        pStream->Release();
    }
}

person Ganesh    schedule 08.02.2014    source источник


Ответы (1)


Пожалуйста, найдите ниже код для отправки данных через UDP с использованием sendto(), вам просто нужно добавить свой код для чтения изображения и сохранить его в буфере (я уже упоминал в приведенном ниже коде, где добавить свой код). И, пожалуйста, не я пишу этот код в c, поэтому рядом с вами нужно поговорить с c по c++.

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h> 
#include <sys/time.h>

#define REMOTE_SERVER_PORT 1501
#define MAX_MSG 100


int main(int argc, char *argv[]) {

  int sd, rc, i;
  struct sockaddr_in cliAddr, remoteServAddr;
  struct hostent *h;
  FILE* filein;
 long lSize;
 char *buffer;

  /* get server IP address (no check if input is IP address or DNS name */
  h = gethostbyname(argv[1]);
  if(h==NULL) {
    printf("%s: unknown host '%s' \n", argv[0], argv[1]);
    exit(1);
  }

  printf("%s: sending data to '%s' (IP : %s) \n", argv[0], h->h_name,
     inet_ntoa(*(struct in_addr *)h->h_addr_list[0]));

  remoteServAddr.sin_family = h->h_addrtype;
  memcpy((char *) &remoteServAddr.sin_addr.s_addr, 
     h->h_addr_list[0], h->h_length);
  remoteServAddr.sin_port = htons(REMOTE_SERVER_PORT);

  /* socket creation */
  sd = socket(AF_INET,SOCK_DGRAM,0);
  if(sd<0) {
    printf("%s: cannot open socket \n",argv[0]);
    exit(1);
  }

  /* bind any port */
  cliAddr.sin_family = AF_INET;
  cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  cliAddr.sin_port = htons(0);

  rc = bind(sd, (struct sockaddr *) &cliAddr, sizeof(cliAddr));
  if(rc<0) {
    printf("%s: cannot bind port\n", argv[0]);
    exit(1);
  }

 //please put Here your code to read image and store it in buffer    


  // send data 

    rc = sendto(sd, buffer, sizeof(buffer)+1, 0, 
        (struct sockaddr *) &remoteServAddr, 
        sizeof(remoteServAddr));

    if(rc<0) {
      printf("%s: cannot send data %d \n",argv[0],i-1);
      close(sd);
      exit(1);
    }

  return 1;

}
person Jayesh Bhoi    schedule 08.02.2014
comment
Спасибо, я попробую. - person Ganesh; 08.02.2014